Exemplo n.º 1
0
        /// <summary>
        /// 恢复指定的任务计划**恢复的是暂停后的任务计划,如果是程序奔溃后 或者是进程杀死后的恢复,此方法无效
        /// </summary>
        /// <param name="sm"></param>
        /// <returns></returns>
        public async Task <(bool Status, string Message)> RunScheduleJobAsync(QuarztScheduleMapping sm)
        {
            try
            {
                var scheduler = await GetSchedulerAsync();

                await scheduler.ResumeJob(new JobKey(sm.JobName, sm.JobGroup));

                return(Success("任务恢复成功"));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "任务恢复失败");
                return(Fail($"任务恢复失败。{ex.Message}"));
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 添加任务
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public async Task <(bool Status, string Message)> AddScheduleJobAsync(QuarztScheduleMapping m)
        {
            try
            {
                if (m == null)
                {
                    return(Fail("model 为空"));
                }

                DateTimeOffset starRunTime = DateBuilder.NextGivenSecondDate(m.StarRunTime, 1);
                DateTimeOffset endRunTime  = DateBuilder.NextGivenSecondDate(m.EndRunTime, 1);
                //2、通过调度工厂获得调度器
                var scheduler = await GetSchedulerAsync();

                //创建一个触发器
                ICronTrigger trigger = (ICronTrigger)TriggerBuilder.Create()
                                       .StartAt(starRunTime)
                                       .EndAt(endRunTime)
                                       .WithIdentity(m.JobName, m.JobGroup)
                                       .WithCronSchedule(m.CronExpress)
                                       .Build();
                //创建任务
                IJobDetail jobDetail = JobBuilder.Create <CustomJob>()
                                       .WithIdentity(m.JobName, m.JobGroup)
                                       .Build();
                //将触发器和任务器绑定到调度器中
                await scheduler.ScheduleJob(jobDetail, trigger);

                await scheduler.Start();

                return(Success("添加任务成功"));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "添加任务失败");
                return(Fail($"添加任务失败。{ex.Message}"));
            }
        }