Exemplo n.º 1
0
        /// <summary>
        /// 任务添加
        /// </summary>
        /// <param name="job"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        private async Task JobAdd(Job job, IJobExecutionContext context)
        {
            try
            {
                string group  = job.Group;
                string name   = job.Name;
                JobKey jobKey = new JobKey(name, group);
                if (!await context.Scheduler.CheckExists(jobKey))
                {
                    IJobDetail jobDetail = JobBuilder.Create <BaseJob>()
                                           .WithIdentity(jobKey)
                                           .UsingJobData("RequestUrl", job.RequestUrl)
                                           .Build();

                    ITrigger trigger = TriggerBuilder.Create()
                                       .WithIdentity(group, name)
                                       .StartNow()
                                       .WithCronSchedule(job.CronExpression)
                                       .Build();

                    await context.Scheduler.ScheduleJob(jobDetail, trigger);

                    await _jobRepository.UpdateStatusAsync(job.Id, (int)JobStatusEnum.Running);
                }
            }
            catch (Exception ex)
            {
                await _jobRunLogRepository.InsertAsync(new JobRunLog(job.Group, job.Name, 0, DateTime.Now, false, "任务新增失败:" + ex.StackTrace));
            }
        }
Exemplo n.º 2
0
        public override async Task JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException, CancellationToken cancellationToken = default)
        {
            string   group       = context.JobDetail.Key.Group;
            string   name        = context.JobDetail.Key.Name;
            DateTime fireTimeUtc = TimeZoneInfo.ConvertTimeFromUtc(context.FireTimeUtc.DateTime, TimeZoneInfo.Local);

            DateTime?nextFireTimeUtc = null;

            if (context.NextFireTimeUtc != null)
            {
                nextFireTimeUtc = TimeZoneInfo.ConvertTimeFromUtc(context.NextFireTimeUtc.Value.DateTime, TimeZoneInfo.Local);
            }

            if (!JobHelper.IsBaseJob(group, name))
            {
                //更新任务运行情况
                await _jobRepository.UpdateExecuteAsync(group, name, fireTimeUtc, nextFireTimeUtc);

                //记录运行日志
                double totalSeconds = context.JobRunTime.TotalSeconds;
                bool   succ         = true;
                string exception    = string.Empty;
                if (jobException != null)
                {
                    succ      = false;
                    exception = jobException.ToString();
                }

                JobRunLog log = new JobRunLog(group, name, totalSeconds, fireTimeUtc, succ, exception);
                await _jobRunLogRepository.InsertAsync(log);
            }
        }