Scheduler API

Headless Client Plugin Guide / Scheduler API

Overview

The Scheduler API provides functionality for scheduling tasks to be executed at specified times or intervals. This is useful for automating recurring tasks like backups, data syncs, or periodic cleanups. The tasks are still subject to Servoy’s single-threaded nature, so any running method will block other tasks from executing.

You can find more detailed information about Scheduler API here.

Features

  • Time-Based Execution: Methods can be scheduled to run at a specific time or at regular intervals.

  • CRON Expressions: The Scheduler API supports CRON expressions for highly flexible task scheduling.

  • Job Management: Jobs can be created, listed, and removed as needed.

Add a job

Here is an example for adding job / scheduling a simple task:

// Schedule a method to run 5 minutes from now
var date = new Date();
date.setMinutes(date.getMinutes() + 5);
plugins.scheduler.addJob('simpleJob', date, simpleTask);

function simpleTask() {
    application.output('Scheduler API: Simple task executed.');
}

Add a job with repeating interval

Here is an example for adding job with repeating interval using plugins.scheduler.addCronJob(jobname, startDate, method, repeatInterval, repeatCount) method:

The example will alternate between Ping and Pong every 1 second, for 10 times.


// Define the ping method
function ping(){
	application.output('Ping'); //Outputs "Ping" to the log.
}

// Define the pong method
function pong(){
	application.output('Pong'); //Outputs "Pong" to the log.
}

// Start the ping-pong loop
function PingPong(){
	var startDatePing = new Date(); // Define start time for ping method
	var startDatePong = new Date(); // Define start time for pong method
	startDatePing.setTime(startDatePing.getTime()+2000); // setting the ping method to start after 2 seconds (2000 ms)
	startDatePong.setTime(startDatePing.getTime()+1000); // setting the pong method to start 1 second (1000 ms) after the ping method

	plugins.scheduler.addJob('ping',startDatePing,ping,2000,9); //repeats the ping method every 2 seconds (2000 ms) for 10 times : 1 initial and 9 repeats
	plugins.scheduler.addJob('pong',startDatePong,pong,2000,9); //repeats the pong method every 2 seconds (2000 ms), starting 1 second after ping method, for 10 times : 1 initial and 9 repeats
}

Add a CRON job

In Servoy, a CRON job is a scheduled task that runs at specified times or intervals using the Scheduler API. CRON jobs in Servoy allow you to automate server-side tasks based on time expressions called CRON expressions. These jobs are useful for recurring tasks such as data synchronization, backups, report generation, and other maintenance tasks that need to run periodically without user intervention.

Here are a few CRON timings generator websites:

Here is an example for adding a CRON job that runs every hour, using plugins.scheduler.addCronJob:

// Schedule a task to run every hour
plugins.scheduler.addCronJob('hourlyJob', '0 0 * * * ?', hourlyTask);

function hourlyTask() {
    application.output('Scheduler API: Hourly task running...');
    // Insert task logic here
}

Here is an example for adding a CRON job that runs every Monday, using plugins.scheduler.addCronJob method:

// Schedule a job to run every Monday at 8 AM
plugins.scheduler.addCronJob('weeklyJob', '0 0 8 ? * MON', weeklyTask);

function weeklyTask() {
    application.output('Scheduler API: Weekly task running...');
    // Weekly task logic
}

List/Remove jobs

Here is an example for listing and removing scheduled jobs using plugins.scheduler.getCurrentJobNames and plugins.scheduler.removeJob method:

// Listing scheduled jobs
var jobList = plugins.scheduler.getCurrentJobNames();
application.output('Scheduled jobs: ' + jobList.join(', '));

// Removing a job
plugins.scheduler.removeJob('simpleJob');
application.output('Scheduler API: Job removed.');

Last updated