ballerina/task

Overview

This module provides APIs to schedule a Ballerina job either once or periodically and to manage the execution of those jobs.

Jobs and Scheduling

Every scheduling job in Ballerina needs to be represented by a Job object. Therefore, a job class with your custom logic needs to be created to execute it when the task is triggered.

The task package has the following two scheduling systems to schedule the job:

  • One-time job execution
  • Frequency-based job execution

One-time Job Execution

This API provides the functionality to schedule a job at a specified time.

The following code snippet shows how to schedule a one-time job.

1class Job {
2
3 *task:Job;
4 string msg;
5
6 public function execute() {
7 io:println(self.msg);
8 }
9
10 isolated function init(string msg) {
11 self.msg = msg;
12 }
13}
14
15time:ZoneOffset zoneOffset = {
16 hours: 5,
17 minutes: 30,
18 seconds: <decimal>0.0
19};
20time:Civil time = {
21 year: 2021,
22 month: 4,
23 day: 13,
24 hour: 4,
25 minute: 50,
26 second: 50.52,
27 timeAbbrev: "Asia/Colombo",
28 utcOffset: zoneOffset
29};
30
31task:JobId result = check task:scheduleOneTimeJob(new Job("Hi"), time);
Frequency-based Job Execution

This API provides the functionality to schedule jobs on a specific interval either once or periodically by configuring the configuration such as start time, end time, and maximum count.

The following code snippet shows how to schedule a recurring job by using this API.

1class Job {
2
3 *task:Job;
4 string msg;
5
6 public function execute() {
7 io:println(self.msg);
8 }
9
10 isolated function init(string msg) {
11 self.msg = msg;
12 }
13}
14
15time:ZoneOffset zoneOffset = {
16 hours: 5,
17 minutes: 30
18};
19time:Civil time = {
20 year: 2021,
21 month: 3,
22 day: 31,
23 hour: 4,
24 minute: 50,
25 second: 50.52,
26 timeAbbrev: "Asia/Colombo",
27 utcOffset: zoneOffset
28};
29
30task:JobId result = check task:scheduleJobRecurByFrequency(new Job("Hi"), 2.5, maxCount = 10, startTime = time);

For information on the operations, which you can perform with the regex module, see the below Functions.

Functions

[10]

configureWorkerPool

Configure the scheduler worker pool.

getRunningJobs

Gets all the running jobs.

getTimeInMillies

Gets time in milliseconds of the given time:Civil.

pauseAllJobs

Pauses all the jobs.

pauseJob

Pauses the particular job.

resumeAllJobs

Resumes all the jobs.

resumeJob

Resumes the particular job.

scheduleJobRecurByFrequency

Schedule the recurring task:Job according to the given duration.

scheduleOneTimeJob

Schedule the given task:Job for the given time.

unscheduleJob

Unschedule the task:Job, which is associated with the given job ID.

Object Types

[1]

Job

The Ballerina Job object provides the abstraction for a job instance, which schedules to execute periodically.

Records

[2]

JobId

A read-only record consisting of a unique identifier for a created job.

TaskPolicy

Policies related to a trigger.

Enums

[2]

ErrorPolicy

Possible options for the ErrorPolicy.

WaitingPolicy

Possible options for the WaitingPolicy.

Errors

[1]

Error

Represents the error type of the ballerina/task module.