From ea83a59372f8dc755832599289d2edae0a53c8bd Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Sat, 14 May 2011 13:57:14 -0400 Subject: [PATCH] give ids of active tasks with .tasks listall; give start time of threads with .tasks list; fix in task_manager.py --- irc/commands/tasks.py | 17 +++++++++++------ wiki/task_manager.py | 3 +-- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/irc/commands/tasks.py b/irc/commands/tasks.py index 2c87899..13504b3 100644 --- a/irc/commands/tasks.py +++ b/irc/commands/tasks.py @@ -52,7 +52,6 @@ class Tasks(BaseCommand): normal_threads = [] task_threads = [] - task_thread_num = 0 for thread in threads: tname = thread.name @@ -62,25 +61,31 @@ class Tasks(BaseCommand): elif tname in ["irc-frontend", "irc-watcher", "wiki-scheduler"]: normal_threads.append("\x0302{}\x0301 (id {})".format(tname, thread.ident)) else: - task_thread_num += 1 - task_threads.append("\x0302{}\x0301 (id {})".format(tname, thread.ident)) + tname, start_time = re.findall("^(.*?) \((.*?)\)$")[0] + task_threads.append("\x0302{}\x0301 (id {}, spawned at {})".format(tname, thread.ident, start_time)) if task_threads: - msg = "\x02{}\x0F threads active: {}, and \x02{}\x0F task threads: {}.".format(len(threads), ', '.join(normal_threads), task_thread_num, ', '.join(task_threads)) + msg = "\x02{}\x0F threads active: {}, and \x02{}\x0F task threads: {}.".format(len(threads), ', '.join(normal_threads), len(task_threads), ', '.join(task_threads)) else: msg = "\x02{}\x0F threads active: {}, and \x020\x0F task threads.".format(len(threads), ', '.join(normal_threads)) self.connection.reply(self.data, msg) def do_listall(self): tasks = task_manager.task_list.keys() - threads = map(lambda t: t.name, threading.enumerate()) + threadlist = threading.enumerate() + threads = map(lambda t: t.name, threadlist) tasklist = [] tasks.sort() for task in tasks: if task in threads: - tasklist.append("\x0302{}\x0301 (\x02active\x0F)".format(task)) + threads_running_task = [t for t in threads if t.name.startswith(task)] + ids = map(lambda t: t.ident, threads_running_task) + if len(ids) == 1: + tasklist.append("\x0302{}\x0301 (\x02active\x0F as id {})".format(task, ids[0])) + else: + tasklist.append("\x0302{}\x0301 (\x02active\x0F as ids {})".format(task, ' ,'.join(ids))) else: tasklist.append("\x0302{}\x0301 (idle)".format(task)) diff --git a/wiki/task_manager.py b/wiki/task_manager.py index 8646839..cd3b79b 100644 --- a/wiki/task_manager.py +++ b/wiki/task_manager.py @@ -63,9 +63,8 @@ def start_task(task_name, **kwargs): print "Couldn't find task '{}': wiki/tasks/{}.py does not exist.".format(task_name, task_name) return - # task_thread = threading.Thread(target=task_wrapper, args=(task, kwargs)) task_thread = threading.Thread(target=lambda: task_wrapper(task, **kwargs)) # Normally we'd do task_wrapper(task, **kwargs), but because of threading we'd have to do Thread(target=task_wrapper, args=(task, **kwargs)), which doesn't work because the **kwargs is inside a tuple, not inside function params. Use lambda to get around the args=tuple nonsense - task_thread.name = "task {} (spawned at {} UTC)".format(task_name, time.asctime()) + task_thread.name = "{} ({} UTC)".format(task_name, time.asctime()) task_thread.daemon = True # stop bot task threads automagically if the main bot stops task_thread.start()