Example #1
0
        /// <summary>
        /// 记录任务运行结果
        /// </summary>
        /// <param name="jobId">任务Id</param>
        /// <param name="jobAction">任务执行动作</param>
        /// <param name="blresultTag">任务执行结果表示,true成功,false失败,初始执行为true</param>
        /// <param name="msg">任务记录描述</param>
        public void RecordRun(string jobId, JobAction jobAction, bool blresultTag = true, string msg = "")
        {
            DateTime    addTime = DateTime.Now;
            TaskManager job     = _repository.GetSingle(jobId);

            if (job == null)
            {
                _taskJobsLogService.Insert(new TaskJobsLog
                {
                    Id          = GuidUtils.CreateNo(),
                    CreatorTime = DateTime.Now,
                    TaskId      = jobId,
                    TaskName    = "",
                    JobAction   = jobAction.ToString(),
                    Status      = false,
                    Description = $"未能找到定时任务:{jobId}"
                });
                return;
            }
            string resultStr = string.Empty, strDesc = string.Empty;

            if (!blresultTag)
            {
                job.ErrorCount++;
                job.LastErrorTime = addTime;
                strDesc           = $"异常," + msg;
            }
            else
            {
                strDesc = $"正常," + msg;
            }
            if (jobAction == JobAction.开始)
            {
                job.RunCount++;
                job.LastRunTime = addTime;

                CronExpression cronExpression = new CronExpression(job.Cron);
                job.NextRunTime = cronExpression.GetNextValidTimeAfter(addTime).ToDateTime();
            }
            _repository.Update(job, jobId);

            _taskJobsLogService.Insert(new TaskJobsLog
            {
                Id          = GuidUtils.CreateNo(),
                CreatorTime = DateTime.Now,
                TaskId      = job.Id,
                TaskName    = job.TaskName,
                JobAction   = jobAction.ToString(),
                Status      = blresultTag,
                Description = strDesc
            });
        }