Exemple #1
0
def run_task(task, queue_uuid, task_uuid, conf, exe):
    # 开始正式执行
    ret_code, ret_msg = get_time.compare_timestamps(
        task.trigger_time, task.task_earliest, task.task_latest
    )
    if ret_code == 2:
        logging.info('Start Sleeping {0} Seconds'.format(ret_msg))
        time.sleep(ret_msg)
    status_code = TaskStatus.Running
    status_msg = MSG_DICT.get(status_code)
    result = TaskResult(queue_uuid=queue_uuid, task_uuid=task_uuid, status_code=status_code,
                        status_msg=status_msg, session=task.session)
    send(result)
    mod = task.task_info["mod"]
    if isinstance(mod, dict):
        # 一个任务
        task_result = exe.run(mod)
        dump_result(result.task_uuid, task_result)
        if len(task_result.lines) > 30:
            task_result.data = None
            task_result.lines = task_result.lines[-30:]
        ret_code = task_result.return_code
        if ret_code != 0:
            status_code = TaskStatus.Failed
            status_msg = u"单任务执行失败"
        else:
            status_code = TaskStatus.Success
            status_msg = u"单任务执行成功"
        result = TaskResult(queue_uuid=queue_uuid, task_uuid=task_uuid, status_code=status_code,
                            status_msg=status_msg, session=task.session, task_result=task_result)
        send(result)
        return 0
    elif isinstance(mod, list):
        # 多个任务
        for each in mod:
            task_result = exe.run(each)
            dump_result(result.task_uuid, task_result)
            if len(task_result.lines) > 30:
                task_result.data = None
                task_result.lines = task_result.lines[-30:]
            ret_code = task_result.return_code
            if ret_code != 0:
                status_code = TaskStatus.Failed
                status_msg = u"多任务执行失败"
                break
            else:
                status_code = TaskStatus.Success
                status_msg = u"多任务执行成功"
        result = TaskResult(queue_uuid=queue_uuid, task_uuid=task_uuid, status_code=status_code,
                            status_msg=status_msg, session=task.session, task_result=task_result)
        send(result)
        return 0
    else:
        # 异常情况 直接返回 - 1
        status_code = TaskStatus.UnKnown
        status_msg = MSG_DICT.get(status_code)
        result = TaskResult(queue_uuid=queue_uuid, task_uuid=task_uuid, status_code=status_code,
                            status_msg=status_msg, session=task.session)
        send(result)
        return -1
Exemple #2
0
def init_status(task, queue_uuid, task_uuid):
    if not worker_pool.vacant():
        # 进程池满
        logging.warning('TaskUUID: {0}, TaskStatus: {1}'.format(task_uuid, TaskStatus.WorkerWaiting.value))
        status_code = TaskStatus.WorkerWaiting
        status_msg = MSG_DICT.get(status_code)
        result = TaskResult(queue_uuid=queue_uuid, task_uuid=task_uuid, status_code=status_code,
                            status_msg=status_msg, session=task.session)
        send(result)
        return -1
    else:
        ret_code, ret_msg = get_time \
            .compare_timestamps(task.trigger_time, task.task_earliest, task.task_latest)
        if ret_code == 3:
            # 无法执行 直接返回-1
            logging.warning('TaskUUID: {0}, TaskStatus: {1}'.format(task_uuid, TaskStatus.TimeRangeExcept.value))
            status_code = TaskStatus.TimeRangeExcept
            status_msg = MSG_DICT.get(status_code)
            result = TaskResult(queue_uuid=queue_uuid, task_uuid=task_uuid, status_code=status_code,
                                status_msg=status_msg, session=task.session)
            send(result)
            return -1
        elif ret_code == 2:
            # 需要等待
            logging.info('TaskUUID: {0}, TaskStatus: {1}'.format(task_uuid, TaskStatus.WorkerWaiting.value))
            status_code = TaskStatus.TriggerTimeWaiting
            status_msg = u'需要等待{0}秒'.format(ret_msg)
            result = TaskResult(queue_uuid=queue_uuid, task_uuid=task_uuid, status_code=status_code,
                                status_msg=status_msg, session=task.session)
            send(result)
        elif ret_code == 1:
            # 该状态不通知Master
            # 可以执行
            # logging.info('TaskUUID: {0}, TaskStatus: {1}'.format(task_uuid, TaskStatus.Runnable.value))
            # status_code = TaskStatus.Runnable
            # status_msg = MSG_DICT.get(status_code)
            # result = TaskResult(queue_uuid=queue_uuid, task_uuid=task_uuid, status_code=status_code,
            #                     status_msg=status_msg, session=task.session)
            # send(result)
            pass
        else:
            # 异常情况 直接返回-1
            logging.warning('TaskUUID: {0}, TaskStatus: {1}'.format(task_uuid, TaskStatus.UnKnown.value))
            status_code = TaskStatus.UnKnown
            status_msg = MSG_DICT.get(status_code)
            result = TaskResult(queue_uuid=queue_uuid, task_uuid=task_uuid, status_code=status_code,
                                status_msg=status_msg, session=task.session)
            send(result)
            return -1
Exemple #3
0
def init_exe(task, queue_uuid, task_uuid, conf):
    try:
        exe = Executor.CreateByWorker(conf)
        return exe
    except (SSHNoValidConnectionsError, SSHAuthenticationException, SSHException), e:
        # SSH连接失败 直接返回-1
        status_code, status_msg = TaskStatus.InitFailed, e.message
        result = TaskResult(queue_uuid=queue_uuid, task_uuid=task_uuid, status_code=status_code,
                            status_msg=status_msg, session=task.session)
        send(result)
        return -1
Exemple #4
0
def init_conf(task, queue_uuid, task_uuid):
    try:
        conf = SSHConfig(**task.task_info["remote"]["params"])
        return conf
    except ConfigInvalid, e:
        # 配置文件格式错误 直接返回-1
        logging.warning('TaskUUID: {0}, TaskStatus: {1}'.format(task_uuid, TaskStatus.InitFailed.value))
        status_code, status_msg = TaskStatus.InitFailed, e.message
        result = TaskResult(queue_uuid=queue_uuid, task_uuid=task_uuid, status_code=status_code,
                            status_msg=status_msg, session=task.session)
        send(result)
        return -1
Exemple #5
0
 def from_dict(cache, dict_data):
     queue = TaskQueue(cache,
                       dict_data['queue_uuid'],
                       trigger_time=dict_data['trigger_time'],
                       sync_group=dict_data['sync_group'])
     queue.queue_status = QueueStatus(dict_data['queue_status'])
     queue.run_all = dict_data['run_all']
     for task_define in dict_data['task_list']:
         task = Task.from_dict(task_define)
         queue.task_list.append(task)
     for result_define in dict_data['task_result_list']:
         result = TaskResult.from_dict(result_define)
         queue.task_result_list.append(result)
     queue.make_todo_task_queue(force=True)
     return queue
 def event_relay(self, event):
     """
     任务分发器,由外部callback触发
     """
     if event.Name == EventName.TaskDispatch:
         worker_name = self.worker_arb()
         task = event.Data
         self.send_task(worker_name, task)
         logging.info('Task[{}] assigned to worker[{}]'.format(
             event.Data.task_uuid, worker_name))
         self.send_result(
             TaskResult(task.queue_uuid, task.task_uuid,
                        TaskStatus.Dispatched,
                        MSG_DICT[TaskStatus.Dispatched], task.session))
     else:
         logging.warning('Invalid event[{}] routed'.format(event.Name))
Exemple #7
0
 def process(data):
     try:
         task_result = TaskResult.deserial(data)
         logging.info(u'Server Receive : {0}'.format(
             json.dumps(task_result.to_dict(), ensure_ascii=False)))
         task_status = task_result.status_code
         if task_status.IsExcepted:
             msg_queue.put_event('except', task_result)
         if task_status.IsInited:
             msg_queue.put_event('init', task_result)
         if task_status.IsRunning:
             msg_queue.put_event('start', task_result)
         if task_status.IsDone:
             msg_queue.put_event('end', task_result)
         return 0
     except DeserialError:
         logging.error('TaskResult Deserialize Error')
         return -1
Exemple #8
0
 def skip_task(self, session=None):
     """
     跳过下一项任务
     """
     if self.todo_task_queue.empty():
         return False
     next_task = self.get()
     if isinstance(next_task, Task):
         result = TaskResult(next_task.queue_uuid, next_task.task_uuid,
                             TaskStatus.Skipped,
                             MSG_DICT[TaskStatus.Skipped], session)
         self.task_result_list.append(result)
         # TaskQueueManager._notify_outside(result)
         if self.todo_task_queue.empty():
             self.queue_status = QueueStatus.Done
         else:
             self.queue_status = QueueStatus.Normal
         return True
     else:
         logging.warning(next_task)
         raise Exception(u'任务定义不正确')