public void TestTriggerFinalized() { Qtz205TriggerListener triggerListener = new Qtz205TriggerListener(); Qtz205ScheListener schedulerListener = new Qtz205ScheListener(); NameValueCollection props = new NameValueCollection(); props["quartz.scheduler.idleWaitTime"] = "1500"; props["quartz.threadPool.threadCount"] = "2"; IScheduler scheduler = new StdSchedulerFactory(props).GetScheduler(); scheduler.ListenerManager.AddSchedulerListener(schedulerListener); scheduler.ListenerManager.AddTriggerListener(triggerListener); IJobDetail job = JobBuilder.Create<Qtz205Job>().WithIdentity("test").Build(); ITrigger trigger = TriggerBuilder.Create() .WithIdentity("test") .WithSchedule(SimpleScheduleBuilder.RepeatSecondlyForTotalCount(3)) .Build(); scheduler.ScheduleJob(job, trigger); scheduler.Start(); Thread.Sleep(5000); scheduler.Shutdown(true); Assert.AreEqual(2, jobExecutionCount); Assert.AreEqual(3, triggerListener.FireCount); Assert.AreEqual(1, schedulerListener.TriggerFinalizedCount); }
public override bool OnStart() { Trace.WriteLine("WorkerRole1 Run", "Information"); var properties = new NameValueCollection(); properties["quartz.jobStore.type"] = "Quartz.Impl.AdoJobStore.JobStoreTX, Quartz"; properties["quartz.jobStore.dataSource"] = "default"; properties["quartz.jobStore.clustered"] = "true"; properties["quartz.jobStore.selectWithLockSQL"] = string.Format(CultureInfo.InvariantCulture, "SELECT * FROM {0}{1} WHERE {2} = {3} AND {4} = @lockName", new object[] { "{0}", "LOCKS", "SCHED_NAME", "{1}", "LOCK_NAME" }); properties["quartz.jobStore.acquireTriggersWithinLock"] = "true"; properties["quartz.scheduler.instanceId"] = "AUTO"; properties["quartz.threadPool.threadCount"] = "1"; properties["quartz.jobStore.tablePrefix"] = "Scheduling."; properties["quartz.dataSource.default.connectionString"] = @"Server = (local)\sqlexpress; Database = DB; Integrated Security = True"; properties["quartz.dataSource.default.provider"] = "SqlServer-20"; var scheduler = new StdSchedulerFactory(properties).GetScheduler(); scheduler.Clear(); var triggerKey = new TriggerKey("t1"); var trigger = scheduler.GetTrigger(triggerKey); var jobBuilder = JobBuilder.Create<Job>(); var job = jobBuilder.Build(); var t = scheduler.GetTrigger(new TriggerKey("t1")); trigger = new SimpleTriggerImpl("t1", 100000, TimeSpan.FromSeconds(5)); scheduler.ScheduleJob(job, trigger); scheduler.Start(); return base.OnStart(); }
static void Main(string[] args) { var scheduler = new StdSchedulerFactory().GetScheduler(); // add scheduler listener scheduler.ListenerManager.AddSchedulerListener(new SchedulerListener()); // add global job listener scheduler.ListenerManager.AddJobListener(new JobListener(), GroupMatcher<JobKey>.AnyGroup()); // add global trigger listener scheduler.ListenerManager.AddTriggerListener(new TriggerListener(), GroupMatcher<TriggerKey>.AnyGroup()); // add jobs & triggers scheduler.ScheduleJob( JobBuilder.Create<ExampleJob>().WithIdentity("MyJob", "MyJobGroup").Build(), TriggerBuilder.Create().WithIdentity("MyTrigger", "MyTriggerGroup") .WithSimpleSchedule(s => s.WithIntervalInSeconds(2).RepeatForever()).Build()); scheduler.Start(); // start scheduler Thread.Sleep(5000); // sleep the main thread (Quartz will fire the job 3x on other threads) scheduler.Shutdown(true); // stop the scheduler Console.ReadLine(); }
public void TestNoConcurrentExecOnSameJobWithBatching() { DateTime startTime = DateTime.Now.AddMilliseconds(300).ToUniversalTime(); // make the triggers fire at the same time. IJobDetail job1 = JobBuilder.Create<TestJob>().WithIdentity("job1").Build(); ITrigger trigger1 = TriggerBuilder.Create().WithSimpleSchedule().StartAt(startTime).Build(); ITrigger trigger2 = TriggerBuilder.Create().WithSimpleSchedule().StartAt(startTime).ForJob(job1).Build(); NameValueCollection props = new NameValueCollection(); props["quartz.scheduler.idleWaitTime"] = "1500"; props["quartz.scheduler.batchTriggerAcquisitionMaxCount"] = "2"; props["quartz.threadPool.threadCount"] = "2"; IScheduler scheduler = new StdSchedulerFactory(props).GetScheduler(); scheduler.ListenerManager.AddJobListener(new TestJobListener(2)); scheduler.ScheduleJob(job1, trigger1); scheduler.ScheduleJob(trigger2); scheduler.Start(); barrier.WaitOne(); scheduler.Shutdown(true); Assert.AreEqual(2, jobExecDates.Count); Assert.Greater(jobExecDates[1] - jobExecDates[0], jobBlockTime); }
public void TestSameDefaultPriority() { NameValueCollection config = new NameValueCollection(); config["quartz.threadPool.threadCount"] = "1"; config["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool"; IScheduler sched = new StdSchedulerFactory(config).GetScheduler(); DateTime n = DateTime.UtcNow; DateTime cal = new DateTime(n.Year, n.Month, n.Day, n.Hour, n.Minute, 1, n.Millisecond); IMutableTrigger trig1 = new SimpleTriggerImpl("T1", null, cal); IMutableTrigger trig2 = new SimpleTriggerImpl("T2", null, cal); JobDetailImpl jobDetail = new JobDetailImpl("JD", null, typeof (TestJob)); sched.ScheduleJob(jobDetail, trig1); trig2.JobKey = new JobKey(jobDetail.Key.Name); sched.ScheduleJob(trig2); sched.Start(); Thread.Sleep(2000); Assert.AreEqual("T1T2", result.ToString()); sched.Shutdown(); }
public virtual void PauseScheduler() { IScheduler sched = null; "Given a scheduler that is started".Given(() => {//arrange sched = new StdSchedulerFactory(_props).GetScheduler(); sched.Start(); }); "When calling pause all".When(() => {//act sched.PauseAll(); sched.GetDescription().LogMe(LogLevel.Debug); }); "Then ".Then(() => { sched.IsStarted.Should().BeTrue("it is still running."); var groupNames = sched.GetJobGroupNames(); if (groupNames.Count == 0) Log.Info("No job groups found to pause."); foreach (var groupName in groupNames) { Log.InfoFormat("GroupName = {0}", groupName); sched.IsJobGroupPaused(groupName).Should().BeTrue("pause all should pause all job groups"); } }); }
static void Main(string[] args) { var settings = SettingsFactory.Create<PingerSettings>(ConfigurationManager.AppSettings["settings"]); if(settings != null && settings.Pings != null && settings.Pings.Count > 0) { // construct a scheduler var scheduler = new StdSchedulerFactory().GetScheduler(); scheduler.Start(); settings.Pings.ForEach(p=> { var job = JobBuilder.Create<Pinger>() .UsingJobData("urls",string.Join(",",p.Urls)) .Build(); var trigger = TriggerBuilder.Create() .WithSimpleSchedule(x => x.WithInterval(TimeSpan.FromMinutes(p.Interval)).RepeatForever()) .Build(); scheduler.ScheduleJob(job, trigger); }); } }
static IScheduler SetupIndexingSchedule() { var scheduler = new StdSchedulerFactory().GetScheduler(); scheduler.Start(); var job = JobBuilder.Create<IndexAgent>().WithIdentity("Indexer").Build(); var trigger = TriggerBuilder.Create() .StartNow() .WithSimpleSchedule(x => x.WithIntervalInHours(1).RepeatForever()) .Build(); scheduler.ScheduleJob(job, trigger); return scheduler; }
public override bool OnStart() { CrashingWorker.xx += WorkerRole_xx; Trace.WriteLine("WorkerRole1 Run", "Information"); var properties = new NameValueCollection(ConfigurationManager.GetSection("quartz") as NameValueCollection); var c = string.Join(".", new string[] { StdSchedulerFactory.PropertyDataSourcePrefix, properties["quartz.jobStore.dataSource"], StdSchedulerFactory.PropertyDataSourceConnectionString }); properties[c] = Database.SchedulerConnection; var scheduler = new StdSchedulerFactory(properties).GetScheduler(); scheduler.Start(); return base.OnStart(); }
public void TestJobInterruption() { // create a simple scheduler NameValueCollection config = new NameValueCollection(); config["quartz.scheduler.instanceName"] = "InterruptableJobTest_Scheduler"; config["quartz.scheduler.instanceId"] = "AUTO"; config["quartz.threadPool.threadCount"] = "2"; config["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool"; IScheduler sched = new StdSchedulerFactory(config).GetScheduler(); sched.Start(); // add a job with a trigger that will fire immediately IJobDetail job = JobBuilder.Create<TestInterruptableJob>() .WithIdentity("j1") .Build(); ITrigger trigger = TriggerBuilder.Create() .WithIdentity("t1") .ForJob(job) .StartNow() .Build(); sched.ScheduleJob(job, trigger); sync.WaitOne(); // make sure the job starts running... IList<IJobExecutionContext> executingJobs = sched.GetCurrentlyExecutingJobs(); Assert.AreEqual(1, executingJobs.Count, "Number of executing jobs should be 1 "); IJobExecutionContext jec = executingJobs[0]; bool interruptResult = sched.Interrupt(jec.FireInstanceId); sync.WaitOne(); // wait for the job to terminate Assert.IsTrue(interruptResult, "Expected successful result from interruption of job "); Assert.IsTrue(TestInterruptableJob.interrupted, "Expected interrupted flag to be set on job class "); sched.Clear(); sched.Shutdown(); }
static void Main(string[] args) { var properties = new NameValueCollection(); properties["quartz.threadPool.threadCount"] = "1"; const string cronExpression = "0/30 * * * * ?"; var scheduler = new StdSchedulerFactory(properties).GetScheduler(); var job = new JobDetail("NomeJob", "GrupoJob", typeof(BillingJob)); var trigger = new CronTrigger("NomeTrigger", "GrupoTrigger", "NomeJob", "GrupoJob", cronExpression); scheduler.ScheduleJob(job, trigger); scheduler.Start(); Console.ReadKey(); scheduler.Shutdown(); }
static void Main(string[] args) { //ScheduledJob.Run(); // TODO: Initialiaze Scheduler var scheduler = new StdSchedulerFactory().GetScheduler(); scheduler.Start(); var jobDetail = JobBuilder.Create<ExampleJob>() .WithIdentity(new JobKey("ExampleJob")) .Build(); jobDetail.JobDataMap.Put("DataKey", "Passed value"); jobDetail.JobDataMap.Put("FilePath", ConfigurationManager.AppSettings["FilePath"] as string); // Let's create a trigger that fires immediately ICronTrigger trigger = TriggerBuilder.Create() .WithIdentity("WriteHelloToLog", "IT") .WithCronSchedule("0 0/1 * 1/1 * ? *") //-> visit http://www.cronmaker.com/ Queues the job every minute .StartAt(DateTime.UtcNow) .WithPriority(1) .Build() as ICronTrigger; ITrigger dailyTrigger = TriggerBuilder.Create() .WithDailyTimeIntervalSchedule(schedBuilder => schedBuilder .WithIntervalInMinutes(30) .OnDaysOfTheWeek(new[] { DayOfWeek.Monday, DayOfWeek.Tuesday, DayOfWeek.Wednesday, DayOfWeek.Thursday, DayOfWeek.Friday }) .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(9, 30)) .EndingDailyAt(TimeOfDay.HourAndMinuteOfDay(19, 30))) .Build(); scheduler.ScheduleJob(jobDetail, trigger); scheduler.TriggerJob(new JobKey("ExampleJob")); }
private void StartCronJobs() { IScheduler sched; IJobDetail jobDetail; ITrigger trigger; var cronJobs = new r3mus_DBEntities().CRONJobs; //.Where(cronJob => cronJob.Enabled == true); cronJobs.ToList().ForEach(cronJob => { sched = new StdSchedulerFactory().GetScheduler(); sched.Start(); jobDetail = JobBuilder.Create(Type.GetType(string.Concat("r3mus.CRONJobs.", cronJob.JobName))) .WithIdentity(string.Format("{0}Instance", cronJob.JobName), string.Format("{0}Group", cronJob.JobName)) .Build(); trigger = TriggerBuilder.Create() .WithIdentity(string.Format("{0}Trigger", cronJob.JobName), string.Format("{0}TriggerGroup", cronJob.JobName)) .StartNow() .WithSimpleSchedule(x => x.WithIntervalInMinutes(cronJob.Schedule).RepeatForever()) .Build(); sched.ScheduleJob(jobDetail, trigger); }); sched = new StdSchedulerFactory().GetScheduler(); sched.Start(); //jobDetail = JobBuilder.Create(Type.GetType("r3mus.CRONJobs.PreloadInfo")) // .WithIdentity("PreloadInfoInstance", "PreloadInfoGroup") // .Build(); //trigger = TriggerBuilder.Create() // .WithIdentity("PreloadInfoTrigger", "PreloadInfoTriggerGroup") // .StartNow() // .WithSimpleSchedule(x => x.WithIntervalInMinutes(15).RepeatForever()) // .Build(); //sched.ScheduleJob(jobDetail, trigger); }
protected override void OnStart(string[] args) { //ScheduledJob.Run(); // TODO Initialiaze Scheduler var scheduler = new StdSchedulerFactory().GetScheduler(); scheduler.Start(); var jobDetail = JobBuilder.Create<HelloWorldJob>() .WithIdentity(new JobKey("HelloWorldJob")) .Build(); jobDetail.JobDataMap.Put("DataKey", "Passed value"); jobDetail.JobDataMap.Put("FilePath", System.Configuration.ConfigurationManager.AppSettings["FilePath"] as string); // Let's create a trigger that fires immediately var trigger = (ICronTrigger)TriggerBuilder.Create() .WithIdentity("WriteHelloToLog", "IT") .WithCronSchedule("0 0/1 * 1/1 * ? *") // visit http://www.cronmaker.com/ Queues the job every minute .StartAt(DateTime.UtcNow) .WithPriority(1) .Build(); scheduler.ScheduleJob(jobDetail, trigger); scheduler.TriggerJob(new JobKey("HelloWorldJob")); }
public virtual void ShutdownScheduler() { IScheduler sched = null; "Given a scheduler that is started".Given(() => {//arrange sched = new StdSchedulerFactory(_props).GetScheduler(); sched.Start(); }); "When calling shutdown".When(() => {//act sched.Shutdown(); sched.GetDescription().LogMe(LogLevel.Debug); }); "Then ".Then(() => sched.IsShutdown.Should().BeTrue("it is shutdown")); //Note: IsStarted, and InStandbyMode are also true. }
static void Main(string[] args) { var scheduler = new StdSchedulerFactory().GetScheduler(); scheduler.Start(); }
/// <summary> /// Executes this instance. /// </summary> public void Execute() { using ( var rockContext = new RockContext() ) { var jobService = new ServiceJobService( rockContext ); ServiceJob job = jobService.Get( JobId ); if ( job != null ) { try { // create a scheduler var scheduleConfig = new System.Collections.Specialized.NameValueCollection(); scheduleConfig.Add( "org.quartz.scheduler.instanceName", "RunNow" ); var sched = new StdSchedulerFactory( scheduleConfig ).GetScheduler(); // create the quartz job and trigger IJobDetail jobDetail = jobService.BuildQuartzJob( job ); var jobTrigger = TriggerBuilder.Create() .WithIdentity( job.Guid.ToString(), job.Name ) .StartNow() .Build(); // schedule the job sched.ScheduleJob( jobDetail, jobTrigger ); // set up the listener to report back from the job when it completes sched.ListenerManager.AddJobListener( new RockJobListener(), EverythingMatcher<JobKey>.AllJobs() ); // start the scheduler sched.Start(); // Wait 10secs to give job chance to start Thread.Sleep( new TimeSpan( 0, 0, 10 ) ); // stop the scheduler when done with job sched.Shutdown( true ); } catch ( Exception ex ) { // create a friendly error message string message = string.Format( "Error loading the job: {0}. Ensure that the correct version of the job's assembly ({1}.dll) in the websites App_Code directory. \n\n\n\n{2}", job.Name, job.Assembly, ex.Message ); job.LastStatusMessage = message; job.LastStatus = "Error Loading Job"; rockContext.SaveChanges(); } } } }
/// <summary> /// Executes this instance. /// </summary> public void Execute() { using ( var rockContext = new RockContext() ) { var jobService = new ServiceJobService( rockContext ); ServiceJob job = jobService.Get( JobId ); if ( job != null ) { try { // create a scheduler specific for the job var scheduleConfig = new System.Collections.Specialized.NameValueCollection(); var runNowSchedulerName = ( "RunNow:" + job.Guid.ToString( "N" ) ).Truncate( 40 ); scheduleConfig.Add( StdSchedulerFactory.PropertySchedulerInstanceName, runNowSchedulerName ); var schedulerFactory = new StdSchedulerFactory( scheduleConfig ); var sched = new StdSchedulerFactory( scheduleConfig ).GetScheduler(); if (sched.IsStarted) { // the job is currently running as a RunNow job return; } // create the quartz job and trigger IJobDetail jobDetail = jobService.BuildQuartzJob( job ); var jobTrigger = TriggerBuilder.Create() .WithIdentity( job.Guid.ToString(), job.Name ) .StartNow() .Build(); // schedule the job sched.ScheduleJob( jobDetail, jobTrigger ); // set up the listener to report back from the job when it completes sched.ListenerManager.AddJobListener( new RockJobListener(), EverythingMatcher<JobKey>.AllJobs() ); // start the scheduler sched.Start(); // Wait 10secs to give job chance to start Thread.Sleep( new TimeSpan( 0, 0, 10 ) ); // stop the scheduler when done with job sched.Shutdown( true ); } catch ( Exception ex ) { // create a friendly error message ExceptionLogService.LogException( ex, null ); string message = string.Format( "Error doing a 'Run Now' on job: {0}. \n\n{2}", job.Name, job.Assembly, ex.Message ); job.LastStatusMessage = message; job.LastStatus = "Error Loading Job"; rockContext.SaveChanges(); } } } }