~ubuntu-branches/ubuntu/utopic/gozerbot/utopic

« back to all changes in this revision

Viewing changes to gozerbot/plugs/job.py

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Malcolm
  • Date: 2009-09-14 09:00:29 UTC
  • mfrom: (1.1.4 upstream) (3.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20090914090029-uval0ekt72kmklxw
Tags: 0.9.1.3-3
Changed dependency on python-setuptools to python-pkg-resources
(Closes: #546435) 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# gozerbot/plugs/job.py
 
2
#
 
3
# (c) Wijnand 'tehmaze' Modderman - http://tehmaze.com
 
4
# BSD License
 
5
 
 
6
""" job management. """
 
7
 
 
8
__author__ = "Wijnand 'tehmaze' Modderman - http://tehmaze.com"
 
9
__license__ = "BSD"
 
10
 
 
11
# gozerbot imports
 
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
 
21
 
 
22
# basic imports
 
23
import copy, datetime, time, types
 
24
 
 
25
plughelp.add('job', 'job management')
 
26
 
 
27
def size():
 
28
 
 
29
    """ show nr of running jobs. """
 
30
 
 
31
    return len(periodical.jobs)
 
32
 
 
33
def handle_job(bot, ievent):
 
34
 
 
35
    """ show data of <jobid>. """
 
36
 
 
37
    if not ievent.args or not ievent.args[0].isdigit():
 
38
        ievent.reply('<job id>')
 
39
        return
 
40
 
 
41
    for job in periodical.jobs:
 
42
        if job.id() == int(ievent.args[0]):
 
43
            next = job.next
 
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)))
 
47
            return
 
48
 
 
49
    ievent.reply('job not found')
 
50
 
 
51
cmnds.add('job', handle_job, 'USER')
 
52
examples.add('job', 'show job data of <jobid> ', 'job 1')
 
53
tests.add('job 1')
 
54
 
 
55
def handle_joblist(bot, ievent):
 
56
 
 
57
    """ show job list. """
 
58
 
 
59
    try:
 
60
        group = ievent.args[0]
 
61
    except IndexError:
 
62
        group = None
 
63
    result = []
 
64
 
 
65
    for job in periodical.jobs:
 
66
        if group and not job.group == group:
 
67
            continue
 
68
        if job.description:
 
69
            result.append('%d (%s)' % (job.id(), job.description))
 
70
        else:
 
71
            result.append('%d (%s)' % (job.id(), str(job.func.func_name)))
 
72
 
 
73
    if result:
 
74
        ievent.reply('jobs scheduled: ', result, dot=True)
 
75
    else:
 
76
        ievent.reply('no jobs')
 
77
 
 
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')
 
82
 
 
83
def handle_jobgroups(bot, ievent):
 
84
 
 
85
    """ show job groups. """
 
86
 
 
87
    result = [job.group for job in periodical.jobs]
 
88
 
 
89
    if result:
 
90
        ievent.reply('job groups: ', uniqlist(result), dot=True)
 
91
    else:
 
92
        ievent.reply('no jobs')
 
93
 
 
94
cmnds.add('job-groups', handle_jobgroups, 'OPER')
 
95
examples.add('job-groups', 'show all job groups', 'job-groups')
 
96
tests.add('job-groups')
 
97
 
 
98
def handle_jobkill(bot, ievent):
 
99
 
 
100
    """ kill a job. """
 
101
 
 
102
    if not ievent.args or not ievent.args[0].isdigit():
 
103
        ievent.missing('<job id> [<job id> ...]')
 
104
        return
 
105
 
 
106
    try:
 
107
        ids = [int(jid) for jid in ievent.args]
 
108
    except ValueError:
 
109
        ievent.missing('<job id> [<job id> ...]')
 
110
        return
 
111
 
 
112
    for jid in ids:
 
113
        periodical.killjob(int(ievent.args[0]))
 
114
 
 
115
    ievent.reply('killed %d jobs' % len(ids))
 
116
 
 
117
cmnds.add('job-kill', handle_jobkill, 'OPER')
 
118
examples.add('job-kill', 'kill job with <jobid>', 'job-kill 100000')
 
119
 
 
120
def handle_jobrunning(bot, ievent):
 
121
 
 
122
    """ show running job threads. """
 
123
 
 
124
    result = Statdict()
 
125
    for job in periodical.running:
 
126
        result.upitem(job.description)
 
127
    ievent.reply("running jobs: ", result)
 
128
 
 
129
cmnds.add("job-running", handle_jobrunning, 'OPER')
 
130
examples.add("job-running", "show running job threads", "job-running")