Skip to content

Scheduler

Brian Lehnen edited this page Dec 8, 2020 · 2 revisions

Job Scheduler

Jobs may be scheduled using Schyntax . The scheduler and consumers are seperate; schedulers don't process any work, they queue it for processing by a consumer. The standard LINQ consumers are used to process work enqueued by a scheduler / schedule.

Any LINQ statement that a linq producer supports can be scheduled using the scheduler.

Multiple schedulers with the same scheduler may be ran 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 the WIKI 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 the WIKI.

See Schyntax for event scheduling format.

[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 += SchedulerOnOnJobQueueException;
        scheduler.OnJobQueue += SchedulerOnOnJobEnQueue;
        scheduler.OnJobNonFatalFailureQueue += SchedulerOnOnJobNonFatalFailureEnQueue;

        scheduler.AddUpdateJob<SqlServerMessageQueueInit, SqlServerJobQueueCreation>("test job1",
        new QueueConnection("sampleSQL", connectionStringSqlite),
            "sec(0,5,10,15,20,25,30,35,40,45,50,55)",
    		(message, workerNotification) => Console.WriteLine(message.MessageId.Id.Value));

       scheduler.AddUpdateJob<RedisQueueInit, RedisJobQueueCreation>("test job2",
       new QueueConnection("sampleRedis", "192.168.0.212"),
       "second(0,15,30,45)",
     	new LinqExpressionToRun("(message, workerNotification) => System.Threading.Thread.Sleep(20000)"));

        scheduler.AddUpdateJob<SqLiteMessageQueueInit, SqliteJobQueueCreation>("test job3",
        new QueueConnection("sampleSQL", connectionStringSqlite),
        "sec(0,5,10,15,20,25,30,35,40,45,50,55)",
        (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 SchedulerOnOnJobNonFatalFailureEnQueue(IScheduledJob scheduledJob, IJobQueueOutputMessage jobQueueOutputMessage)
{
}

private static void SchedulerOnOnJobEnQueue(IScheduledJob scheduledJob, IJobQueueOutputMessage jobQueueOutputMessage)
{
}

private static void SchedulerOnOnJobQueueException(IScheduledJob scheduledJob, Exception error)
{
}

To consume / process scheduled jobs, a Linq Consumer is used

Stopping the scheduler

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 seperate 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.

Clone this wiki locally