コード例 #1
0
        /// <summary>
        /// 恢复任务
        /// </summary>
        /// <param name="crontabId">定时任务Id</param>
        public static void Resume(string crontabId)
        {
            JobKey jobKey = new JobKey(crontabId);

            if (_Scheduler.CheckExists(jobKey).Result)
            {
                _Scheduler.ResumeJob(jobKey).Wait();
            }
            else
            {
                using (ICrontabStore crontabStore = ResolveMediator.ResolveOptional <ICrontabStore>())
                {
                    if (crontabStore != null)
                    {
                        ICrontab crontab = crontabStore.Get <ICrontab>(crontabId);
                        if (crontab != null)
                        {
                            ScheduleMediator.Schedule(crontab);
                        }
                        else
                        {
                            throw new NullReferenceException($"Id为\"{crontabId}\"的任务不存在!");
                        }
                    }
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// 清空任务
        /// </summary>
        public static void Clear()
        {
            _Scheduler.Clear().Wait();

            using (ICrontabStore crontabStore = ResolveMediator.ResolveOptional <ICrontabStore>())
            {
                crontabStore?.Clear();
            }
        }
コード例 #3
0
        /// <summary>
        /// 调度任务
        /// </summary>
        /// <typeparam name="T">任务类型</typeparam>
        /// <param name="delay">延迟时间(毫秒)</param>
        /// <remarks>适用于没有参数,只有策略的任务</remarks>
        public static void Schedule <T>(int delay = 1000) where T : class, ICrontab
        {
            //调度任务
            Type crontabType = typeof(T);

            #region # 验证

            if (!CrontabSetting.CrontabStrategies.ContainsKey(crontabType.Name))
            {
                throw new InvalidOperationException($"没有为定时任务\"{crontabType.Name}\"配置执行策略");
            }

            #endregion

            ExecutionStrategy executionStrategy = CrontabSetting.CrontabStrategies[crontabType.Name];
            ICrontab          crontab           = CrontabFactory.CreateCrontab(crontabType, executionStrategy);

            IEnumerable <ICrontabExecutor> crontabExecutors = CrontabExecutorFactory.GetCrontabExecutorsFor(crontabType);
            foreach (ICrontabExecutor crontabExecutor in crontabExecutors)
            {
                JobKey jobKey = new JobKey(crontab.Id);
                if (!_Scheduler.CheckExists(jobKey).Result)
                {
                    Type       jobType    = crontabExecutor.GetType();
                    JobBuilder jobBuilder = JobBuilder.Create(jobType);

                    //设置任务数据
                    IDictionary <string, object> dictionary = new Dictionary <string, object>();
                    dictionary.Add(crontab.Id, crontab);
                    jobBuilder.SetJobData(new JobDataMap(dictionary));

                    //创建任务明细
                    IJobDetail jobDetail = jobBuilder.WithIdentity(jobKey).Build();

                    //创建触发器
                    ITrigger trigger = GetTrigger(crontab.ExecutionStrategy);

                    //为调度者添加任务明细与触发器
                    _Scheduler.ScheduleJob(jobDetail, trigger).Wait();

                    //开始调度
                    _Scheduler.StartDelayed(TimeSpan.FromMilliseconds(delay)).Wait();
                }
                else
                {
                    _Scheduler.ResumeJob(jobKey).Wait();
                }
            }

            //保存任务
            using (ICrontabStore crontabStore = ResolveMediator.ResolveOptional <ICrontabStore>())
            {
                crontab.Status = CrontabStatus.Scheduled;
                crontabStore?.Store(crontab);
            }
        }
コード例 #4
0
        //Public

        #region # 保存任务 —— static void Store(ICrontab crontab)
        /// <summary>
        /// 保存任务
        /// </summary>
        /// <param name="crontab">定时任务</param>
        public static void Store(ICrontab crontab)
        {
            using (ICrontabStore crontabStore = ResolveMediator.ResolveOptional <ICrontabStore>())
            {
                if (crontabStore == null)
                {
                    throw new NotImplementedException("未注册持久化存储提供者!");
                }
                crontabStore.Store(crontab);
            }
        }
コード例 #5
0
        /// <summary>
        /// 调度任务
        /// </summary>
        /// <param name="crontab">定时任务</param>
        public static void Schedule(ICrontab crontab)
        {
            #region # 验证

            if (crontab.ExecutionStrategy == null)
            {
                throw new InvalidOperationException("执行策略不可为空!");
            }

            #endregion

            //调度任务
            Type crontabType = crontab.GetType();
            IEnumerable <ICrontabExecutor> crontabSchedulers = CrontabExecutorFactory.GetCrontabExecutorsFor(crontabType);
            foreach (ICrontabExecutor scheduler in crontabSchedulers)
            {
                JobKey jobKey = new JobKey(crontab.Id);

                if (!_Scheduler.CheckExists(jobKey).Result)
                {
                    Type       jobType    = scheduler.GetType();
                    JobBuilder jobBuilder = JobBuilder.Create(jobType);

                    //设置任务数据
                    IDictionary <string, object> dictionary = new Dictionary <string, object>();
                    dictionary.Add(crontab.Id, crontab);
                    jobBuilder.SetJobData(new JobDataMap(dictionary));

                    //创建任务明细
                    IJobDetail jobDetail = jobBuilder.WithIdentity(jobKey).Build();

                    //创建触发器
                    ITrigger trigger = GetTrigger(crontab.ExecutionStrategy);

                    //为调度者添加任务明细与触发器
                    _Scheduler.ScheduleJob(jobDetail, trigger).Wait();

                    //开始调度
                    _Scheduler.Start().Wait();
                }
                else
                {
                    _Scheduler.ResumeJob(jobKey).Wait();
                }
            }

            //保存任务
            using (ICrontabStore crontabStore = ResolveMediator.ResolveOptional <ICrontabStore>())
            {
                crontab.Status = CrontabStatus.Scheduled;
                crontabStore?.Store(crontab);
            }
        }
コード例 #6
0
        /// <summary>
        /// 获取全部任务列表
        /// </summary>
        /// <returns>定时任务列表</returns>
        /// <remarks>需要CrontabStore持久化支持</remarks>
        public static IList <ICrontab> FindAll()
        {
            using (ICrontabStore crontabStore = ResolveMediator.ResolveOptional <ICrontabStore>())
            {
                if (crontabStore == null)
                {
                    throw new NotImplementedException("未注册持久化存储提供者!");
                }

                IList <ICrontab> crontabs = crontabStore.FindAll();

                return(crontabs);
            }
        }
コード例 #7
0
        /// <summary>
        /// 获取任务
        /// </summary>
        /// <typeparam name="T">定时任务类型</typeparam>
        /// <param name="crontabId">定时任务Id</param>
        /// <returns>定时任务</returns>
        /// <remarks>需要CrontabStore持久化支持</remarks>
        public static T GetCrontab <T>(string crontabId) where T : ICrontab
        {
            using (ICrontabStore crontabStore = ResolveMediator.ResolveOptional <ICrontabStore>())
            {
                if (crontabStore == null)
                {
                    throw new NotImplementedException("未注册持久化存储提供者!");
                }

                T crontab = crontabStore.Get <T>(crontabId);

                return(crontab);
            }
        }
コード例 #8
0
        /// <summary>
        /// 删除任务
        /// </summary>
        /// <param name="crontabId">定时任务Id</param>
        public static void Remove(string crontabId)
        {
            JobKey jobKey = new JobKey(crontabId);

            if (_Scheduler.CheckExists(jobKey).Result)
            {
                _Scheduler.DeleteJob(jobKey).Wait();
            }

            using (ICrontabStore crontabStore = ResolveMediator.ResolveOptional <ICrontabStore>())
            {
                crontabStore?.Remove(crontabId);
            }
        }
コード例 #9
0
        /// <summary>
        /// 删除任务
        /// </summary>
        /// <param name="crontab">定时任务</param>
        public static void Remove <T>(T crontab) where T : class, ICrontab
        {
            JobKey jobKey = new JobKey(crontab.Id);

            if (_Scheduler.CheckExists(jobKey).Result)
            {
                _Scheduler.DeleteJob(jobKey).Wait();
            }

            using (ICrontabStore crontabStore = ResolveMediator.ResolveOptional <ICrontabStore>())
            {
                crontabStore?.Remove(crontab);
            }
        }
コード例 #10
0
        /// <summary>
        /// 恢复全部任务
        /// </summary>
        /// <remarks>需要CrontabStore持久化支持</remarks>
        public static void ResumeAll()
        {
            using (ICrontabStore crontabStore = ResolveMediator.ResolveOptional <ICrontabStore>())
            {
                if (crontabStore == null)
                {
                    throw new NotImplementedException("未注册持久化存储提供者!");
                }

                IList <ICrontab> crontabs = crontabStore.FindAll();

                foreach (ICrontab crontab in crontabs)
                {
                    ScheduleMediator.Schedule(crontab);
                }
            }
        }
コード例 #11
0
        /// <summary>
        /// 异常恢复
        /// </summary>
        /// <remarks>需要CrontabStore持久化支持</remarks>
        public static void Recover()
        {
            using (ICrontabStore crontabStore = ResolveMediator.ResolveOptional <ICrontabStore>())
            {
                if (crontabStore == null)
                {
                    throw new NotImplementedException("未注册持久化存储提供者!");
                }

                IList <ICrontab>       crontabs          = crontabStore.FindAll();
                IEnumerable <ICrontab> scheduledCrontabs = crontabs.Where(x => x.Status == CrontabStatus.Scheduled);
                foreach (ICrontab crontab in scheduledCrontabs)
                {
                    Schedule(crontab);
                }
            }
        }