ballerina/task2.0.0-beta.2
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 {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: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 {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: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 |
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 |