# 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](/reference/servoyextensions/server-plugins/scheduler.md#scheduler).

## 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:

```javascript
// 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)](/reference/servoyextensions/server-plugins/scheduler.md#addjob-jobname-startdate-method-repeatinterval-repeatcount) method:

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

```javascript

// 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**](https://en.wikipedia.org/wiki/Cron) is a scheduled task that runs at specified times or intervals using the [Scheduler API](/reference/servoyextensions/server-plugins/scheduler.md#scheduler). 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:

* [www.cronmaker.com](http://www.cronmaker.com)
* [crontab.guru](https://crontab.guru/)
* [crontab.cronhub.io](https://crontab.cronhub.io/)
* [www.freeformatter.com](https://www.freeformatter.com/cron-expression-generator-quartz.html)

Here is an example for adding a CRON job that runs every hour, using [plugins.scheduler.addCronJob](/reference/servoyextensions/server-plugins/scheduler.md#addcronjob-jobname-crontimings-method):

```javascript
// 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](/reference/servoyextensions/server-plugins/scheduler.md#addcronjob-jobname-crontimings-method) method:

```javascript
// 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](/reference/servoyextensions/server-plugins/scheduler.md#getcurrentjobnames) and [plugins.scheduler.removeJob](/reference/servoyextensions/server-plugins/scheduler.md#removejob-jobname) method:

```javascript
// 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.');
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.servoy.com/guides/develop/programming-guide/automation-and-scheduling/scheduler-api.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
