ballerina/task2.0.0-alpha9
Package Overview
This package provides the functionality to schedule a Ballerina job and manages the execution of Ballerina jobs either once or periodically.
Jobs and Scheduling
Every scheduled job in Ballerina is represented by a Job object. You need to create a jobs class with custom logic 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 date.
The following code snippet shows how to schedule a one-time job.
1class Job {23 *task:Job;4 string msg;56 public function execute() {7 io:println(self.msg);8 }910 isolated function init(string msg) {11 self.msg = msg;12 }13}1415time:ZoneOffset zoneOffset = {16 hours: 5,17 minutes: 30,18 seconds: <decimal>0.019};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: zoneOffset29};3031task:Error|task:JobId result = task:scheduleOneTimeJob(new Job("Hi"), time);
For an example on the usage of the scheduleOneTimeJob
, see the Task One-time Job Execution Example.
Frequency-based Job Execution
This API provides the functionality to schedule jobs on a specific interval either once or periodically with an optional start time, end time, and maximum count.
The following code snippet shows how to schedule a recurrence job by using this API.
1class Job {23 *task:Job;4 string msg;56 public function execute() {7 io:println(self.msg);8 }910 isolated function init(string msg) {11 self.msg = msg;12 }13}1415time:ZoneOffset zoneOffset = {16 hours: 5,17 minutes: 3018};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: zoneOffset28};2930task:Error|task:JobId result = task:scheduleJobRecurByFrequency(new Job("Hi"), 2.5, maxCount = 10, startTime = time);
For an example on the usage of the scheduleJobRecurByFrequency
, see the Task Frequency Job Execution Example.
Functions
[10]
configureWorkerPool | Configure the scheduler worker pool. |
getRunningJobs | Gets all the running jobs. |
getTimeInMillies | Gets time in milliseconds of the given |
pauseAllJobs | Pauses all the jobs. |
pauseJob | Pauses the particular job. |
resumeAllJobs | Resumes all the jobs. |
resumeJob | Resumes the particular job. |
scheduleJobRecurByFrequency | Schedule the recurring |
scheduleOneTimeJob | Schedule the given |
unscheduleJob | Unschedule the |
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 |
WaitingPolicy | Possible options for the |
Errors
[1]
Error | Represents the error type of the |