|
@@ -33,85 +33,87 @@ class Tasks(BaseCommand): |
|
|
else: |
|
|
else: |
|
|
self.connection.reply(data, "no arguments provided. Maybe you wanted '!{cmnd} list', '!{cmnd} start', or '!{cmnd} listall'?".format(cmnd=data.command)) |
|
|
self.connection.reply(data, "no arguments provided. Maybe you wanted '!{cmnd} list', '!{cmnd} start', or '!{cmnd} listall'?".format(cmnd=data.command)) |
|
|
return |
|
|
return |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if data.args[0] == "list": |
|
|
if data.args[0] == "list": |
|
|
self.do_list() |
|
|
self.do_list() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
elif data.args[0] == "start": |
|
|
elif data.args[0] == "start": |
|
|
self.do_start() |
|
|
self.do_start() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
elif data.args[0] in ["listall", "all"]: |
|
|
elif data.args[0] in ["listall", "all"]: |
|
|
self.do_listall() |
|
|
self.do_listall() |
|
|
|
|
|
|
|
|
else: # they asked us to do something we don't know |
|
|
else: # they asked us to do something we don't know |
|
|
self.connection.reply(data, "unknown argument: \x0303{}\x0301.".format(data.args[0])) |
|
|
|
|
|
|
|
|
self.connection.reply(data, "unknown argument: \x0303{0}\x0301.".format(data.args[0])) |
|
|
|
|
|
|
|
|
def do_list(self): |
|
|
def do_list(self): |
|
|
threads = threading.enumerate() |
|
|
threads = threading.enumerate() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
normal_threads = [] |
|
|
normal_threads = [] |
|
|
task_threads = [] |
|
|
task_threads = [] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for thread in threads: |
|
|
for thread in threads: |
|
|
tname = thread.name |
|
|
tname = thread.name |
|
|
if tname == "MainThread": |
|
|
if tname == "MainThread": |
|
|
tname = self.get_main_thread_name() |
|
|
tname = self.get_main_thread_name() |
|
|
normal_threads.append("\x0302{}\x0301 (as main thread, id {})".format(tname, thread.ident)) |
|
|
|
|
|
|
|
|
normal_threads.append("\x0302{0}\x0301 (as main thread, id {1})".format(tname, thread.ident)) |
|
|
elif tname in ["irc-frontend", "irc-watcher", "wiki-scheduler"]: |
|
|
elif tname in ["irc-frontend", "irc-watcher", "wiki-scheduler"]: |
|
|
normal_threads.append("\x0302{}\x0301 (id {})".format(tname, thread.ident)) |
|
|
|
|
|
|
|
|
normal_threads.append("\x0302{0}\x0301 (id {1})".format(tname, thread.ident)) |
|
|
|
|
|
elif tname.startswith("reminder"): |
|
|
|
|
|
normal_threads.append("\x0302reminder\x0301 (until {0})".format(tname.replace("reminder ", ""))) |
|
|
else: |
|
|
else: |
|
|
tname, start_time = re.findall("^(.*?) \((.*?)\)$", tname)[0] |
|
|
tname, start_time = re.findall("^(.*?) \((.*?)\)$", tname)[0] |
|
|
task_threads.append("\x0302{}\x0301 (id {}, since {})".format(tname, thread.ident, start_time)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
task_threads.append("\x0302{0}\x0301 (id {1}, since {2})".format(tname, thread.ident, start_time)) |
|
|
|
|
|
|
|
|
if task_threads: |
|
|
if task_threads: |
|
|
msg = "\x02{}\x0F threads active: {}, and \x02{}\x0F task threads: {}.".format(len(threads), ', '.join(normal_threads), len(task_threads), ', '.join(task_threads)) |
|
|
|
|
|
|
|
|
msg = "\x02{0}\x0F threads active: {1}, and \x02{2}\x0F task threads: {3}.".format(len(threads), ', '.join(normal_threads), len(task_threads), ', '.join(task_threads)) |
|
|
else: |
|
|
else: |
|
|
msg = "\x02{}\x0F threads active: {}, and \x020\x0F task threads.".format(len(threads), ', '.join(normal_threads)) |
|
|
|
|
|
|
|
|
msg = "\x02{0}\x0F threads active: {1}, and \x020\x0F task threads.".format(len(threads), ', '.join(normal_threads)) |
|
|
self.connection.reply(self.data, msg) |
|
|
self.connection.reply(self.data, msg) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def do_listall(self): |
|
|
def do_listall(self): |
|
|
tasks = task_manager.task_list.keys() |
|
|
tasks = task_manager.task_list.keys() |
|
|
threads = threading.enumerate() |
|
|
threads = threading.enumerate() |
|
|
tasklist = [] |
|
|
tasklist = [] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tasks.sort() |
|
|
tasks.sort() |
|
|
|
|
|
|
|
|
for task in tasks: |
|
|
for task in tasks: |
|
|
threads_running_task = [t for t in threads if t.name.startswith(task)] |
|
|
threads_running_task = [t for t in threads if t.name.startswith(task)] |
|
|
ids = map(lambda t: str(t.ident), threads_running_task) |
|
|
ids = map(lambda t: str(t.ident), threads_running_task) |
|
|
if not ids: |
|
|
if not ids: |
|
|
tasklist.append("\x0302{}\x0301 (idle)".format(task)) |
|
|
|
|
|
|
|
|
tasklist.append("\x0302{0}\x0301 (idle)".format(task)) |
|
|
elif len(ids) == 1: |
|
|
elif len(ids) == 1: |
|
|
tasklist.append("\x0302{}\x0301 (\x02active\x0F as id {})".format(task, ids[0])) |
|
|
|
|
|
|
|
|
tasklist.append("\x0302{0}\x0301 (\x02active\x0F as id {1})".format(task, ids[0])) |
|
|
else: |
|
|
else: |
|
|
tasklist.append("\x0302{}\x0301 (\x02active\x0F as ids {})".format(task, ', '.join(ids))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tasklist.append("\x0302{0}\x0301 (\x02active\x0F as ids {1})".format(task, ', '.join(ids))) |
|
|
|
|
|
|
|
|
tasklist = ", ".join(tasklist) |
|
|
tasklist = ", ".join(tasklist) |
|
|
|
|
|
|
|
|
msg = "{} tasks loaded: {}.".format(len(tasks), tasklist) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
msg = "{0} tasks loaded: {1}.".format(len(tasks), tasklist) |
|
|
self.connection.reply(self.data, msg) |
|
|
self.connection.reply(self.data, msg) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def do_start(self): |
|
|
def do_start(self): |
|
|
data = self.data |
|
|
data = self.data |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try: |
|
|
try: |
|
|
task_name = data.args[1] |
|
|
task_name = data.args[1] |
|
|
except IndexError: # no task name given |
|
|
except IndexError: # no task name given |
|
|
self.connection.reply(data, "what task do you want me to start?") |
|
|
self.connection.reply(data, "what task do you want me to start?") |
|
|
return |
|
|
return |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try: |
|
|
try: |
|
|
data.parse_kwargs() |
|
|
data.parse_kwargs() |
|
|
except KwargParseException, arg: |
|
|
except KwargParseException, arg: |
|
|
self.connection.reply(data, "error parsing argument: \x0303{}\x0301.".format(arg)) |
|
|
|
|
|
|
|
|
self.connection.reply(data, "error parsing argument: \x0303{0}\x0301.".format(arg)) |
|
|
return |
|
|
return |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if task_name not in task_manager.task_list.keys(): # this task does not exist or hasn't been loaded |
|
|
if task_name not in task_manager.task_list.keys(): # this task does not exist or hasn't been loaded |
|
|
self.connection.reply(data, "task could not be found; either wiki/tasks/{}.py doesn't exist, or it wasn't loaded correctly.".format(task_name)) |
|
|
|
|
|
|
|
|
self.connection.reply(data, "task could not be found; either wiki/tasks/{0}.py doesn't exist, or it wasn't loaded correctly.".format(task_name)) |
|
|
return |
|
|
return |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
task_manager.start_task(task_name, **data.kwargs) |
|
|
task_manager.start_task(task_name, **data.kwargs) |
|
|
self.connection.reply(data, "task \x0302{}\x0301 started.".format(task_name)) |
|
|
|
|
|
|
|
|
self.connection.reply(data, "task \x0302{0}\x0301 started.".format(task_name)) |
|
|
|
|
|
|
|
|
def get_main_thread_name(self): |
|
|
def get_main_thread_name(self): |
|
|
"""Return the "proper" name of the MainThread; e.g. "irc-frontend" or "irc-watcher".""" |
|
|
"""Return the "proper" name of the MainThread; e.g. "irc-frontend" or "irc-watcher".""" |
|
|