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 import cgi
17 import logging
18 from collections import defaultdict
19 from Products import Zuul
20 from Products.ZenUtils.Ext import DirectRouter, DirectResponse
21 from Products.Jobber.exceptions import NoSuchJobException
22
23 log = logging.getLogger('zen.JobsRouter')
24
25
26 JOBKEYS = ['uuid', 'type', 'description', 'scheduled', 'started', 'finished',
27 'status', 'user']
28
29
31 """
32 A JSON/Ext.Direct interface to operations on jobs
33 """
35 self.api = Zuul.getFacade('jobs', context.dmd)
36 self.context = context
37 self.request = request
38 super(DirectRouter, self).__init__(context, request)
39
40 - def getJobs(self, start, limit, page, sort, dir, uid=None):
41
42 user = self.context.dmd.ZenUsers.getUserSettings()
43 createdBy = user.id if user.hasNoGlobalRoles() else None
44
45 results, total = self.api.getJobs(start=start, limit=limit, sort=sort,
46 dir=dir, createdBy=createdBy)
47 jobs = Zuul.marshal(results, keys=JOBKEYS)
48 for job in jobs:
49 job['description'] = cgi.escape(job.get('description') or '')
50 return DirectResponse(jobs=jobs, totalCount=total)
51
53 for id_ in jobids:
54 try:
55 self.api.abortJob(id_)
56 except NoSuchJobException:
57 log.debug("Unable to abort job: %s No such job found.", id_)
58
60
61 if not Zuul.checkPermission('Manage DMD'):
62 return DirectResponse.fail("You don't have permission to execute this command", sticky=False)
63
64 deletedJobs = []
65 for id_ in jobids:
66 try:
67 self.api.deleteJob(id_)
68 except NoSuchJobException:
69 log.debug("Unable to delete job: %s No such job found.", id_)
70 else:
71 deletedJobs.append(id_)
72 if deletedJobs:
73 return DirectResponse.succeed(deletedJobs=Zuul.marshal(deletedJobs))
74
76 job = self.api.getInfo(jobid)
77 return DirectResponse.succeed(data=Zuul.marshal(job))
78
80 try:
81 logfile, content, maxLimit = self.api.getJobDetail(jobid)
82 except NoSuchJobException:
83
84
85 logfile, content, maxLimit = None, None, None
86 return {'content': content, 'logfile': logfile, 'maxLimit' : maxLimit}
87
89 results = defaultdict(list)
90 totals = {}
91 validstates = {'STARTED':'started', 'SUCCESS':'finished',
92 'PENDING':'scheduled'}
93 for job in self.api.getUserJobs():
94 if job.status in validstates:
95 results[job.status].append(job)
96
97 for status, jobs in results.iteritems():
98 jobs.sort(key=lambda j:getattr(j, validstates[status]),
99 reverse=True)
100 totals[status] = len(jobs)
101 results[status] = jobs[:10]
102 jobs = Zuul.marshal(results, keys=JOBKEYS)
103 for joblist in jobs.itervalues():
104 for job in joblist:
105 job['description'] = cgi.escape(job['description'])
106 return DirectResponse(jobs=jobs, totals=totals)
107