コード例 #1
0
        public async Task <IResultModel> Pause(Guid id)
        {
            var entity = await _repository.GetAsync(id);

            if (entity == null)
            {
                return(ResultModel.Failed("任务不存在"));
            }

            if (entity.Status == JobStatus.Stop)
            {
                return(ResultModel.Failed("任务已停止,无法暂停"));
            }

            if (entity.Status == JobStatus.Completed)
            {
                return(ResultModel.Failed("任务已完成,无法暂停"));
            }

            if (entity.Status == JobStatus.Pause)
            {
                return(ResultModel.Failed("任务已暂停,请刷新页面"));
            }

            var jobKey = new JobKey(entity.Code, entity.Group);
            await _quartzServer.PauseJob(jobKey);

            return(ResultModel.Success());
        }
コード例 #2
0
        public async Task <IResultModel> Pause(Guid id)
        {
            var entity = await _repository.GetAsync(id);

            if (entity == null)
            {
                return(ResultModel.Failed("任务不存在"));
            }

            if (entity.Status == JobStatus.Completed)
            {
                return(ResultModel.Failed("任务已结束"));
            }

            if (entity.Status != JobStatus.Pause)
            {
                entity.Status = JobStatus.Pause;
                using (var tran = _repository.BeginTransaction())
                {
                    if (await _repository.UpdateAsync(entity))
                    {
                        try
                        {
                            var jobKey = new JobKey(entity.Code, entity.Group);

                            await _quartzServer.PauseJob(jobKey);

                            tran.Commit();

                            return(ResultModel.Success("已暂停"));
                        }
                        catch (Exception ex)
                        {
                            _logger.LogError($"任务暂停失败:{ex}");
                        }
                    }
                }
            }

            return(ResultModel.Failed("暂停失败"));
        }
コード例 #3
0
        public async Task <IResultModel> Pause(Guid id)
        {
            var entity = await _repository.GetAsync(id);

            if (entity == null)
            {
                return(ResultModel.Failed("���񲻴���"));
            }

            if (entity.Status == JobStatus.Completed)
            {
                return(ResultModel.Failed("�����ѽ���"));
            }

            if (entity.Status != JobStatus.Pause)
            {
                entity.Status = JobStatus.Pause;
                using (var uow = _dbContext.NewUnitOfWork())
                {
                    if (await _repository.UpdateAsync(entity))
                    {
                        try
                        {
                            var jobKey = new JobKey(entity.Code, entity.Group);

                            await _quartzServer.PauseJob(jobKey);

                            uow.Commit();

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

            return(ResultModel.Failed("��ͣʧ��"));
        }
コード例 #4
0
        public async Task <IResultModel> Pause(string id)
        {
            try
            {
                var entity = await _jobRepository.FirstAsync(id);

                if (entity == null)
                {
                    return(ResultModel.NotExists);
                }

                if (entity.Status == JobStatus.Stop)
                {
                    return(ResultModel.Failed("任务已停止,无法暂停"));
                }

                if (entity.Status == JobStatus.Completed)
                {
                    return(ResultModel.Failed("任务已完成,无法暂停"));
                }

                if (entity.Status == JobStatus.Pause)
                {
                    return(ResultModel.Failed("任务已暂停,请刷新页面"));
                }


                var jobKey = new JobKey(entity.Code, entity.JobGroup);
                await _quartzServer.PauseJob(jobKey);
            }
            catch (Exception ex)
            {
                _logger.LogError($"暂停任务失败,任务ID:{id},异常信息:{ex.Message}", ex);
                return(ResultModel.Failed(ex.Message));
            }

            return(ResultModel.Success());
        }
コード例 #5
0
        /// <summary>
        /// 添加任务
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="start">是否立即启动</param>
        /// <returns></returns>
        private async Task <IResultModel> AddJob(TaskEntity entity, bool start = false)
        {
            var jobClassType = Type.GetType(entity.ClassName);

            if (jobClassType == null)
            {
                return(ResultModel.Failed($"任务类({entity.ClassName})不存在"));
            }

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

            var triggerBuilder = TriggerBuilder.Create().WithIdentity(entity.TaskCode, entity.Group)
                                 .EndAt(entity.EndDate.ToUniversalTime())
                                 .WithDescription(entity.TaskName);

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

            if (entity.TriggerType == 1)
            {
                //简单任务
                triggerBuilder.WithSimpleSchedule(builder =>
                {
                    builder.WithIntervalInSeconds(entity.IntervalInSeconds);
                    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}", ex);
            }

            return(ResultModel.Failed());
        }