def test_add_job(self): service = JobService(self.testdb) service.add_job('Test title', 'Test description') jobs = service.get_jobs() self.assertEqual(1, len(jobs)) self.assertEqual(jobs[0].title, 'Test title')
def _on_job_remove(self, event): from service.job_service import JobService job_id = event.job_id job = JobService.get_job(job_id) # 有自动删除的情况 if job.status != job.Status.STOPPED.value: JobService.change_job_status(job_id, job.Status.STOPPED) logger.info('定时任务%s被移除了调度器,信息:%s' % (job_id, str(job)))
def _on_job_add(self, event): from service.job_service import JobService job_id = event.job_id job = JobService.get_job(job_id) # 有自动加入的情况 if job.status != job.Status.RUNNING.value: JobService.change_job_status(job_id, job.Status.RUNNING) job.status = job.Status.RUNNING.value logger.info('定时任务%s加入了调度器,信息:%s' % (job_id, str(job)))
def _on_job_modify(self, event): from service.job_service import JobService job_id = event.job_id job = self._scheduler.get_job(job_id) if job.next_run_time is None: job = JobService.get_job(job_id) logger.info('定时任务%s已暂停,信息:%s' % (job_id, str(job))) if job.status != job.Status.SUSPENDED.value: JobService.change_job_status(job_id, job.Status.SUSPENDED) else: logger.info('定时任务%s被修改或重启了并正在运行中,信息:%s' % (job_id, str(job)))
def test_add_steps_to_job(self): service = JobService(self.testdb) service.add_job('Test title', 'Test description') jobs = service.get_jobs() job_id = jobs[0].id step1_parameter1 = TaskParameter(key='step1_key1', value='step1_value1') step1_parameter2 = TaskParameter(key='step1_key2', value='step1_value2') step1 = Step('Test step 1', job_id) step1.parameters = [step1_parameter1, step1_parameter2] step2_parameter1 = TaskParameter(key='step2_key1', value='step2_value1') step2_parameter2 = TaskParameter(key='step2_key2', value='step2_value2') step2_parameter3 = TaskParameter(key='step2_key3', value='step2_value3') step2 = Step('Test step 2', job_id) step2.parameters = [ step2_parameter1, step2_parameter2, step2_parameter3 ] service.add_steps(job_id, [step1, step2]) steps = service.get_steps(job_id) self.assertEqual(2, len(steps)) self.assertEqual('Test step 1', steps[0].title)
def test_queue_build(self): build_service = BuildService(self.testdb) job_service = JobService(self.testdb) job_service.add_job('Test job') job = job_service.get_jobs()[0] self.assertIsNotNone(job) build_service.queueBuild(job.id) build_service.queueBuild(job.id) builds = build_service.get_queue_builds(job.id) self.assertEqual(2, len(builds)) self.assertFalse(builds[0].id == builds[1].id)
def _on_job_error(self, event): from service.job_service import JobService e = event.exception job_id = event.job_id logger.warn('定时任务%s发生错误,将被移除调度器' % job_id) logger.error(str(e)) task = JobService.get_job(job_id) self.remove_job(task)
def pause_job(self, job_id): return JobService.pause_job(job_id)
def test_submit_job(): bytes = "#!/usr/bin/env python3\n# -*- coding: utf-8 -*-\ndef run():" config = {'name': 'hahah', 'job_id': 'jjjj', 'cron': 'ddd'} JobService.submit_job(bytes, config)
def test_pause_job(job_id): JobService.pause_job(job_id)
def test_modify_job(job_id): JobService.modify_job(job_id, {'name': 'test', 'cron': '{"seconds": 8}'})
def test_stop_job(job_id): JobService.stop_job(job_id)
def test_start_job(job_id): JobService.start_job(job_id)
def test_resume_scheduler(): JobService.resume_scheduler()
def pause_scheduler(self): return JobService.pause_scheduler()
def modify_job(self, job_id, config): return JobService.modify_job(job_id, config)
def resume_scheduler(self): return JobService.resume_scheduler()
def test_add_job_without_title(self): service = JobService(self.testdb) service.add_job(None) jobs = service.get_jobs() self.assertEqual(0, len(jobs))
def stop_job(self, job_id): return JobService.stop_job(job_id)
def status(self): return JobService.status()
def submit_job(self, file_bytes, config): return JobService.submit_job(file_bytes, config)
class JobBaseController(BaseController): job_service = JobService()
def test_stop_scheduler(): JobService.stop_scheduler()
def _on_job_execute(self, event): from service.job_service import JobService job_id = event.job_id logger.info('定时任务%s开始执行' % job_id) JobService.add_executed_times(job_id, 1)
def test_pause_scheduler(): JobService.pause_scheduler()
#!/usr/bin/env python3 # -*- coding: utf-8 -*- '执行入口' from common.log import logger from service.job_service import JobService from thrift_server import server __author__ = 'Jiateng Liang' if __name__ == '__main__': # 防止意外停止 JobService.stop_all_jobs() try: server.run() except Exception as ex: logger.error(ex)
def stop_scheduler(self): return JobService.stop_scheduler()