Пример #1
0
        public void Execute()
        {
            DateTime now = DateTime.Now;

            this.Init();
            TaskBase[] taskBaseArray = this.DequeueTasks(DateTime.Now);
            TaskBase[] array         = taskBaseArray;
            for (int i = 0; i < array.Length; i++)
            {
                TaskBase taskBase = array[i];
                bool     flag     = !this.UpdateNextTime(taskBase.JobId, taskBase.ExecuteTime, taskBase.NextExecuteTime);
                if (flag)
                {
                    taskBase.TaskResult = TaskExecResult.Executed;
                }
            }
            AppEventLog.Debug("tasks:" + taskBaseArray.Length.ToString());
            TaskBase[] array2 = taskBaseArray;
            for (int j = 0; j < array2.Length; j++)
            {
                TaskBase task = array2[j];
                try
                {
                    bool flag2 = task.TaskResult == TaskExecResult.Executed;
                    if (flag2)
                    {
                        AppEventLog.Debug("TaskResult:true.");
                    }
                    bool flag3 = task.TaskResult != TaskExecResult.Executed;
                    if (flag3)
                    {
                        DateTime dateTime = this.AddZQ(now, task.RequiredExecuteTime, task.CycleOption.CycleUnit, task.CycleOption.CycleValue, task.CycleOption.CycleExecWeek, task.CycleOption.CycleExecMonth, task.CycleOption.CycleExecTime);
                        bool     flag4    = DateTime.Compare(dateTime, now) < 0;
                        if (flag4)
                        {
                            bool skipExecute = task.SkipExecute;
                            if (skipExecute)
                            {
                                task.TaskResult = TaskExecResult.Skip;
                                List <string> list   = task.LogInfoDetail;
                                string        format = "{0} C|跳过 {1} 作业计划已过期,跳过该次运行。";
                                string        str    = DateTime.Now.ToString();
                                string        str2   = task.RequiredExecuteTime.ToString();
                                string        str3   = string.Format(format, str, str2);
                                list.Add(str3);
                                while (DateTime.Compare(this.AddZQ(now, dateTime, task.CycleOption.CycleUnit, task.CycleOption.CycleValue, task.CycleOption.CycleExecWeek, task.CycleOption.CycleExecMonth, task.CycleOption.CycleExecTime), now) < 0)
                                {
                                    task.RequiredExecuteTime = dateTime;
                                    dateTime        = this.AddZQ(now, dateTime, task.CycleOption.CycleUnit, task.CycleOption.CycleValue, task.CycleOption.CycleExecWeek, task.CycleOption.CycleExecMonth, task.CycleOption.CycleExecTime);
                                    task.TaskResult = TaskExecResult.Skip;
                                    List <string> list2   = task.LogInfoDetail;
                                    string        format2 = "{0} C|跳过 {1} 作业计划已过期,跳过该次运行。";
                                    string        str4    = DateTime.Now.ToString();
                                    string        str5    = task.RequiredExecuteTime.ToString();
                                    string        str6    = string.Format(format2, str4, str5);
                                    list2.Add(str6);
                                }
                                JobLogUtils.WriteServiceLog(task);
                            }
                            else
                            {
                                task.NextExecuteTime = dateTime;
                                TimerJob timerJob = new TimerJob();
                                timerJob.TaskModule = task;
                                TimerManager.RunTask(timerJob, task.ExecUserId, AppConfig.DefaultDbId);
                                while (DateTime.Compare(this.AddZQ(now, dateTime, task.CycleOption.CycleUnit, task.CycleOption.CycleValue, task.CycleOption.CycleExecWeek, task.CycleOption.CycleExecMonth, task.CycleOption.CycleExecTime), now) < 0)
                                {
                                    task.RequiredExecuteTime = dateTime;
                                    dateTime             = this.AddZQ(now, dateTime, task.CycleOption.CycleUnit, task.CycleOption.CycleValue, task.CycleOption.CycleExecWeek, task.CycleOption.CycleExecMonth, task.CycleOption.CycleExecTime);
                                    task.NextExecuteTime = dateTime;
                                    timerJob.TaskModule  = task;
                                    TimerManager.RunTask(timerJob, task.ExecUserId, AppConfig.DefaultDbId);
                                }
                                bool flag5 = task.TaskResult != TaskExecResult.Failure && task.TaskResult != TaskExecResult.Exception;
                                if (flag5)
                                {
                                    task.TaskResult = TaskExecResult.Success;
                                }
                                JobLogUtils.WriteServiceLog(task);
                            }
                            bool flag6 = DateTime.Compare(dateTime, now) < 0;
                            if (flag6)
                            {
                                task.RequiredExecuteTime = dateTime;
                                task.NextExecuteTime     = this.AddZQ(now, dateTime, task.CycleOption.CycleUnit, task.CycleOption.CycleValue, task.CycleOption.CycleExecWeek, task.CycleOption.CycleExecMonth, task.CycleOption.CycleExecTime);
                                TimerManager.RunTask(new TimerJob
                                {
                                    TaskModule = task
                                }, task.ExecUserId, AppConfig.DefaultDbId);
                                bool flag7 = task.TaskResult != TaskExecResult.Failure && task.TaskResult != TaskExecResult.Exception;
                                if (flag7)
                                {
                                    task.TaskResult = TaskExecResult.Success;
                                }
                                JobLogUtils.WriteServiceLog(task);
                            }
                        }
                        else
                        {
                            TimerManager.RunTask(new TimerJob
                            {
                                TaskModule = task
                            }, task.ExecUserId, AppConfig.DefaultDbId);
                            bool flag8 = task.TaskResult != TaskExecResult.Failure && task.TaskResult != TaskExecResult.Exception;
                            if (flag8)
                            {
                                task.TaskResult = TaskExecResult.Success;
                            }
                            JobLogUtils.WriteServiceLog(task);
                        }
                    }
                }
                catch (Exception ex_4E3)
                {
                    task.TaskResult = TaskExecResult.Exception;
                    JobLogUtils.WriteServiceLog(task);
                }
            }
        }
Пример #2
0
        public static void WriteServiceLog(TaskBase task)
        {
            Database database = LogicContext.GetDatabase();

            try
            {
                database.BeginTrans();
                HSQL sql = new HSQL(database);
                sql.Add("insert into JOBLOG(JOBLOG_JOBID, JOBLOG_LOGNO, JOBLOG_LOGTIME, JOBLOG_OPERTYPE, JOBLOG_EXECRESULT, JOBLOG_DESC, JOBLOG_DETAIL)");
                sql.Add("values(:JOBLOG_JOBID, :JOBLOG_LOGNO, :JOBLOG_LOGTIME, :JOBLOG_OPERTYPE, :JOBLOG_EXECRESULT, :JOBLOG_DESC, :JOBLOG_DETAIL)");
                sql.ParamByName("JOBLOG_JOBID").Value    = task.JobId;
                sql.ParamByName("JOBLOG_LOGNO").Value    = TimIdUtils.GenUtoId("JOBLOGNO");
                sql.ParamByName("JOBLOG_LOGTIME").Value  = AppRuntime.ServerDateTime;
                sql.ParamByName("JOBLOG_OPERTYPE").Value = "E";
                sql.ParamByName("JOBLOG_DETAIL").Value   = string.Empty;
                StringBuilder stringBuilder = new StringBuilder();
                foreach (string str in task.LogInfoDetail)
                {
                    stringBuilder.AppendLine(str);
                }
                string str2 = stringBuilder.ToString();
                string str3 = string.Empty;
                bool   flag = task.TaskResult == TaskExecResult.Success;
                if (flag)
                {
                    str3 = string.Format("{0} 作业计划执行成功。", task.RequiredExecuteTime.ToString());
                    bool flag2 = string.IsNullOrEmpty(str2);
                    if (flag2)
                    {
                        str2 = str3;
                    }
                    sql.ParamByName("JOBLOG_EXECRESULT").Value = "A";
                    sql.ParamByName("JOBLOG_DESC").Value       = str3;
                    sql.AddParam("JOBLOG_DETAIL", TimDbType.Blob, 0, str2);
                }
                else
                {
                    bool flag3 = task.TaskResult == TaskExecResult.Failure;
                    if (flag3)
                    {
                        str3 = string.Format("{0} 作业计划执行失败。", task.RequiredExecuteTime.ToString());
                        bool flag4 = string.IsNullOrEmpty(str2);
                        if (flag4)
                        {
                            str2 = str3;
                        }
                        sql.ParamByName("JOBLOG_EXECRESULT").Value = "B";
                        sql.ParamByName("JOBLOG_DESC").Value       = str3;
                        sql.AddParam("JOBLOG_DETAIL", TimDbType.Blob, 0, str2);
                        JobLogUtils.SendErrorMsgToMail(task);
                    }
                    else
                    {
                        bool flag5 = task.TaskResult == TaskExecResult.Skip;
                        if (flag5)
                        {
                            str3 = "部分作业计划已过期,跳过运行。";
                            bool flag6 = string.IsNullOrEmpty(str2);
                            if (flag6)
                            {
                                str2 = str3;
                            }
                            sql.ParamByName("JOBLOG_EXECRESULT").Value = "C";
                            sql.ParamByName("JOBLOG_DESC").Value       = str3;
                            sql.AddParam("JOBLOG_DETAIL", TimDbType.Blob, 0, str2);
                        }
                        else
                        {
                            bool flag7 = task.TaskResult == TaskExecResult.Exception;
                            if (flag7)
                            {
                                str3 = string.Format("{0} 作业计划执行错误。", task.RequiredExecuteTime.ToString());
                                bool flag8 = string.IsNullOrEmpty(str2);
                                if (flag8)
                                {
                                    str2 = str3;
                                }
                                sql.ParamByName("JOBLOG_EXECRESULT").Value = "D";
                                sql.ParamByName("JOBLOG_DESC").Value       = str3;
                                sql.AddParam("JOBLOG_DETAIL", TimDbType.Blob, 0, str2);
                                JobLogUtils.SendErrorMsgToMail(task);
                            }
                            else
                            {
                                bool flag9 = task.TaskResult == TaskExecResult.StartExecute;
                                if (flag9)
                                {
                                    str3 = string.Format("{0} 作业计划开始执行。", task.RequiredExecuteTime.ToString());
                                    bool flag10 = string.IsNullOrEmpty(str2);
                                    if (flag10)
                                    {
                                        str2 = str3;
                                    }
                                    sql.ParamByName("JOBLOG_EXECRESULT").Value = "A";
                                    sql.ParamByName("JOBLOG_DESC").Value       = str3;
                                    sql.AddParam("JOBLOG_DETAIL", TimDbType.Blob, 0, str2);
                                }
                            }
                        }
                    }
                }
                database.ExecSQL(sql);
                task.LogInfoDetail.Clear();
                sql.Clear();
                sql.Add("update JOB set JOB_PREEXECTIME = :JOB_PREEXECTIME,JOB_PREEXECSTATUS = :JOB_PREEXECSTATUS,JOB_PREEXECDESC = :JOB_PREEXECDESC ");
                sql.Add(" where JOB_JOBID = :JOB_JOBID");
                sql.ParamByName("JOB_PREEXECSTATUS").Value = ((task.TaskResult != TaskExecResult.Success) ? ((task.TaskResult != TaskExecResult.Failure) ? ((task.TaskResult != TaskExecResult.Exception) ? "C" : "D") : "B") : "A");
                sql.ParamByName("JOB_PREEXECTIME").Value   = task.ExecuteTime;
                sql.ParamByName("JOB_PREEXECDESC").Value   = str3;
                sql.ParamByName("JOB_JOBID").Value         = task.JobId;
                database.ExecSQL(sql);
                database.CommitTrans();
            }
            catch (Exception ex)
            {
                database.RollbackTrans();
                AppEventLog.Error("记录作业计划日志失败,原因:" + ex.Message);
            }
        }