def add_job(): """Adds a new job.""" try: from apscheduler.jobconf import JobConf conf = JobConf() conf.id = 1 conf.cmd = 'date +%s; echo ----------------------------------------' conf.name = 'job1' conf.status = 1 #from apscheduler.triggers.interval import IntervalTrigger # current_app.swrapper.scheduler.add_job(cmdfunc, 'interval', seconds=100, id='tick1', name='tick1 x', replace_existing=True, conf=conf) from apscheduler.triggers.cron import CronTrigger trigger = CronTrigger(second='*/5') current_app.swrapper.scheduler.add_job(cmdfunc, trigger, second=5, id=conf.id, name=conf.name, replace_existing=True, conf=conf, jobstore=current_app.swrapper.jobstore_alias) # data = request.get_json(force=True) # if data: # job = current_app.swrapper.scheduler.add_job(**data) # return __jsonify(job.conf.to_dict()) # else: # return __jsonify({'message': 'data is empty', 'code':0}) return __jsonify({'message': 'add done', 'code':0}) except ConflictingIdError: return __jsonify(dict(error_message='Job %s already exists.' % conf.id), status=409) except LookupError as e: return __jsonify(dict(error_message=str(e)), status=400) except Exception as e: return __jsonify(dict(error_message=str(e)), status=500)
def _get_jobs(self, *conditions): jobs = [] selectable = select([x for x in self.wm_jobs_t.c]) # selectable = selectable.order_by(self.wm_jobs_t.c.next_run_time) selectable = selectable.where(*conditions).where(self.wm_jobs_t.c.status == 1) if conditions else selectable failed_job_ids = set() for row in self.engine.execute(selectable): try: jobs.append(self._reconstitute_job(row)) except: logging.exception('Unable to restore job "%s" -- removing it', row.id) failed_job_ids.add(row.id) # Remove all the jobs we failed to restore if failed_job_ids: # delete = self.jobs_t.delete().where(self.jobs_t.c.id.in_(failed_job_ids)) # logic delete msg = 'job %s update status to 2 cause of failing to _reconstitute_job' % ','.join(list(failed_job_ids)) logging.error(msg) update = self.wm_jobs_t.update().where(self.wm_jobs_t.c.id.in_(failed_job_ids)).values(status='2') self.engine.execute(update) # TODO ... add history here from apscheduler.history import add_log conf = JobConf() conf.id = 0 conf.cmd = ' ' add_log(conf, output=msg) return jobs
def _get_jobs(self, *conditions): jobs = [] selectable = select([x for x in self.wm_jobs_t.c]) # selectable = selectable.order_by(self.wm_jobs_t.c.next_run_time) selectable = selectable.where(*conditions).where( self.wm_jobs_t.c.status == 1) if conditions else selectable failed_job_ids = set() for row in self.engine.execute(selectable): try: jobs.append(self._reconstitute_job(row)) except: logging.exception('Unable to restore job "%s" -- removing it', row.id) failed_job_ids.add(row.id) # Remove all the jobs we failed to restore if failed_job_ids: # delete = self.jobs_t.delete().where(self.jobs_t.c.id.in_(failed_job_ids)) # logic delete msg = 'job %s update status to 2 cause of failing to _reconstitute_job' % ','.join( list(failed_job_ids)) logging.error(msg) update = self.wm_jobs_t.update().where( self.wm_jobs_t.c.id.in_(failed_job_ids)).values(status='2') self.engine.execute(update) # TODO ... add history here from apscheduler.history import add_log conf = JobConf() conf.id = 0 conf.cmd = ' ' add_log(conf, output=msg) return jobs
def _reconstitute_job(self, row): ''' code gen by shell cmd: cat a | awk -F '=' '{print $1}' | cut -c5- | awk '{ print "job."$1" = row."$1}' what in file a is the wm_jobs_t create statement which can be found in the current source code file ''' conf = JobConf() conf.id = row.id conf.cmd = row.cmd conf.cron_str = row.cron_str conf.name = row.name conf.desc = row.desc conf.mails = row.mails conf.phones = row.phones conf.team = row.team conf.owner = row.owner conf.hosts = row.hosts conf.host_strategy = row.host_strategy conf.restore_strategy = row.restore_strategy conf.retry_strategy = row.retry_strategy conf.error_strategy = row.error_strategy conf.exist_strategy = row.exist_strategy conf.running_timeout_s = row.running_timeout_s conf.status = row.status conf.modify_time = row.modify_time conf.modify_user = row.modify_user conf.create_time = row.create_time conf.create_user = row.create_user conf.start_date = row.start_date conf.end_date = row.end_date conf.oupput_match_reg = row.oupput_match_reg conf.next_run_time = row.next_run_time job = Job.__new__(Job) job.conf = conf job.id = job.conf.id job._scheduler = self._scheduler job._jobstore_alias = self._alias job.trigger = self._create_trigger_by_conf(job) t = apscheduler.util.local_timestamp_to_datetime( conf.next_run_time) if conf.next_run_time > 0 else None t = apscheduler.util.convert_to_ware_datetime(t, get_localzone(), 'conf.next_run_time') state = { 'version': 1, 'conf': conf, 'id': conf.id, 'name': conf.name, 'next_run_time': t, } job.__setstate__(state) return job
def _reconstitute_job(self, row): ''' code gen by shell cmd: cat a | awk -F '=' '{print $1}' | cut -c5- | awk '{ print "job."$1" = row."$1}' what in file a is the wm_jobs_t create statement which can be found in the current source code file ''' conf = JobConf() conf.id = row.id conf.cmd = row.cmd conf.cron_str = row.cron_str conf.name = row.name conf.desc = row.desc conf.mails = row.mails conf.phones = row.phones conf.team = row.team conf.owner = row.owner conf.hosts = row.hosts conf.host_strategy = row.host_strategy conf.restore_strategy = row.restore_strategy conf.retry_strategy = row.retry_strategy conf.error_strategy = row.error_strategy conf.exist_strategy = row.exist_strategy conf.running_timeout_s = row.running_timeout_s conf.status = row.status conf.modify_time = row.modify_time conf.modify_user = row.modify_user conf.create_time = row.create_time conf.create_user = row.create_user conf.start_date = row.start_date conf.end_date = row.end_date conf.oupput_match_reg = row.oupput_match_reg conf.next_run_time = row.next_run_time job = Job.__new__(Job) job.conf = conf job.id = job.conf.id job._scheduler = self._scheduler job._jobstore_alias = self._alias job.trigger = self._create_trigger_by_conf(job) t = apscheduler.util.local_timestamp_to_datetime(conf.next_run_time) if conf.next_run_time > 0 else None t = apscheduler.util.convert_to_ware_datetime(t, get_localzone(), 'conf.next_run_time' ) state = { 'version': 1, 'conf': conf, 'id': conf.id, 'name': conf.name, 'next_run_time': t, } job.__setstate__(state) return job