def test_modify_job(self): headers = {'Content-Type': 'application/json; charset=UTF-8'} data = { 'job_class_string': 'hello.world', 'name': 'hello world job', 'minute': '*/5' } response = self.fetch(self.JOBS_URL, method='POST', headers=headers, body=json.dumps(data)) return_info = json.loads(response.body.decode()) self.assertTrue('job_id' in return_info) self.assertEqual(len(return_info['job_id']), 32) job = self.scheduler.get_job(return_info['job_id']) self.assertEqual(utils.get_job_name(job), data['job_class_string']) self.assertEqual(job.name, data['name']) headers = {'Content-Type': 'application/json; charset=UTF-8'} data = { 'job_class_string': 'hello.world!!!!', 'name': 'hello world job~~~~', 'minute': '*/20' } response = self.fetch(self.JOBS_URL + '/' + return_info['job_id'] + '?sync=true', method='PUT', headers=headers, body=json.dumps(data)) self.assertEqual(response.code, 200) job = self.scheduler.get_job(return_info['job_id']) self.assertEqual(utils.get_job_name(job), data['job_class_string']) self.assertEqual(job.name, data['name'])
def _run_job(self, job_id): """Kicks off a job. :param str job_id: Job id. :return: A dictionary with the only field of execution_id. :rtype: dict """ job = self.scheduler_manager.get_job(job_id) if not job: self.set_status(400) return {'error': 'Job not found: %s' % job_id} job_name = utils.get_job_name(job) args = utils.get_job_args(job) kwargs = job.kwargs scheduler = utils.import_from_path(settings.SCHEDULER_CLASS) execution_id = scheduler.run_job(job_name, job_id, settings.DATABASE_CLASS, self.datastore.db_config, self.datastore.table_names, *args, **kwargs) # Audit log self.datastore.add_audit_log(job_id, job.name, constants.AUDIT_LOG_CUSTOM_RUN, user=self.username, description=execution_id) response = {'execution_id': execution_id} return response
def _build_execution(self, row): """Return job execution info from a row of scheduler_execution table. :param obj row: A row instance of scheduler_execution table. :return: A dictionary of job execution info. :rtype: dict """ return_json = { 'execution_id': row.eid, 'state': constants.EXECUTION_STATUS_DICT[row.state], 'hostname': row.hostname, 'pid': row.pid, 'task_id': row.task_id, 'description': row.description, 'result': row.result, 'scheduled_time': self.get_time_isoformat_from_db(row.scheduled_time), 'updated_time': self.get_time_isoformat_from_db(row.updated_time) } job = self.lookup_job(row.job_id) if job: return_json['job'] = { 'job_id': job.id, 'name': job.name, 'task_name': utils.get_job_name(job), 'pub_args': utils.get_job_args(job) } return_json['job'].update(utils.get_cron_strings(job)) return return_json
def _build_job_dict(self, job): """Transforms apscheduler's job structure to a python dictionary. :param Job job: An apscheduler.job.Job instance. :return: dictionary for job info :rtype: dict """ if job.next_run_time: next_run_time = job.next_run_time.isoformat() else: next_run_time = '' return_dict = { 'job_id': job.id, 'name': job.name, 'next_run_time': next_run_time, 'job_class_string': utils.get_job_name(job), 'pub_args': utils.get_job_args(job) } return_dict.update(utils.get_cron_strings(job)) return return_dict
def _build_execution(self, row): """Return job execution info from a row of scheduler_execution table. :param obj row: A row instance of scheduler_execution table. :return: A dictionary of job execution info. :rtype: dict """ return_json = { 'execution_id': row.eid, 'state': constants.EXECUTION_STATUS_DICT[row.state], 'hostname': row.hostname, 'pid': row.pid, 'task_id': row.task_id, 'description': row.description, 'result': row.result, 'scheduled_time': self.get_time_isoformat_from_db(row.scheduled_time), 'updated_time': self.get_time_isoformat_from_db(row.updated_time) } job = self.lookup_job(row.job_id) if job: return_json['job'] = { 'job_id': job.id, 'name': job.name, 'task_name': utils.get_job_name(job), 'pub_args': utils.get_job_args(job) } if isinstance(job.trigger, apscheduler.triggers.cron.CronTrigger): return_json.update(utils.get_cron_strings(job)) return_json["trigger_type"] = "cron" elif isinstance(job.trigger, apscheduler.triggers.interval.IntervalTrigger): return_json["interval"] = job.trigger.interval.total_seconds() return_json["trigger_type"] = "interval" else: return_json["trigger_type"] = "unknown" return return_json