def run(self): logging.debug('[JOB: %d] Set startdate and status as %s', self.jobid, cqStatus.RUNNING) self.update({'status': cqStatus.RUNNING, 'startdate': cqUtils.ts2ftime()}) releaseLock(self.lockfile) #self.plugin.onJobStart(self.jobid, self.cmd) # not supported on Windows args = shlex.split(self.cmd) try: logging.debug('[JOB: %d] Try to run job: %s', self.jobid, self.cmd) job = Popen(args) logging.debug('[JOB: %d] Try to save PID(%d) into database', self.jobid, job.pid) self.update({'pid': job.pid}) logging.debug('[JOB: %d] Wait for job to finish', self.jobid) job.wait() if job.returncode == 0: logging.debug('[JOB: %d] Job finished normally, try to update the status and finishdate', self.jobid) self.update({'status': cqStatus.COMPLETE, 'finishdate': cqUtils.ts2ftime()}) else: logging.debug('[JOB: %d] Job ended unexpectedly, try to update the status and finishdate', self.jobid) self.update({'status': cqStatus.KILLED, 'finishdate': cqUtils.ts2ftime()}) #self.plugin.onJobEnd(self.jobid, self.cmd, job.returncode) except OSError, e: logging.debug('[JOB: %d] Job failed to start, try to update the status and finishdate', self.jobid) #self.plugin.onJobEnd(self.jobid, self.cmd, -99) self.update({'status': cqStatus.ERROR, 'finishdate': cqUtils.ts2ftime()})
def insertWorker(self, pid, paused=0, startdate=None): if startdate == None: startdate = cqUtils.ts2ftime() return self.execute(''' INSERT INTO worker (pid, paused, startdate) VALUES ('%d', '%d', '%s') ''' % (pid, paused, startdate))
def killJobByID(self, jid): jobs = self.db.getJobs("rowid=%d AND status='%s'" % (jid, cqStatus.RUNNING)) if len(jobs) > 0: try: os.kill(jobs[0][3], signal.SIGTERM) except OSError, e: pass self.db.updateJob(jid, {'status': cqStatus.KILLED, 'finishdate': cqUtils.ts2ftime()}) plugin.onJobKill(jid, jobs[0][3], jobs[0][1], jobs[0][2]) print '! Job %d was killed at PID: %d' % (jid, jobs[0][3])
def insertJob(self, job): djob = { 'submitdate': cqUtils.ts2ftime(), 'status': cqStatus.PENDING, 'priority': 0 } djob.update(job); return self.execute(''' INSERT INTO jobs (name, cmd, submitdate, status, priority) VALUES ('%s', '%s', '%s', '%s', '%s') ''' % (djob['name'], djob['cmd'], djob['submitdate'], djob['status'], djob['priority']))
parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter, description='A command queue system, avaible for *nix, Windows, and maybe OSX') parser.add_argument('--config', help='The config file or the config string, default: %(default)s.\n\ If config file path contains no directory separator, script directory will be used.\n\ A config string: db=test.db&workercount=2&interval=60&logfile=cmdQueue.log&loglevel=WARNING\n\ In config file:\n[cmdQueue]\ndb=test.db\nworkercount=2\ninterval=60\nlogfile=cmdQueue.log', default='cmdQueue.conf') parser.add_argument('-v', '--version', action='version', version='%(prog)s ' + __version__) subparsers = parser.add_subparsers(title='Subcommands', help='<subcommand> -h', dest='command') parser_add = subparsers.add_parser('add', help='Add a command into the queue') parser_add.add_argument('-n', '--name', required=True) parser_add.add_argument('-c', '--cmd', required=True) parser_add.add_argument('-p', '--priority', required=True) parser_list = subparsers.add_parser('list', help='List the workers and jobs') parser_list.add_argument('-f', '--frm', help='From which date to show the jobs, default: %(default)s', default=cqUtils.ts2ftime(1)) parser_list.add_argument('-t', '--to', help='To which date to show the jobs, default: now(%(default)s)', default=cqUtils.ts2ftime()) parser_list.add_argument('-n', '--number', type=int, help='Limited number of jobs to show, default: %(default)s', default=10) parser_list.add_argument('-s', '--status', help='Show jobs with the status (ALL|'+cqStatus.PENDING+'|'+cqStatus.RUNNING+'|'+cqStatus.KILLED+'|'+cqStatus.COMPLETE+'|'+cqStatus.ERROR+', default: %(default)s', default='ALL') parser_listworkers = subparsers.add_parser('listworkers', help='List the workers') parser_listjobs = subparsers.add_parser('listjobs', help='List the jobs') parser_listjobs.add_argument('-f', '--frm', help='From which date to show the jobs, default: %(default)s', default=cqUtils.ts2ftime(1)) parser_listjobs.add_argument('-t', '--to', help='To which date to show the jobs, default: now(%(default)s)', default=cqUtils.ts2ftime()) parser_listjobs.add_argument('-n', '--number', type=int, help='Limited number of jobs to show, default: %(default)s', default=10) parser_listjobs.add_argument('-s', '--status', help='Show jobs with the status (ALL|'+cqStatus.PENDING+'|'+cqStatus.RUNNING+'|'+cqStatus.KILLED+'|'+cqStatus.COMPLETE+'|'+cqStatus.ERROR+', default: %(default)s', default='ALL') parser_reset = subparsers.add_parser('reset', help='Reset the queue, kill all workers and jobs, and remove them from database') parser_reset = subparsers.add_parser('resetjobs', help='Reset all jobs to '+cqStatus.PENDING+' status')