import ballerina/io;
import ballerina/runtime;
import ballerina/task;

int reminderCount = 0;

public function main() {
    // The Appointment data record provides the appointment configurations.
    task:AppointmentData appointmentData = {
        seconds: "0/2",
        minutes: "*",
        hours: "*",
        daysOfMonth: "?",
        months: "*",
        daysOfWeek: "*",
        year: "*"
    };

    // Create an Appointment using the configurations.
    task:Scheduler appointment = new ({appointmentDetails: appointmentData});

    // Attach the service to the scheduler and exit if there is an error.
    var attachResult = appointment.attach(appointmentService);
    if (attachResult is error) {
        io:println("Error attaching the service.");
        return;
    }

    // Start the scheduler and exit if there is an error.
    var startResult = appointment.start();
    if (startResult is error) {
        io:println("Starting the task is failed.");
        return;
    }

    runtime:sleep(10000);

    // Cancel the appointment.
    var result = appointment.stop();
    if (result is error) {
        io:println("Error occurred while cancelling the task");
        return;
    }
    io:println("Appointment cancelled.");
}

// Creating a service on the task Listener.
service appointmentService = service {
    // This resource triggers when the appointment is due.
    resource function onTrigger() {
        if (reminderCount < 5) {
            reminderCount = reminderCount + 1;
            io:println("Schedule is due - Reminder: " + reminderCount.toString());
        }
    }
};

Task Scheduler Appointment

A Task Scheduler can be used to schedule an Appointment. An appointment should have an appointmentData field. It can be either a string representing a CRON expression or an AppointmentData record. There is an optional noOfRecurrences field that can be used to provide the maximum number of times the appointment should run before shutting down. A service can be attached to the Scheduler using the attach() function. Then the Scheduler can be started using start() method. When the scheduler needs to be cancelled, stop() method can be called.

import ballerina/io;
import ballerina/runtime;
import ballerina/task;
int reminderCount = 0;
public function main() {
    task:AppointmentData appointmentData = {
        seconds: "0/2",
        minutes: "*",
        hours: "*",
        daysOfMonth: "?",
        months: "*",
        daysOfWeek: "*",
        year: "*"
    };

The Appointment data record provides the appointment configurations.

    task:Scheduler appointment = new ({appointmentDetails: appointmentData});

Create an Appointment using the configurations.

    var attachResult = appointment.attach(appointmentService);
    if (attachResult is error) {
        io:println("Error attaching the service.");
        return;
    }

Attach the service to the scheduler and exit if there is an error.

    var startResult = appointment.start();
    if (startResult is error) {
        io:println("Starting the task is failed.");
        return;
    }

Start the scheduler and exit if there is an error.

    runtime:sleep(10000);
    var result = appointment.stop();
    if (result is error) {
        io:println("Error occurred while cancelling the task");
        return;
    }
    io:println("Appointment cancelled.");
}

Cancel the appointment.

service appointmentService = service {

Creating a service on the task Listener.

    resource function onTrigger() {
        if (reminderCount < 5) {
            reminderCount = reminderCount + 1;
            io:println("Schedule is due - Reminder: " + reminderCount.toString());
        }
    }
};

This resource triggers when the appointment is due.

# To start the service, navigate to the directory that contains the
# `.bal` file and execute the `ballerina run` command below.
ballerina run task_scheduler_appointment.bal
Schedule is due - Reminder: 1
Schedule is due - Reminder: 2
Schedule is due - Reminder: 3
Schedule is due - Reminder: 4
Schedule is due - Reminder: 5
Appointment cancelled.

This example demonstrates an Appointment, which is used to send a reminder every 2 seconds up to 5 reminders. When the reminder count reaches 5, the task scheduler will stop running.