Scheduled Jobs
Scheduled jobs can execute code at regular intervals or at specific times on designated days.
Create a Scheduled Job
To create a scheduled job, define a new class that is tagged with the ServiceJob
attribute and implements the IServiceJob
interface.
Project.Services / Jobs / MyJob.cs
The ServiceJob
attribute takes the following parameters.
- Job Name: The name of the job as it will appear in the Admin Dashboard.
- Queue: A queue executes one job at a time. Multiple jobs can run in parallel if they are assigned to different queues.
- TimeSpan: When the JobSchedule is set to interval, this defines the time between executions. For example, a TimeSpan of "00:05:00" will trigger the job every 5 minutes. When JobSchedule is set to TimeOfDay, this represents the specific execution time in UTC. For instance, a TimeSpan of "05:00:00" will execute the job at 5:00 AM UTC.
- Job Schedule: Defines when the job runs and can be either Interval or TimeOfDay.
- Days of Week: Flags that specify which days of the week the job should execute.
Time of day example
Below is an example of a job that executes on Monday and Wednesday at 3:00 AM UTC:
Project.Services / Jobs / MyJob.cs
Execute on one server only
In a multi-server environment, jobs execute on all servers by default. To ensure a job runs on only a single server, use the JobServer
attribute with ExecuteJobOn.One
.
Project.Services / Jobs / MyJob.cs
When JobServer is set to ExecuteJobOn.One and no specific server is specified, the job will always execute on the server whose name comes first alphabetically. For example, if there are two servers named Alpha and Beta, the job will always run on Alpha because its name appears first in alphabetical order.
Execute on specific server only
In a multi-server environment, to ensure a job runs only on a specific server, provide the server name as the second parameter in the JobServer
attribute.
Project.Services / Jobs / MyJob.cs
To set the name of the server, use the SERVER_NAME environment variable.