1
# gozerbot/plugs/job.py
3
# (c) Wijnand 'tehmaze' Modderman - http://tehmaze.com
6
""" job management. """
8
__author__ = "Wijnand 'tehmaze' Modderman - http://tehmaze.com"
12
from gozerbot.aliases import aliases
13
from gozerbot.commands import cmnds
14
from gozerbot.periodical import periodical, at
15
from gozerbot.plugins import plugins
16
from gozerbot.examples import examples
17
from gozerbot.plughelp import plughelp
18
from gozerbot.utils.generic import uniqlist
19
from gozerbot.tests import tests
20
from gozerbot.utils.statdict import Statdict
23
import copy, datetime, time, types
25
plughelp.add('job', 'job management')
29
""" show nr of running jobs. """
31
return len(periodical.jobs)
33
def handle_job(bot, ievent):
35
""" show data of <jobid>. """
37
if not ievent.args or not ievent.args[0].isdigit():
38
ievent.reply('<job id>')
41
for job in periodical.jobs:
42
if job.id() == int(ievent.args[0]):
44
if type(next) in [types.FloatType, types.IntType]:
45
next = datetime.datetime(*time.localtime(next)[:7])
46
ievent.reply('%s, fires at %s' % (job.__repr__(), str(next)))
49
ievent.reply('job not found')
51
cmnds.add('job', handle_job, 'USER')
52
examples.add('job', 'show job data of <jobid> ', 'job 1')
55
def handle_joblist(bot, ievent):
57
""" show job list. """
60
group = ievent.args[0]
65
for job in periodical.jobs:
66
if group and not job.group == group:
69
result.append('%d (%s)' % (job.id(), job.description))
71
result.append('%d (%s)' % (job.id(), str(job.func.func_name)))
74
ievent.reply('jobs scheduled: ', result, dot=True)
76
ievent.reply('no jobs')
78
cmnds.add('job-list', handle_joblist, 'OPER')
79
examples.add('job-list', 'show all waiting jobs or all jobs belonging to [group]', '1) job-list 2) job-list rss')
80
aliases.data['jobs'] = 'job-list'
81
tests.add('job-list', 'runner')
83
def handle_jobgroups(bot, ievent):
85
""" show job groups. """
87
result = [job.group for job in periodical.jobs]
90
ievent.reply('job groups: ', uniqlist(result), dot=True)
92
ievent.reply('no jobs')
94
cmnds.add('job-groups', handle_jobgroups, 'OPER')
95
examples.add('job-groups', 'show all job groups', 'job-groups')
96
tests.add('job-groups')
98
def handle_jobkill(bot, ievent):
102
if not ievent.args or not ievent.args[0].isdigit():
103
ievent.missing('<job id> [<job id> ...]')
107
ids = [int(jid) for jid in ievent.args]
109
ievent.missing('<job id> [<job id> ...]')
113
periodical.killjob(int(ievent.args[0]))
115
ievent.reply('killed %d jobs' % len(ids))
117
cmnds.add('job-kill', handle_jobkill, 'OPER')
118
examples.add('job-kill', 'kill job with <jobid>', 'job-kill 100000')
120
def handle_jobrunning(bot, ievent):
122
""" show running job threads. """
125
for job in periodical.running:
126
result.upitem(job.description)
127
ievent.reply("running jobs: ", result)
129
cmnds.add("job-running", handle_jobrunning, 'OPER')
130
examples.add("job-running", "show running job threads", "job-running")