Package Products :: Package Zuul :: Package routers :: Module jobs
[hide private]
[frames] | no frames]

Source Code for Module Products.Zuul.routers.jobs

 1  ############################################################################## 
 2  #  
 3  # Copyright (C) Zenoss, Inc. 2010, all rights reserved. 
 4  #  
 5  # This content is made available according to terms specified in 
 6  # License.zenoss under the directory where your Zenoss product is installed. 
 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   
30 -class JobsRouter(DirectRouter):
31 """ 32 A JSON/Ext.Direct interface to operations on jobs 33 """
34 - def __init__(self, context, request):
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 # if user isn't global only show them the jobs they created 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
52 - def abort(self, jobids):
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
59 - def deleteJobs(self, jobids):
60 for id_ in jobids: 61 try: 62 self.api.deleteJob(id_) 63 except NoSuchJobException: 64 log.debug("Unable to delete job: %s No such job found.", id_)
65
66 - def getInfo(self, jobid):
67 job = self.api.getInfo(jobid) 68 return DirectResponse.succeed(data=Zuul.marshal(job))
69
70 - def detail(self, jobid):
71 try: 72 logfile, content, maxLimit = self.api.getJobDetail(jobid) 73 except NoSuchJobException: 74 # Probably a detail request overlapped a delete request. Just 75 # return None. 76 logfile, content, maxLimit = None, None, None 77 return {'content': content, 'logfile': logfile, 'maxLimit' : maxLimit}
78
79 - def userjobs(self):
80 results = defaultdict(list) 81 totals = {} 82 validstates = {'STARTED':'started', 'SUCCESS':'finished', 83 'PENDING':'scheduled'} 84 for job in self.api.getUserJobs(): 85 if job.status in validstates: 86 results[job.status].append(job) 87 # Sort and slice appropriately -- most recent 10 items 88 for status, jobs in results.iteritems(): 89 jobs.sort(key=lambda j:getattr(j, validstates[status]), 90 reverse=True) 91 totals[status] = len(jobs) 92 results[status] = jobs[:10] 93 jobs = Zuul.marshal(results, keys=JOBKEYS) 94 for joblist in jobs.itervalues(): 95 for job in joblist: 96 job['description'] = cgi.escape(job['description']) 97 return DirectResponse(jobs=jobs, totals=totals)
98