コード例 #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>
        /// <param name="crontabType">定时任务类型</param>
        /// <param name="strategy">执行策略</param>
        /// <returns>定时任务</returns>
        public static ICrontab CreateCrontab(Type crontabType, ExecutionStrategy strategy)
        {
            #region # 验证

            if (crontabType == null)
            {
                throw new ArgumentNullException(nameof(crontabType), "定时任务类型不可为空!");
            }
            if (strategy == null)
            {
                throw new ArgumentNullException(nameof(strategy), "执行策略不可为空!");
            }
            if (!typeof(ICrontab).IsAssignableFrom(crontabType))
            {
                throw new ArgumentOutOfRangeException(nameof(crontabType), "给定类型不是定时任务类型!");
            }

            #endregion

            object instance = Activator.CreateInstance(crontabType, strategy);

            #region # 验证

            if (instance == null)
            {
                throw new NullReferenceException("给定类型的定时任务不存在!");
            }

            #endregion

            ICrontab crontab = (ICrontab)instance;

            return(crontab);
        }
コード例 #3
0
        /// <summary>
        /// 获取定时任务
        /// </summary>
        /// <typeparam name="T">定时任务类型</typeparam>
        /// <param name="crontabId">定时任务Id</param>
        /// <returns>定时任务</returns>
        public T Get <T>(string crontabId) where T : ICrontab
        {
            string   crontabStr = this._redisClient.HashGet(_CacheKey, crontabId);
            ICrontab crontab    = crontabStr.JsonToCrontab();

            return((T)crontab);
        }
コード例 #4
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);
            }
        }
コード例 #5
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);
            }
        }
コード例 #6
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);
            }
        }
コード例 #7
0
        /// <summary>
        /// 获取全部定时任务列表
        /// </summary>
        /// <returns>定时任务列表</returns>
        public IList <ICrontab> FindAll()
        {
            RedisValue[]     crontabsStr = this._redisClient.HashValues(_CacheKey);
            IList <ICrontab> crontabs    = new List <ICrontab>();

            foreach (string crontabStr in crontabsStr)
            {
                ICrontab specCrontab = crontabStr.JsonToCrontab();
                crontabs.Add(specCrontab);
            }

            return(crontabs);
        }
コード例 #8
0
        /// <summary>
        /// 创建定时任务
        /// </summary>
        /// <param name="assemblyName">程序集名称</param>
        /// <param name="typeName">类型名称</param>
        /// <param name="strategy">执行策略</param>
        /// <returns>定时任务</returns>
        public static ICrontab CreateCrontab(string assemblyName, string typeName, ExecutionStrategy strategy)
        {
            #region # 验证

            if (string.IsNullOrWhiteSpace(assemblyName))
            {
                throw new ArgumentNullException(nameof(assemblyName), "程序集名称不可为空!");
            }
            if (string.IsNullOrWhiteSpace(typeName))
            {
                throw new ArgumentNullException(nameof(typeName), "类型名称不可为空!");
            }
            if (strategy == null)
            {
                throw new ArgumentNullException(nameof(strategy), "执行策略不可为空!");
            }

            #endregion

            Type crontabType = Type.GetType($"{typeName},{assemblyName}");

            #region # 验证

            if (crontabType == null)
            {
                throw new NullReferenceException("给定类型的定时任务不存在!");
            }
            if (!typeof(ICrontab).IsAssignableFrom(crontabType))
            {
                throw new ArgumentOutOfRangeException(nameof(typeName), "给定类型不是定时任务类型!");
            }

            #endregion

            object instance = Activator.CreateInstance(crontabType, strategy);

            #region # 验证

            if (instance == null)
            {
                throw new NullReferenceException("给定类型的定时任务不存在!");
            }

            #endregion

            ICrontab crontab = (ICrontab)instance;

            return(crontab);
        }
コード例 #9
0
        /// <summary>
        /// 获取定时任务
        /// </summary>
        /// <typeparam name="T">定时任务类型</typeparam>
        /// <param name="crontabId">定时任务Id</param>
        /// <returns>定时任务</returns>
        public T Get <T>(string crontabId) where T : ICrontab
        {
            string crontabStr = this._redisClient.HashGet(_CacheKey, crontabId);

            #region # 验证

            if (string.IsNullOrWhiteSpace(crontabStr))
            {
                return(default(T));
            }

            #endregion

            ICrontab crontab = crontabStr.JsonToCrontab();

            return((T)crontab);
        }
コード例 #10
0
        /// <summary>
        /// 获取全部定时任务列表
        /// </summary>
        /// <returns>定时任务列表</returns>
        public IList <ICrontab> FindAll()
        {
            IList <ICrontab> crontabs = new List <ICrontab>();

            RedisValue[] crontabsStr = this._redisClient.HashValues(_CacheKey);
            foreach (string crontabStr in crontabsStr)
            {
                #region # 验证

                if (string.IsNullOrWhiteSpace(crontabStr))
                {
                    continue;
                }

                #endregion

                ICrontab crontab = crontabStr.JsonToCrontab();
                crontabs.Add(crontab);
            }

            return(crontabs);
        }
コード例 #11
0
 /// <summary>
 /// 删除定时任务
 /// </summary>
 /// <param name="crontab">定时任务</param>
 public void Remove(ICrontab crontab)
 {
     this._redisClient.HashDelete(_CacheKey, crontab.Id);
 }
コード例 #12
0
 /// <summary>
 /// 保存定时任务
 /// </summary>
 /// <param name="crontab">定时任务</param>
 public void Store(ICrontab crontab)
 {
     this._redisClient.HashSet(_CacheKey, crontab.Id, crontab.CrontabToJson());
 }