-
Notifications
You must be signed in to change notification settings - Fork 16
Scheduler
Jobs may be scheduled using cron expressions (powered by the Cronos library). The scheduler and consumers are separate; schedulers don't process any work, they queue it for processing by a consumer. The standard Method consumers are used to process work enqueued by a scheduler / schedule.
Multiple schedulers with the same schedule may be run if needed for redundancy. However, it's important that the clocks on the machines are in sync, or that the same time provider is injected into the schedulers and consumers. See Scheduler Redundancy for more information on this.
Generally speaking, you may get funny results if you are using multiple machines and the clocks are not in sync. The server based transports tend to provide solutions for this if you can't sync the clocks of the local machines; see Time Configuration.
See Job Scheduler for cron expression format and the IJobSchedule API.
[Scheduler]
The scheduler and container must be kept in scope until you are done scheduling work or shutting down. No work will be queued if the scheduler is disposed or falls out of scope.
var queueConnection = new QueueConnection(queueName, connectionString);
using (var jobContainer = new JobSchedulerContainer(QueueCreation))
{
using (var scheduler = jobContainer.CreateJobScheduler())
{
//events for job added, job add exception and job add failure
scheduler.OnJobQueueException += SchedulerOnJobQueueException;
scheduler.OnJobQueue += SchedulerOnJobEnQueue;
scheduler.OnJobNonFatalFailureQueue += SchedulerOnJobNonFatalFailureEnQueue;
// Every 5 seconds (6-field cron with seconds column)
scheduler.AddUpdateJob<SqlServerMessageQueueInit, SqlServerJobQueueCreation>(
"test job1",
new QueueConnection("sampleSQL", connectionString),
"*/5 * * * * *",
(message, workerNotification) => Console.WriteLine(message.MessageId.Id.Value));
// Every 15 seconds
scheduler.AddUpdateJob<RedisQueueInit, RedisJobQueueCreation>(
"test job2",
new QueueConnection("sampleRedis", connectionStringRedis),
"*/15 * * * * *",
(message, workerNotification) => Task.Delay(20000).Wait());
// Every 5 seconds
scheduler.AddUpdateJob<SqLiteMessageQueueInit, SqliteJobQueueCreation>(
"test job3",
new QueueConnection("sampleSQLite", connectionStringSqlite),
"*/5 * * * * *",
(message, workerNotification) => Console.WriteLine(message.MessageId.Id.Value));
//start may be called before or after adding jobs
scheduler.Start();
Console.WriteLine("Running - press any key to stop");
Console.ReadKey(true);
}
}
private static void SchedulerOnJobNonFatalFailureEnQueue(IScheduledJob scheduledJob, IJobQueueOutputMessage jobQueueOutputMessage)
{
}
private static void SchedulerOnJobEnQueue(IScheduledJob scheduledJob, IJobQueueOutputMessage jobQueueOutputMessage)
{
}
private static void SchedulerOnJobQueueException(IScheduledJob scheduledJob, Exception error)
{
}To consume / process scheduled jobs, a Method Consumer is used.
To stop the scheduler, call dispose on it. This will stop it from queuing more work, but will not stop jobs in progress, as those are processed by separate consumers.
scheduler.Dispose();Calling dispose on the scheduler container will dispose all schedulers created by that container as well.
Dispose is blocking operation. Depending on your configuration settings it may take a while to return.
For any issues please use the GitHub issues