kbmMW Scheduler Tidbits #4

The kbmMW Scheduler schedules jobs. The jobs may be one time or recurrent jobs.

Some jobs may not be critical related to the exactness of their executing time on the mSec (like generating a report, doing a backup, importing some data and other such housekeeping and maintenance), while others may require as accurate as possible execution, for example sampling data, screen grabbing or stuff like that.

The scheduler can do both.

When defining a recurrent job, you typically also tells it with which interval it should run. The scheduler contains multiple fluent expressions to define the interval, like EveryMinute, EveryHour, EveryDay, EverySecond, EveryMSecond etc. which all takes floating point values.

Hence you can easily schedule (and activate) a job to run every 1.5 hour, by writing this:

Scheduler.Schedule(somejob).EveryHour(1.5).Activate;

kbmMW default assumes that jobs scheduled with an interval less than 2 seconds should be considered “precise” jobs, while others should be considered “relaxed” jobs.

A “Relaxed” job is a job, that is being assigned a thread from a thread pool, when it’s about to execute. If there are no threads available, the job is put on hold until a thread is free.

Thus Relaxed jobs often share one or a few threads to run, and thus are very gentle to the system resources. But the downside is that they may not execute completely on time. Their execution may be delayed a few mSecs to even minutes or hours, depending on the available number of threads in the relaxed thread pool, and the load of the scheduled relaxed jobs. Typically any delays will be minimal, but there is no guarantee.

A “Precise” job is a job that from time of defining the job, will have its own thread assigned to it, and thus will run independently of all other jobs.

As mentioned kbmMW automatically decides, upon scheduling time, which mode the job is to take, relaxed or precise. But you can override this manually by specifically stating either Relaxed or Precise as part of the fluent expression. E.g.

// Schedule a job in its own thread, running every 1.5 hour.
Scheduler.Schedule(somejob).EveryHour(1.5).Precise.Activate;
// Schedule a job to be run every 100 msecs, from a shared thread from the relaxed threadpool.
Scheduler.Schedule(somejob).EveryMSecond(100).Relaxed.Activate;

In a previous Scheduler Tidbit blog post, I mentioned the new CronTab scheduling style. Since UNIX cron only has an interval resolution of 1 minute, scheduling such a job using kbmMW’s scheduler also will predefine the use of a one minute interval, and thus all cron jobs will default be scheduled as relaxed jobs.

However you can choose a different interval resolution by using any of the Every…. fluent expressions before the Cron expression. Eg.

// This will schedule a job to be run every minute from 5:00 to 5:59 every day.
// The job will default be considered a relaxed scheduled job.
Scheduler.Schedule(somejob).Cron('* 5 * * *').Activate;

// This will schedule a job to be run every second from 5:00 to 5:59 every day.
// The job will default be considered a precise scheduled job.
Scheduler.Schedule(somejob).EverySecond(1).Cron('* 5 * * *').Activate;

If you state the Every… expression after the Cron expression, it will add to the default 1 minute cron schedule. Eg.

// This will schedule a job to be run every 1.5 minute from 5:00 to 5:59 every day.
Scheduler.Schedule(somejob).Cron('* 5 * * *').EverySecond(30).Activate;