コード例 #1
0
        public async Task AddJobAsync(AddScheduleInput scheduleInput)
        {
            if (await _scheduler.CheckExists(new JobKey(scheduleInput.JobName, scheduleInput.GroupName)))
            {
                throw new BusinessException($"任务已存在");
            }
            var validExpression = IsValidExpression(scheduleInput.Cron);

            if (!validExpression)
            {
                throw new BusinessException($"请确认表达式{scheduleInput.Cron}是否正确!");
            }
            scheduleInput.Status = (int)TriggerState.Normal;
            //http请求配置
            var httpDir = new Dictionary <string, string>()
            {
                { QuartzConstant.REQUESTURL, scheduleInput.RequestUrl },
                { QuartzConstant.REQUESTPARAMS, scheduleInput.RequestParams },
                { QuartzConstant.REQUESTTYPE, ((int)scheduleInput.RequestType).ToString() },
                { QuartzConstant.HEADERS, scheduleInput.Headers },
                { QuartzConstant.CREATETIME, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") },
            };
            // 定义这个工作,并将其绑定到我们的IJob实现类
            IJobDetail job = JobBuilder.Create <HttpJob>()
                             .SetJobData(new JobDataMap(httpDir))
                             .WithDescription(scheduleInput.Describe)
                             .WithIdentity(scheduleInput.JobName, scheduleInput.GroupName)
                             .Build();
            ITrigger trigger = TriggerBuilder.Create()
                               .WithIdentity(scheduleInput.JobName, scheduleInput.GroupName)
                               .StartNow()
                               .WithDescription(scheduleInput.Describe)
                               .WithCronSchedule(scheduleInput.Cron, cronScheduleBuilder => cronScheduleBuilder.WithMisfireHandlingInstructionFireAndProceed()) //指定cron表达式
                               .ForJob(scheduleInput.JobName, scheduleInput.GroupName)                                                                          //作业名称
                               .Build();

            await _scheduler.ScheduleJob(job, trigger);

            if (scheduleInput.Status == (int)TriggerState.Normal)
            {
                await _scheduler.Start();
            }
            else
            {
                await TriggerAction(scheduleInput.JobName, scheduleInput.GroupName, JobAction.暂停, scheduleInput);

                //FileQuartz.WriteStartLog($"作业:{taskOptions.TaskName},分组:{taskOptions.GroupName},新建时未启动原因,状态为:{taskOptions.Status}");
            }
        }
コード例 #2
0
        public async Task <IActionResult> Update([FromBody] AddScheduleInput input)
        {
            await _schedulerCenter.UpdateJobAsync(input);

            return(Ok(ResponseBody.From("修改成功")));
        }
コード例 #3
0
 public async Task UpdateJobAsync(AddScheduleInput scheduleInput)
 {
     await TriggerAction(scheduleInput.JobName, scheduleInput.GroupName, JobAction.修改, scheduleInput);
 }
コード例 #4
0
        /// <summary>
        /// 触发新增、删除、修改、暂停、启用、立即执行事件
        /// </summary>
        /// <param name="schedulerFactory"></param>
        /// <param name="taskName"></param>
        /// <param name="groupName"></param>
        /// <param name="action"></param>
        /// <param name="taskOptions"></param>
        /// <returns></returns>
        public async Task TriggerAction(string taskName, string groupName, JobAction action, AddScheduleInput scheduleInput = null)
        {
            if (string.IsNullOrEmpty(taskName) || string.IsNullOrEmpty(groupName))
            {
                throw new BusinessException($"任务名或组名不能为空");
            }
            var scheduler  = _scheduler;
            var jobKey     = new JobKey(taskName, groupName);
            var triggerKey = new TriggerKey(taskName, groupName);

            if (!await scheduler.CheckExists(jobKey))
            {
                throw new BusinessException($"未发现任务");
            }
            if (!await scheduler.CheckExists(triggerKey))
            {
                throw new BusinessException($"未发现触发器");
            }

            switch (action)
            {
            case JobAction.除:
            case JobAction.修改:
                await scheduler.PauseTrigger(triggerKey);

                await scheduler.UnscheduleJob(triggerKey);    // 移除触发器

                await scheduler.DeleteJob(jobKey);

                if (action == JobAction.修改)
                {
                    await AddJobAsync(scheduleInput);

                    $">>>>{triggerKey}修改成功".WriteSuccessLine();
                }
                break;

            case JobAction.暂停:
                await scheduler.PauseTrigger(triggerKey);

                $">>>>{triggerKey}暂停".WriteSuccessLine();
                break;

            case JobAction.停止:
                await scheduler.Shutdown();

                $">>>>{triggerKey}停止".WriteSuccessLine();
                break;

            case JobAction.开启:
                await scheduler.ResumeTrigger(triggerKey);

                $">>>>{triggerKey}开启".WriteSuccessLine();
                break;

            case JobAction.立即执行:
                await scheduler.TriggerJob(jobKey);

                $">>>>{triggerKey}立即执行".WriteSuccessLine();
                break;
            }
        }