Esempio n. 1
0
        /// <summary>
        /// 立即运行
        /// </summary>
        /// <param name="jobName"></param>
        /// <param name="jobGroup"></param>
        /// <returns></returns>
        public async Task <ScheduleResult> RunOnceTask(string jobName, string jobGroup)
        {
            var result = new ScheduleResult();
            var jk     = new JobKey(jobName, jobGroup);

            if (!await Scheduler.Result.CheckExists(jk))
            {
                return(new ScheduleResult
                {
                    Code = -1,
                    Msg = jobName + "立即运行未完成"
                });
            }
            var    jobDetail = Scheduler.Result.GetJobDetail(jk).Result;
            var    triggers  = Scheduler.Result.GetTriggersOfJob(jk).Result;
            string taskName  = jobName;

            var type     = jobDetail.JobType;
            var instance = Activator.CreateInstance(type);
            var method   = type.GetMethod("Execute");

            method.Invoke(instance, new object[] { null });
            await Console.Out.WriteLineAsync(string.Format("任务“{0}”立即运行", taskName));

            result = new ScheduleResult
            {
                Code = 1000,
                Msg  = "运行成功!"
            };
            return(result);
        }
Esempio n. 2
0
        /// <summary>
        /// 添加一个工作调度(映射程序集指定IJob实现类)
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        private async Task <ScheduleResult> AddScheduleJob(ScheduleEntity m)
        {
            var result = new ScheduleResult();

            try
            {
                //检查任务是否已存在
                var jk = new JobKey(m.JobName, m.JobGroup);
                if (await this.Scheduler.Result.CheckExists(jk))
                {
                    //删除已经存在任务
                    await this.Scheduler.Result.DeleteJob(jk);
                }
                //反射获取任务执行类
                //var jobType = FileHelper.GetAbsolutePath(m.AssemblyName, m.AssemblyName + "." + m.ClassName);

                Assembly assembly = Assembly.Load(new AssemblyName(m.AssemblyName.Trim()));
                Type     jobType  = assembly.GetType(m.AssemblyName.Trim() + "." + m.ClassName.Trim());

                // 定义这个工作,并将其绑定到我们的IJob实现类
                IJobDetail job = new JobDetailImpl(m.JobName, m.JobGroup, jobType);
                //IJobDetail job = JobBuilder.CreateForAsync<T>().WithIdentity(m.JobName, m.JobGroup).Build();
                // 创建触发器
                ITrigger trigger;
                //校验是否正确的执行周期表达式
                if (!string.IsNullOrEmpty(m.Cron) && CronExpression.IsValidExpression(m.Cron))
                {
                    trigger = CreateCronTrigger(m);
                }
                else
                {
                    trigger = CreateSimpleTrigger(m);
                }
                // 设置监听器
                JobListener listener = new JobListener();
                // IMatcher<JobKey> matcher = KeyMatcher<JobKey>.KeyEquals(job.Key);
                this.Scheduler.Result.ListenerManager.AddJobListener(listener, GroupMatcher <JobKey> .AnyGroup());
                // 告诉Quartz使用我们的触发器来安排作业
                await this.Scheduler.Result.ScheduleJob(job, trigger);

                result.Code = 1000;
            }
            catch (Exception ex)
            {
                await Console.Out.WriteLineAsync(string.Format("添加任务出错{0}", ex.Message));

                result.Code = 1001;
                result.Msg  = ex.Message;
            }
            return(result);
        }
Esempio n. 3
0
        /// <summary>
        /// 添加任务调度(指定IJob实现类)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="m"></param>
        /// <returns></returns>
        private async Task <ScheduleResult> AddScheduleJob <T>(ScheduleEntity m) where T : IJob
        {
            var result = new ScheduleResult();

            try
            {
                //检查任务是否已存在
                var jk = new JobKey(m.JobName, m.JobGroup);
                if (await this.Scheduler.Result.CheckExists(jk))
                {
                    //删除已经存在任务
                    await this.Scheduler.Result.DeleteJob(jk);
                }
                // 定义这个工作,并将其绑定到我们的IJob实现类
                IJobDetail job = JobBuilder.CreateForAsync <T>().WithIdentity(m.JobName, m.JobGroup).Build();
                // 创建触发器
                ITrigger trigger;
                //校验是否正确的执行周期表达式
                if (!string.IsNullOrEmpty(m.Cron) && CronExpression.IsValidExpression(m.Cron))
                {
                    trigger = CreateCronTrigger(m);
                }
                else
                {
                    trigger = CreateSimpleTrigger(m);
                }
                // 设置监听器
                JobListener listener = new JobListener();
                // IMatcher<JobKey> matcher = KeyMatcher<JobKey>.KeyEquals(job.Key);
                this.Scheduler.Result.ListenerManager.AddJobListener(listener, GroupMatcher <JobKey> .AnyGroup());
                // 告诉Quartz使用我们的触发器来安排作业
                await this.Scheduler.Result.ScheduleJob(job, trigger);

                result.Code = 1000;
            }
            catch (Exception ex)
            {
                await Console.Out.WriteLineAsync(string.Format("添加任务出错", ex.Message));

                result.Code = 1001;
                result.Msg  = ex.Message;
            }
            return(result);
        }
Esempio n. 4
0
        /// <summary>
        /// 恢复运行暂停的任务
        /// </summary>
        /// <param name="jobName">任务名称</param>
        /// <param name="jobGroup">任务分组</param>
        public async Task <ScheduleResult> ResumeJob(string jobName, string jobGroup)
        {
            var result = new ScheduleResult();

            try
            {
                //检查任务是否存在
                var jk = new JobKey(jobName, jobGroup);
                if (!await Scheduler.Result.CheckExists(jk))
                {
                    return(new ScheduleResult
                    {
                        Code = -1,
                        Msg = jobName + "任务未运行"
                    });
                }
                //任务已经存在则恢复运行暂停任务
                await this.Scheduler.Result.ResumeJob(jk);

                await Console.Out.WriteLineAsync(string.Format("任务“{0}”恢复运行", jobName));

                result = new ScheduleResult
                {
                    Code = 1000,
                    Msg  = "恢复运行任务成功!"
                };
            }
            catch (Exception ex)
            {
                await Console.Out.WriteLineAsync(string.Format("恢复任务失败!{0}", ex));

                result = new ScheduleResult
                {
                    Code = -1,
                    Msg  = "恢复运行任务失败"
                };
            }
            return(result);
        }
Esempio n. 5
0
        /// <summary>
        /// 暂停指定的计划
        /// </summary>
        /// <param name="jobGroup">任务分组</param>
        /// <param name="jobName">任务名称</param>
        /// <param name="isDelete">停止并删除任务</param>
        /// <returns></returns>
        public async Task <ScheduleResult> StopScheduleJob <T>(string jobGroup, string jobName, bool isDelete = false) where T : ScheduleManage, new()
        {
            var result = new ScheduleResult();

            try
            {
                //检查任务是否存在
                var jk = new JobKey(jobName, jobGroup);
                if (!await Scheduler.Result.CheckExists(jk))
                {
                    return(new ScheduleResult
                    {
                        Code = -1,
                        Msg = jobName + "任务未运行"
                    });
                }
                await this.Scheduler.Result.PauseJob(jk);

                if (isDelete)
                {
                    Activator.CreateInstance <T>().RemoveScheduleModel(jobGroup, jobName);
                }
                result = new ScheduleResult
                {
                    Code = 1000,
                    Msg  = "停止任务成功!"
                };
            }
            catch (Exception ex)
            {
                result = new ScheduleResult
                {
                    Code = -1,
                    Msg  = "停止任务失败"
                };
                throw ex;
            }
            return(result);
        }
Esempio n. 6
0
        /// <summary>
        ///  运行指定的计划(泛型指定IJob实现类)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="V"></typeparam>
        /// <param name="jobGroup"></param>
        /// <param name="jobName"></param>
        /// <returns></returns>
        public async Task <ScheduleResult> RunScheduleJob <T, V>(string jobGroup, string jobName) where T : ScheduleManage, new() where V : IJob
        {
            ScheduleResult result;

            //开启调度器
            await this.Scheduler.Result.Start();

            //创建指定泛型类型参数指定的类型实例
            T t = Activator.CreateInstance <T>();
            //获取任务实例
            ScheduleEntity scheduleModel = t.GetScheduleModel(jobGroup, jobName);
            //添加任务
            var addResult = AddScheduleJob <V>(scheduleModel).Result;

            if (addResult.Code == 1000)
            {
                scheduleModel.Status = JobStatus.Start;
                t.UpdateScheduleStatus(scheduleModel);
                //用给定的密钥恢复(取消暂停)IJobDetail
                await this.Scheduler.Result.ResumeJob(new JobKey(jobName, jobGroup));

                result = new ScheduleResult
                {
                    Code = 1000,
                    Msg  = "启动成功"
                };
            }
            else
            {
                result = new ScheduleResult
                {
                    Code = -1,
                    Msg  = addResult.Msg
                };
            }

            return(result);
        }