Beispiel #1
0
 public void RunJob(int jobId)
 {
     using (var ctx = new BatchJobDbContext())
     {
         try
         {
             var job = ctx.Jobs.Find(jobId);
             if (job != null)
             {
                 if (job.Triggers == null || job.Triggers.Count <= 0)
                 {
                     throw new BusinessException("当前作业未配置触发器.");
                 }
                 var scheduler = QuartzHelper.GetScheduler(job.JobGroup.Scheduler.Name, job.JobGroup.Scheduler.ThreadPoolSize, (int)job.JobGroup.Scheduler.Priority);
                 foreach (var trigger in job.Triggers)
                 {
                     if (trigger.Type == TriggerType.Cron)
                     {
                         var cronTrigger = DomainContext.Current.Set <CronTriggerEntity>().Find(trigger.Id);
                         if (cronTrigger != null)
                         {
                             scheduler.ScheduleJobIfExists(job.Code, job.JobGroup.Name, cronTrigger.Code, cronTrigger.CronExpression, typeof(ExcuteJob));
                         }
                     }
                 }
                 job.RunJob();
                 ctx.SaveChanges();
             }
         }
         catch (Exception ex)
         {
             throw ex;
         }
     }
 }
        // GET: /Home/
        public async Task <ActionResult> Index(string group)
        {
            IScheduler scheduler = null;

            ViewBag.ErrorDetails = "";
            try
            {
                List <SelectListItem> items = db.Jobs.Select(o => new SelectListItem()
                {
                    Text = o.GroupName, Value = o.GroupName
                }).Distinct().OrderBy(o => o.Text).ToList();
                ViewBag.Groups = items;
                if (group == null && items.Count() > 0)
                {
                    group = items.First().Text;
                }

                scheduler = await QuartzHelper.GetScheduler();
            }
            catch (Exception ex)
            {
                ViewBag.ErrorDetails = ex.Message;
            }
            ViewBag.Group = group;
            return(View("Index", scheduler));
        }
        public async Task <ActionResult> ResumeTrigger(string name, string group)
        {
            IScheduler scheduler = await QuartzHelper.GetScheduler();

            await scheduler.ResumeTrigger(new TriggerKey(name, group));

            return(RedirectToAction("Index", "Home", new { group = group }));
        }
        public async Task <ActionResult> Delete(string name, string group)
        {
            IScheduler scheduler = await QuartzHelper.GetScheduler();

            await scheduler.DeleteJob(new JobKey(name, group));

            return(RedirectToAction("Index", "Home", new { group = group }));
        }
Beispiel #5
0
        static void Main(string[] args)
        {
            var key = "test";
            var jt  = key.ToJobTrigger();

            var trigger = QuartzHelper.CreateTrigger(jt.Trigger, "0/2 * * * * ?", DateTime.UtcNow, DateTime.UtcNow.AddSeconds(6));
            // var trigger = QuartzHelper.CreateTrigger(jt.Trigger, "0/2 * * * * ?",DateTime.Now);
            var job = QuartzHelper.CreateSimpleJob(jt.Job, () => Console.WriteLine(DateTime.Now));

            QuartzHelper.GetScheduler().ScheduleJob(job, trigger);

            Console.ReadKey();
        }
        public async Task <ActionResult> StopScheduler()
        {
            IScheduler scheduler = await QuartzHelper.GetScheduler();

            if (!scheduler.InStandbyMode)
            {
                await scheduler.Standby();
            }
            else
            {
                await scheduler.Start();
            }

            return(RedirectToAction("Index"));
        }
        public async Task <PartialViewResult> RunningJobs()
        {
            IScheduler scheduler = null;

            ViewBag.ErrorDetails = "";
            try
            {
                scheduler = await QuartzHelper.GetScheduler();
            }
            catch (Exception ex)
            {
                ViewBag.ErrorDetails = ex.Message;
            }

            return(PartialView(scheduler));
        }
        public async Task <ActionResult> InterruptJob(string name, string group, string instance)
        {
            TempData["ErrorDetails"] = "";
            try
            {
                IScheduler scheduler = await QuartzHelper.GetScheduler();

                //scheduler.Interrupt(new JobKey(name, group));
                await scheduler.Interrupt(instance);
            }
            catch (Exception ex)
            {
                TempData["ErrorDetails"] = ex.Message;
                // give up
            }

            return(RedirectToAction("Index"));
        }
Beispiel #9
0
 public void StopJob(int jobId)
 {
     using (var ctx = new BatchJobDbContext())
     {
         try
         {
             var job = ctx.Jobs.Find(jobId);
             if (job != null)
             {
                 var scheduler = QuartzHelper.GetScheduler(job.JobGroup.Scheduler.Name, job.JobGroup.Scheduler.ThreadPoolSize, (int)job.JobGroup.Scheduler.Priority);
                 scheduler.PauseJob(job.Code, job.JobGroup.Name);
                 job.StopJob();
                 ctx.SaveChanges();
             }
         }
         catch (Exception ex)
         {
             throw ex;
         }
     }
 }
Beispiel #10
0
        public void ServiceRuntime()
        {
            var triggerDetails = TriggerRepository.GetCronTriggerDetails();
            var triggerDetailsSchedulerGroup = triggerDetails.GroupBy(a => new { a.SchedulerId, a.SchedulerName, a.SchedulerPriority, a.SchedulerThreadPoolSize }).ToDictionary(a => a.Key, a => a);

            //分组并创建调度器
            foreach (var schedulerGroupItem in triggerDetailsSchedulerGroup)
            {
                var scheduler = QuartzHelper.GetScheduler(schedulerGroupItem.Key.SchedulerName, schedulerGroupItem.Key.SchedulerThreadPoolSize, (int)schedulerGroupItem.Key.SchedulerPriority);
                foreach (var item in schedulerGroupItem.Value)
                {
                    //如果已经存在作业,需要先删除
                    var jobKey = new JobKey(item.JobCode, item.JobGroupName);
                    if (scheduler.CheckExists(jobKey))
                    {
                        scheduler.DeleteJob(jobKey);
                    }
                    var job = JobBuilder.Create <ExcuteJob>()
                              .WithIdentity(jobKey)
                              .Build();
                    //如果已经存在触发器,需要先删除
                    var triggerKey = new TriggerKey(item.TriggerCode);
                    if (scheduler.CheckExists(triggerKey))
                    {
                        scheduler.UnscheduleJob(triggerKey);
                    }
                    var trigger = TriggerBuilder.Create()
                                  .WithIdentity(triggerKey)
                                  .WithCronSchedule(item.CronExpression)
                                  .StartNow()
                                  .Build();
                    //调度作业
                    scheduler.ScheduleJob(job, trigger);
                }
            }
        }
Beispiel #11
0
        public async Task <ActionResult> SyncJobs(int ID = 0)
        {
            string _group = "";

            try
            {
                // get all jobs from db
                IEnumerable <Job> jobs = db.Jobs.Include("JobParams");

                Job jobToSync = jobs.Where(o => o.ID == ID).SingleOrDefault();

                // remove missing jobs first
                IScheduler sched = await QuartzHelper.GetScheduler();

                IReadOnlyCollection <string> jobGroups = await sched.GetJobGroupNames();

                foreach (string group in jobGroups)
                {
                    var groupMatcher = Quartz.Impl.Matchers.GroupMatcher <JobKey> .GroupEquals(group);

                    var jobKeys = sched.GetJobKeys(groupMatcher);
                    foreach (var jobKey in jobKeys.Result)
                    {
                        // clear all jobs
                        if ((ID == 0) || (jobKey.Name.ToLower().Trim() == jobToSync.JobName.ToLower().Trim() && group.ToLower().Trim() == jobToSync.GroupName.ToLower().Trim()))
                        {
                            foreach (ITrigger triger in sched.GetTriggersOfJob(jobKey).Result)
                            {
                                // remove schedulers
                                // sched.UnscheduleJob(triger.Key);
                            }

                            await sched.DeleteJob(jobKey);                             // remove it from the service
                        }
                    }
                }

                // add all jobs to service
                foreach (Job job in jobs.Where(o => o.Enabled && (ID == 0 || o.ID == ID)))
                {
                    JobDataMap jdata = new JobDataMap();
                    foreach (JobParam param in job.JobParams)
                    {
                        jdata.Add(new KeyValuePair <string, object>(param.ParamKey, param.ParamValue));
                    }

                    IJobDetail sjob = JobBuilder
                                      .Create(Type.GetType(job.JobType))
                                      .WithIdentity(job.JobName, job.GroupName)
                                      .WithDescription(job.Details)
                                      .UsingJobData(jdata)
                                      .Build();

                    ITrigger strigger = TriggerBuilder
                                        .Create()
                                        .WithIdentity(job.TriggerName, job.TriggerGroup)
                                        .WithPriority(job.Priority)
                                        .StartNow()
                                        .WithCronSchedule(job.CronExpression)
                                        .Build();

                    if (ID > 0)
                    {
                        _group = job.GroupName;
                    }

                    await sched.ScheduleJob(sjob, strigger);
                }
            }
            catch (System.Net.Sockets.SocketException)
            {
                return(RedirectToAction("Index", "Home", new { group = _group }));
            }
            catch (Quartz.SchedulerException)
            {
                TempData["ErrorDetails"] = @"There was an error syncing jobs! Please consider using the <a href=""" + Url.Action("SyncJobs", new { id = 0 }) + @""">Sync All</a> feature if you recently changed the job name, group, trigger or trigger group. This is normal behavior in this case";
                return(RedirectToAction("Index", "Home"));
            }

            return(RedirectToAction("Index", "Home", new { group = _group }));
        }