Job Scheduling

Introduction

Configuring a cron job or windows task is common for web apps. Spark utilizes Coravel’s scheduler to accomplish this.

Setup

Spark registers the scheduler in the Application.Startup.AppServiceRegistration.AddAppServices() method for you.

services.AddScheduler();

Your individual jobs also need to be registered in the service container. This can be done in the Application.Startup.AppServiceRegistration.AddJobServices() method.

private static IServiceCollection AddJobServices(this IServiceCollection services)
{
    services.AddTransient<ExampleJob>();
    return services;
}

Defining Schedules

Jobs are scheduled in the Application.Startup.Scheduler.RegisterScheduledJobs() method.

Inside of that method, the UseScheduler() method is called. Your scheduled jobs and their intervals are defined inside of it.

public static class Scheduler
{
    public static IServiceProvider RegisterScheduledJobs(this IServiceProvider services)
    {
        services.UseScheduler(scheduler =>
        {
            // example scheduled job
            scheduler
               .Schedule<ExampleJob>()
               .EveryFiveMinutes();
        });
        return services;
    }
}

Scheduling Jobs With Parameters

If you have a job that expects parameters, you can use the ScheduleWithParams<T>() method.

For example, if you had a job that accepted a string parameter:

public class ExampleJob : IInvocable
{
    private string _someParm;

    public WeatherJob(string param)
    {
        this._someParm = param;
    }

    public Task Invoke()
    {
        // lookup states weather data
        return Task.CompletedTask;
    }
}

You could pass the parameter like this:

scheduler
    .ScheduleWithParams<ExampleJob>("some string param")
    .Hourly();

Intervals

When scheduling jobs, you will also need to specify the interval in which they run. There are many schedule intervals that you may assign to a job:

// Run the job using a Cron expression
.Cron("* * * * *")

// Run the job every second
.EverySecond()

// Run the job every five seconds            
.EveryFiveSeconds()

// Run the job every ten seconds
.EveryTenSeconds()

// Run the job every fifteen seconds
.EveryFifteenSeconds()

// Run the job every thirty seconds
.EveryThirtySeconds()

// 	Run the job every 3 seconds
.EverySeconds(3)

// Run the job once a minute
.EveryMinute()

// Run the job every five minutes
.EveryFiveMinutes()

// Run the job every ten minutes
.EveryTenMinutes()

// Run the job every fifteen minutes
.EveryFifteenMinutes()

// Run the job every thirty minutes
.EveryThirtyMinutes()

// Run the job every hour
.Hourly()

// Run the job at 12 minutes past every hour
.HourlyAt(12)

// 	Run the job once a day at midnight
.Daily()

// Run the job once a day at 1 p.m. UTC
.DailyAtHour(13)

// Run the job once a day at 1:30 p.m. UTC
.DailyAt(13, 30)

// Run the job once a week
.Weekly()

// Run the job on the first of every month midnight
.Monthly()

The scheduler uses UTC time by default

Day Contraints

Once you specify an interval, you can also restrict what day’s the job is allowed to run on.

.Monday()
.Tuesday()
.Wednesday()
.Thursday()
.Friday()
.Saturday()
.Sunday()
.Weekday()
.Weekend()

You can also chain these methods.

For instance, the following would mean a job runs every hour on Monday and Wednesdays:

scheduler.Schedule<ExampleJob>()
    .Hourly()
    .Monday()
    .Wednesday();

Timezones

By default, the scheduler uses UTC time to trigger jobs.

If you would like to run your schedules against a particular time zone, use the Zoned() method.

scheduler.Schedule<ExampleJob>()
    .Hourly()
    .Zoned(TimeZoneInfo.Local);

Preventing Job Overlap

By default, scheduled jobs will run even if the previous instance of the job is still running.

To prevent this, you may use the PreventOverlapping() method.

scheduler
    .Schedule<ExampleJob>()
    .EveryMinute()
    .PreventOverlapping("SomeJob");

This method takes in one paramater, a unique string key. The key is whats used to determine if the job has an instance running or not when the scheduler goes to trigger a job.