Пример #1
0
        /// <summary>
        /// 添加任务
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="start">是否立即启动</param>
        /// <returns></returns>
        private async Task <IResultModel> AddJob(JobEntity entity, bool start = false)
        {
            try
            {
                var jobClassType = Type.GetType(entity.JobClass);
                if (jobClassType == null)
                {
                    return(ResultModel.Failed($"任务类({entity.JobClass})不存在"));
                }

                var jobKey = new JobKey(entity.Code, entity.JobGroup);
                var job    = JobBuilder.Create(jobClassType).WithIdentity(jobKey)
                             .UsingJobData("id", entity.Id.ToString()).Build();

                var triggerBuilder = TriggerBuilder.Create().WithIdentity(entity.Code, entity.JobGroup)
                                     .EndAt(entity.EndTime.AddDays(1))
                                     .WithDescription(entity.Name);

                //如果开始日期小于等于当前日期则立即启动
                if (entity.StartTime <= DateTime.Now)
                {
                    triggerBuilder.StartNow();
                }
                else
                {
                    triggerBuilder.StartAt(entity.StartTime);
                }

                if (entity.TriggerType == TriggerType.Simple)
                {
                    //简单任务
                    triggerBuilder.WithSimpleSchedule(builder =>
                    {
                        builder.WithIntervalInSeconds((int)entity.Interval);
                        if (entity.RepeatCount > 0)
                        {
                            builder.WithRepeatCount((int)entity.RepeatCount);
                        }
                        else
                        {
                            builder.RepeatForever();
                        }
                    });
                }
                else
                {
                    if (!CronExpression.IsValidExpression(entity.Cron))
                    {
                        return(ResultModel.Failed("CRON表达式无效"));
                    }

                    //CRON任务
                    triggerBuilder.WithCronSchedule(entity.Cron);
                }

                var trigger = triggerBuilder.Build();
                try
                {
                    await _quartzServer.AddJob(job, trigger);

                    if (!start)
                    {
                        await _quartzServer.PauseJob(jobKey);  //先暂停
                    }
                    return(ResultModel.Success());
                }
                catch (Exception ex)
                {
                    _logger.LogError($"任务调度添加任务失败:{ex.Message}", ex);
                }

                return(ResultModel.Failed());
            }
            catch (Exception ex)
            {
                _logger.LogError($"添加任务失败:{ex.Message}", ex);
                return(ResultModel.Failed(ex.Message));
            }
        }
Пример #2
0
        /// <summary>
        /// ��������
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="start">�Ƿ���������</param>
        /// <returns></returns>
        private async Task <IResultModel> AddJob(JobEntity entity, bool start = false)
        {
            var jobClassType = Type.GetType(entity.JobClass);

            if (jobClassType == null)
            {
                return(ResultModel.Failed($"������({entity.JobClass})������"));
            }

            var jobKey         = new JobKey(entity.Code, entity.Group);
            var job            = JobBuilder.Create(jobClassType).WithIdentity(jobKey).UsingJobData("id", entity.Id.ToString()).Build();
            var triggerBuilder = TriggerBuilder.Create().WithIdentity(entity.Code, entity.Group)
                                 .EndAt(entity.EndDate.ToUniversalTime())
                                 .WithDescription(entity.Name);

            //�����ʼ����С�ڵ��ڵ�ǰ��������������
            if (entity.BeginDate <= DateTime.Now)
            {
                triggerBuilder.StartNow();
            }
            else
            {
                triggerBuilder.StartAt(entity.BeginDate.ToUniversalTime());
            }

            if (entity.TriggerType == TriggerType.Simple)
            {
                //������
                triggerBuilder.WithSimpleSchedule(builder =>
                {
                    builder.WithIntervalInSeconds(entity.Interval);
                    if (entity.RepeatCount > 0)
                    {
                        builder.WithRepeatCount(entity.RepeatCount - 1);
                    }
                    else
                    {
                        builder.RepeatForever();
                    }
                });
            }
            else
            {
                if (!CronExpression.IsValidExpression(entity.Cron))
                {
                    return(ResultModel.Failed("CRON����ʽ��Ч"));
                }

                //CRON����
                triggerBuilder.WithCronSchedule(entity.Cron);
            }

            var trigger = triggerBuilder.Build();

            try
            {
                await _quartzServer.AddJob(job, trigger);

                if (!start)
                {
                    //����ͣ
                    await _quartzServer.PauseJob(jobKey);
                }

                return(ResultModel.Success());
            }
            catch (Exception ex)
            {
                _logger.LogError("���������������ʧ��" + ex);
            }

            return(ResultModel.Failed());
        }