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