Cron Tasks

Cron Tasks are background jobs primarily responsible for maintaining system hygiene by cleaning up stale data, such as expired tokens and sessions.

A Cron Task is implemented by extending the io.supertokens.cronjobs.CronTask class.

Instances of Cron Tasks are managed by the ResourceDistributor.

Cron Task Scope

The scope of a Cron Task is determined by the function overridden in its implementation. The available options are as follows:

doTaskPerStorage(storage)

Executes the task for each StorageLayer instance initialized by the core. For example, this can be used to delete all expired tokens across storage layers.

doTaskPerApp(app)

Executes the task for each app loaded in the core. For instance, license checks and telemetry tasks are performed for each app.

doTaskPerTenant(tenant)

Executes the task for each tenant loaded in the core. For example, deleting used TOTP codes, as each tenant may have different rate limit settings.

doTaskForTargetTenant(targetTenant)

Executes the task for a specific tenant only. For example, this can be used to synchronize all tenant configurations using the base tenant.

For more information about apps and tenants, refer to the Multitenancy documentation.

Initial Wait Time and Interval

The execution schedule of a Cron Task can be controlled by overriding the following methods:

getInitialWaitTimeSeconds()

Specifies the delay, in seconds, before the first execution after the core starts. The default value is 0.

getIntervalTimeSeconds()

Specifies the interval, in seconds, between subsequent executions of the task. The default value is 3600 (1 hour).

Related Topics