Пример #1
0
 def _handle_event(self, event):
     close_old_connections()
     obj = SimpleLazyObject(lambda: Task.objects.filter(pk=event.job_id).first())
     if event.code == EVENT_SCHEDULER_SHUTDOWN:
         logger.info(f'EVENT_SCHEDULER_SHUTDOWN: {event}')
         Notify.make_notify('schedule', '1', '调度器已关闭', '调度器意外关闭,你可以在github上提交issue')
     elif event.code == EVENT_JOB_MAX_INSTANCES:
         logger.info(f'EVENT_JOB_MAX_INSTANCES: {event}')
         send_fail_notify(obj, '达到调度实例上限,一般为上个周期的执行任务还未结束,请增加调度间隔或减少任务执行耗时')
     elif event.code == EVENT_JOB_ERROR:
         logger.info(f'EVENT_JOB_ERROR: job_id {event.job_id} exception: {event.exception}')
         send_fail_notify(obj, f'执行异常:{event.exception}')
     elif event.code == EVENT_JOB_EXECUTED:
         if event.retval:
             score = 0
             for item in event.retval:
                 score += 1 if item[1] else 0
             history = History.objects.create(
                 task_id=event.job_id,
                 status=2 if score == len(event.retval) else 1 if score else 0,
                 run_time=human_datetime(event.scheduled_run_time),
                 output=json.dumps(event.retval)
             )
             Task.objects.filter(pk=event.job_id).update(latest=history)
             if score != 0:
                 send_fail_notify(obj)
Пример #2
0
def schedule_worker_handler(job):
    history_id, host_id, command = json.loads(job)
    if host_id == 'local':
        code, duration, out = local_executor(command)
    else:
        host = Host.objects.filter(pk=host_id).first()
        if not host:
            code, duration, out = 1, 0, f'unknown host id for {host_id!r}'
        else:
            code, duration, out = host_executor(host, command)

    close_old_connections()
    with transaction.atomic():
        history = History.objects.select_for_update().get(pk=history_id)
        output = json.loads(history.output)
        output[str(host_id)] = [code, duration, out]
        history.output = json.dumps(output)
        if all(output.values()):
            history.status = '1' if sum(x[0]
                                        for x in output.values()) == 0 else '2'
        history.save()
    if history.status == '2':
        task = Task.objects.get(pk=history.task_id)
        send_fail_notify(task)