Skip to content

Instantly share code, notes, and snippets.

@logicbomb
Created November 12, 2012 15:54
Show Gist options
  • Save logicbomb/4060140 to your computer and use it in GitHub Desktop.
Save logicbomb/4060140 to your computer and use it in GitHub Desktop.
Process doesn't exit after delayed start
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Quartz;
using Quartz.Impl;
namespace quartz.test
{
class Program
{
private const string TriggerName = "trigger";
private const string JobName = "job";
static void Main(string[] args)
{
var factory = new StdSchedulerFactory();
var scheduler = factory.GetScheduler();
var trigger = TriggerBuilder
.Create()
.WithSimpleSchedule(s => s.WithIntervalInHours(24).WithMisfireHandlingInstructionFireNow().RepeatForever())
.WithIdentity(TriggerName);
var detail = JobBuilder
.Create<TestJob>()
.WithIdentity(JobName)
.StoreDurably(false)
.Build();
scheduler.ScheduleJob(detail, trigger.Build());
var next1201am = getTimeSpanUntil1201a();
// Comment out this line, and uncomment either line
scheduler.StartDelayed(next1201am);
// after the timespan has elapsed the job will exit
// scheduler.StartDelayed(new TimeSpan(0, 0, 7));
// Both of the following schedulers shut down appropriately
// I think it's because the job has executed at least once
//scheduler.StartDelayed(new TimeSpan(0, 0, 2));
//scheduler.Start();
Thread.Sleep(3000);
scheduler.DeleteJob(new JobKey(JobName));
scheduler.UnscheduleJob(new TriggerKey(TriggerName));
scheduler.Shutdown(false);
Console.WriteLine("Why am I still here?");
}
private static TimeSpan getTimeSpanUntil1201a()
{
var tomorrow = DateTime.Today.AddDays(1);
var next1201Am = (tomorrow - DateTime.Now).Add(new TimeSpan(0, 0, 1, 0));
Console.WriteLine("Next 12:01 am occurs in {0} minutes", next1201Am.TotalMinutes);
return next1201Am;
}
}
public class TestJob : IJob
{
public void Execute(IJobExecutionContext context)
{
Console.WriteLine("This is a test job");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment