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 # Make sure they have permission to delete. 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
75 - def getInfo(self, jobid):
76 job = self.api.getInfo(jobid) 77 return DirectResponse.succeed(data=Zuul.marshal(job))
78
79 - def detail(self, jobid):
80 try: 81 logfile, content, maxLimit = self.api.getJobDetail(jobid) 82 except NoSuchJobException: 83 # Probably a detail request overlapped a delete request. Just 84 # return None. 85 logfile, content, maxLimit = None, None, None 86 return {'content': content, 'logfile': logfile, 'maxLimit' : maxLimit}
87
88 - def userjobs(self):
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 # Sort and slice appropriately -- most recent 10 items 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