Exemplo n.º 1
0
 public void SetTask(ExecTaskInfo execTask)
 {
     lock (_locklist)
     {
         var tempTask = _listTask.FindIndex(x => x.Guid == execTask.Guid);
         if (tempTask > -1)
         {
             if (execTask.IsExec)
             {
                 _listTask[tempTask] = execTask;
             }
             else
             {
                 _listTask.RemoveAt(tempTask);
             }
         }
         else
         {
             if (execTask.IsExec)
             {
                 _listTask.Add(execTask);
             }
         }
     }
 }
Exemplo n.º 2
0
        private int TaskManager_OnTaskExecBefore(ExecTaskInfo task)
        {
            int logId = 0;

            try
            {
                Ts_ExecLog tsLog = new Ts_ExecLog();
                tsLog.ExecParams     = task.Params;
                tsLog.ExecStatrtTime = task.LastExecTime;
                tsLog.ExecUrl        = task.ExecUrl;
                tsLog.TaskGuid       = task.Guid;

                logId = (int)_ormExecLog.Add(tsLog);
            }
            catch (Exception ex)
            {
                log.ErrorAndEmail(string.Format("保存执行日志结果异常TaskManager_OnTaskExecBefore,参数:{0}", task.ToJson()), ex);
            }
            try
            {
                Ts_TaskExec taskExec = new Ts_TaskExec();
                taskExec.LastExecId   = logId;
                taskExec.LastExecTime = task.LastExecTime;
                taskExec.TaskGuid     = task.Guid;

                _ormTaskExec.Update(taskExec);
            }
            catch (Exception ex) {
                log.ErrorAndEmail(string.Format("修改最后一次执行时间异常TaskManager_OnTaskExecBefore,参数:{0}", task.ToJson()), ex);
            }
            return(logId);
        }
Exemplo n.º 3
0
 public bool RemoveTask(ExecTaskInfo execTask)
 {
     lock (_locklist)
     {
         var tempTask = _listTask.FindIndex(x => x.Guid == execTask.Guid);
         if (tempTask > -1)
         {
             _listTask.RemoveAt(tempTask);
             return(true);
         }
         else
         {
             return(false);
         }
     }
 }
Exemplo n.º 4
0
        public bool RunTask(string TaskGuid)
        {
            var task = _ormTasks.Find(w => w.Guid == TaskGuid);

            if (task == null)
            {
                throw new BOException("找不到任务ID");
            }
            var taskinfo    = new ExecTaskInfo(task, GetTaskExecByGuid(task.Guid));
            var taskManager = TasksManage.GetInstance();

            taskManager.OnTaskExecAfter  += TaskManager_OnTaskExecAfter;
            taskManager.OnTaskExecBefore += TaskManager_OnTaskExecBefore;
            taskManager.RunTask(taskinfo);
            return(true);
        }
Exemplo n.º 5
0
        /// <summary>
        /// 执行一个任务
        /// </summary>
        /// <param name="taskInfo"></param>
        /// <returns></returns>
        public Task <TaskExecResult> RunTask(ExecTaskInfo taskInfo)
        {
            Task <TaskExecResult> taskRun = Task.Run <TaskExecResult>(() =>
            {
                taskInfo.LastExecTime = DateTime.Now;
                int execLogId         = 0;
                if (OnTaskExecBefore != null)
                {
                    execLogId = OnTaskExecBefore(taskInfo);
                }

                TaskExecLog taskExecLog = new TaskExecLog()
                {
                    ExecGuid       = taskInfo.Guid,
                    ExecLogId      = execLogId,
                    IsErrorAlert   = taskInfo.IsErrorAlert,
                    Title          = taskInfo.Title,
                    ReceiveEmail   = taskInfo.ReceiveEmail,
                    ExecStatrtTime = taskInfo.LastExecTime
                };

                TaskExecResult execResult = new TaskExecResult();
                string rsp = null;
                if (taskInfo.ExecType == (int)ExecTypeEnum.HTTP)
                {
                    rsp = Net.HttpRequest(taskInfo.ExecUrl, taskInfo.Params, taskInfo.ExecMethod, taskInfo.Timeout, taskInfo.Encoding, taskInfo.IsResponseNorm || taskInfo.IsLogResult);
                    taskExecLog.ExecEndTime = DateTime.Now;

                    if (rsp.StartsWith(":("))
                    {
                        execResult.Code = -999;
                        execResult.Msg  = string.Format("任务:{0}, 调用异常 url:{1} ", taskInfo.Title, taskInfo.ExecUrl);
                        execResult.Data = rsp;
                        if (OnTaskExecAfter != null)
                        {
                            OnTaskExecAfter(taskExecLog, execResult);
                        }
                        return(execResult);
                    }
                    if (taskInfo.IsResponseNorm)
                    {
                        try
                        {
                            execResult = rsp.FromJson <TaskExecResult>();
                        }
                        catch (Exception ex)
                        {
                            execResult.Code = -900;
                            execResult.Msg  = string.Format("任务:{0},调用 url:{1} , 转换返回结果失败 结果:{1} ", taskInfo.Title, taskInfo.ExecUrl, rsp);
                            execResult.Data = ex;
                            if (OnTaskExecAfter != null)
                            {
                                OnTaskExecAfter(taskExecLog, execResult);
                            }
                            return(execResult);
                        }
                    }
                    else
                    {
                        if (taskInfo.IsLogResult)
                        {
                            execResult.Data = rsp;
                        }
                        execResult.Code = 0;
                        execResult.Msg  = string.Format("任务:{0},调用 url:{1} , 处理成功 ", taskInfo.Title, taskInfo.ExecUrl);
                    }
                }
                else if (taskInfo.ExecType == (int)ExecTypeEnum.EXE)
                {
                    if (ProcessUtil.StartProcess(taskInfo.ExecUrl, taskInfo.Params, taskInfo.Timeout, ref rsp, taskInfo.IsLogResult))
                    {
                        taskExecLog.ExecEndTime = DateTime.Now;
                        execResult.Code         = 0;
                        if (taskInfo.IsLogResult)
                        {
                            execResult.Data = rsp;
                        }
                        execResult.Msg = string.Format("任务:{0},调用 url:{1} , 处理成功 ", taskInfo.Title, taskInfo.ExecUrl);
                    }
                    else
                    {
                        taskExecLog.ExecEndTime = DateTime.Now;
                        execResult.Code         = -800;
                        execResult.Msg          = string.Format("任务:{0},调用 url:{1} , 转换返回结果失败 结果:{1} ", taskInfo.Title, taskInfo.ExecUrl, rsp);
                    }
                }
                else
                {
                    execResult.Code = -1;
                    execResult.Msg  = "执行类型不存在";
                }
                if (OnTaskExecAfter != null)
                {
                    OnTaskExecAfter(taskExecLog, execResult);
                }
                return(execResult);
            });

            return(taskRun);
        }