1
2
3
4
5
6
7
8
9
10
11 """
12 Operations for jobs.
13
14 Available at: /zport/dmd/jobs_router
15 """
16
17 import logging
18 from time import mktime
19 from datetime import datetime
20 from collections import defaultdict
21 from Products import Zuul
22 from Products.ZenUtils.Ext import DirectRouter, DirectResponse
23 from Products.ZenMessaging.audit import audit
24 from Products.Jobber.exceptions import NoSuchJobException
25
26 log = logging.getLogger('zen.JobsRouter')
27
28
29 JOBKEYS = ['uuid', 'type', 'description', 'scheduled', 'started', 'finished',
30 'status', 'user']
31
32
34 """
35 A JSON/Ext.Direct interface to operations on jobs
36 """
38 self.api = Zuul.getFacade('jobs', context.dmd)
39 self.context = context
40 self.request = request
41 super(DirectRouter, self).__init__(context, request)
42
43 - def getJobs(self, start, limit, page, sort, dir, uid=None):
44
45 user = self.context.dmd.ZenUsers.getUserSettings()
46 createdBy = user.id if user.hasNoGlobalRoles() else None
47
48 jobs, total = self.api.getJobs(start=start, limit=limit, sort=sort, dir=dir, createdBy=createdBy)
49 return DirectResponse(jobs=Zuul.marshal(jobs, keys=JOBKEYS), totalCount=total)
50
52 for id_ in jobids:
53 try:
54 self.api.abortJob(id_)
55 except NoSuchJobException:
56 log.debug("Unable to abort job: %s No such job found.", id_)
57
59 for id_ in jobids:
60 try:
61 self.api.deleteJob(id_)
62 except NoSuchJobException:
63 log.debug("Unable to delete job: %s No such job found.", id_)
64
66 try:
67 logfile, content, maxLimit = self.api.getJobDetail(jobid)
68 except NoSuchJobException:
69
70
71 logfile, content, maxLimit = None, None, None
72 return {'content': content, 'logfile': logfile, 'maxLimit' : maxLimit}
73
75 results = defaultdict(list)
76 totals = {}
77 validstates = {'STARTED':'started', 'SUCCESS':'finished',
78 'PENDING':'scheduled'}
79 for job in self.api.getUserJobs():
80 if job.status in validstates:
81 results[job.status].append(job)
82
83 for status, jobs in results.iteritems():
84 jobs.sort(key=lambda j:getattr(j, validstates[status]),
85 reverse=True)
86 totals[status] = len(jobs)
87 results[status] = jobs[:10]
88 return DirectResponse(jobs=Zuul.marshal(results, keys=JOBKEYS),
89 totals=totals)
90