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   
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   
33 -class JobsRouter(DirectRouter):
34 """ 35 A JSON/Ext.Direct interface to operations on jobs 36 """
37 - def __init__(self, context, request):
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 # if user isn't global only show them the jobs they created 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
51 - def abort(self, jobids):
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
58 - def deleteJobs(self, jobids):
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
65 - def detail(self, jobid):
66 try: 67 logfile, content, maxLimit = self.api.getJobDetail(jobid) 68 except NoSuchJobException: 69 # Probably a detail request overlapped a delete request. Just 70 # return None. 71 logfile, content, maxLimit = None, None, None 72 return {'content': content, 'logfile': logfile, 'maxLimit' : maxLimit}
73
74 - def userjobs(self):
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 # Sort and slice appropriately -- most recent 10 items 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