Repository navigation
Scheduler
You can schedule jobs with cron expressions, which the Cronos library parses. The scheduler and consumers are separate. Schedulers do not process any work. They queue the work, and a consumer processes it. The standard Method consumers process the work that a scheduler or schedule enqueues.
You can run multiple schedulers with the same schedule if you need redundancy. But it is important that the clocks on the machines are in sync, or that you inject the same time provider into the schedulers and consumers. See Scheduler Redundancy for more information.
If you use multiple machines and the clocks are not in sync, you may get funny results. If you cannot sync the clocks of the local machines, the server based transports tend to provide solutions for this. See Time Configuration.
See Job Scheduler for cron expression format and the IJobSchedule API.
[Scheduler]
You must keep the scheduler and container in scope until you are done scheduling work or shutting down. If the scheduler is disposed or falls out of scope, no work is queued.
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 and process scheduled jobs, use a Method Consumer.
To stop the scheduler, call dispose on it. The scheduler then stops queuing more work. It does not stop jobs in progress, because separate consumers process those jobs.
scheduler.Dispose();If you call dispose on the scheduler container, it also disposes all schedulers that it created.
Dispose is a blocking operation. Depending on your configuration settings, it may take a while to return.
For any issues please use the GitHub issues