[Proposal] Task Listener #7541
Labels
module/task
Status/Draft
In circulation by the author for initial review and consensus-building
Type/NewFeature
Type/Proposal
Summary
The Ballerina Task package provides APIs to create, schedule, and manage jobs. This proposal introduces a task listener that allows jobs to be executed based on schedule configurations. Jobs are represented as Ballerina services that can be attached to the task listener.
Goals
Non-Goals
Motivation
Currently, scheduling a task in Ballerina requires blocking the main strand, typically using a
sleep
statement in the main function:This approach is not user-friendly. Introducing a task listener will simplify job execution based on predefined schedules.
Description
Task service type
With the new listener-based approach, a job is implemented as a Ballerina service attached to the task listener. The task service type is defined as:
The
onTrigger
function executes when the scheduled trigger fires.Task listener
Configuration
The task listener requires a schedule configuration (one-time or recurring) and supports an optional worker pool:
If no worker pool is provided, the listener uses a global scheduler with the following configuration:
Listener APIs
The task listener provides the following APIs:
Lifecycle Management
start()
: Starts the task listener.gracefulStop()
: Stops the task listener gracefully.immediateStop()
: Stops the task listener immediately.attach(service)
/scheduleJob(service)
: Attaches/Schedules a task service to the task listener.detach(service)
/unscheduleJob(service)
: Detaches/Unschedules a task service from the task listener.Job Management
pauseAllJobs()
: Pauses all the jobs.resumeAllJobs()
: Resumes all the jobs.pauseJob(id)
: Pauses a specific job.resumeJob(id)
: Resumes a specific job.getRunningJobs()
: Returns the list of running job ids.Service implementation
Each task service should have a unique task ID for job management, specified in the service declaration as an attachment point. The service must also implement the
onTrigger
remote function, which defines the job's execution logic.Example
The following example demonstrates using a task listener to execute a scheduled job:
This setup ensures jobs execute on schedule without blocking the main execution flow.
The text was updated successfully, but these errors were encountered: