Cron jobs#
Cron jobs schedule custom functions at specific times via `croner`, activated through server config and optional task files.The cron.enabled configuration option should be set to true in the ./config/server.js (or ./config/server.ts for TypeScript projects) file.
cron allows scheduling arbitrary functions for execution at specific dates, with optional recurrence rules. These functions are named cron jobs.
This feature is powered by the package.
The cron format consists of:
* * * * * *
⬠⬠⬠⬠⬠ā¬
ā ā ā ā ā |
ā ā ā ā ā ā day of week (0 - 7) (0 or 7 is Sun)
ā ā ā ā āāāāāā month (1 - 12)
ā ā ā āāāāāāāāāāā day of month (1 - 31)
ā ā āāāāāāāāāāāāāāāā hour (0 - 23)
ā āāāāāāāāāāāāāāāāāāāāā minute (0 - 59)
āāāāāāāāāāāāāāāāāāāāāāāāāā second (0 - 59, OPTIONAL)
To define cron jobs and have them run at the required times:
Creating a cron job#
A cron job can be created using the object format or key format.
Using the object format#
To define a cron job with the object format, create a file with the following structure:
module.exports = {
/**
* Simple example.
* Every monday at 1am.
*/
myJob: {
task: ({ strapi }) => {
// Add your own logic here (e.g. send a queue of email, create a database backup, etc.).
},
options: {
rule: "0 0 1 * * 1",
},
},
};
import type { Modules } from "@strapi/strapi";
export default {
/**
* Simple example.
* Every monday at 1am.
*/
myJob: {
task: async ({ strapi }) => {
// Add your own logic here (e.g. send a queue of email, create a database backup, etc.).
},
options: {
rule: "0 0 1 * * 1",
},
},
} satisfies Modules.Cron.CronTasks;
Advanced example #1: Timezones
The following cron job runs on a specific timezone:
module.exports = {
/**
* Cron job with timezone example.
* Every Monday at 1am for Asia/Dhaka timezone.
* List of valid timezones: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones#List
*/
myJob: {
task: ({ strapi }) => {
/* Add your own logic here */
},
options: {
rule: "0 0 1 * * 1",
tz: "Asia/Dhaka",
},
},
};
import type { Modules } from "@strapi/strapi";
export default {
/**
* Cron job with timezone example.
* Every Monday at 1am for Asia/Dhaka timezone.
* List of valid timezones: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones#List
*/
myJob: {
task: async ({ strapi }) => {
/* Add your own logic here */
},
options: {
rule: "0 0 1 * * 1",
tz: "Asia/Dhaka",
},
},
} satisfies Modules.Cron.CronTasks;
Advanced example #2: One-off cron jobs
The following cron job is run only once at a given time:
module.exports = {
myJob: {
task: ({ strapi }) => {
/* Add your own logic here */
},
// only run once after 10 seconds
options: new Date(Date.now() + 10000),
},
};
import type { Modules } from "@strapi/strapi";
export default {
myJob: {
task: async ({ strapi }) => {
/* Add your own logic here */
},
// only run once after 10 seconds
options: new Date(Date.now() + 10000),
},
} satisfies Modules.Cron.CronTasks;
Advanced example #3: Start and end times
The following cron job uses start and end times:
module.exports = {
myJob: {
task: ({ strapi }) => {
/* Add your own logic here */
},
options: {
rule: "* * * * * *",
// start 10 seconds from now
start: new Date(Date.now() + 10000),
// end 20 seconds from now
end: new Date(Date.now() + 20000),
},
},
};
import type { Modules } from "@strapi/strapi";
export default {
myJob: {
task: async ({ strapi }) => {
/* Add your own logic here */
},
// only run once after 10 seconds
options: {
rule: "* * * * * *",
// start 10 seconds from now
start: new Date(Date.now() + 10000),
// end 20 seconds from now
end: new Date(Date.now() + 20000),
},
},
} satisfies Modules.Cron.CronTasks;
Using the key format#
To define a cron job with the key format, create a file with the following structure:
module.exports = {
/**
* Simple example.
* Every monday at 1am.
*/
"0 0 1 * * 1": ({ strapi }) => {
// Add your own logic here (e.g. send a queue of email, create a database backup, etc.).
},
};
import type { Modules } from "@strapi/strapi";
export default {
/**
* Simple example.
* Every monday at 1am.
*/
"0 0 1 * * 1": async ({ strapi }) => {
// Add your own logic here (e.g. send a queue of email, create a database backup, etc.).
},
} satisfies Modules.Cron.CronTasks;
Enabling cron jobs#
To enable cron jobs, set cron.enabled to true in the server configuration file and declare the jobs:
const cronTasks = require("./cron-tasks");
module.exports = ({ env }) => ({
host: env("HOST", "0.0.0.0"),
port: env.int("PORT", 1337),
app: {
keys: env.array("APP_KEYS"),
},
cron: {
enabled: true,
tasks: cronTasks,
},
});
import cronTasks from "./cron-tasks";
import type { Core } from '@strapi/strapi';
export default ({ env }: Core.Config.Shared.ConfigParams): Core.Config.Server => ({
host: env("HOST", "0.0.0.0"),
port: env.int("PORT", 1337),
app: {
keys: env.array("APP_KEYS")!,
},
cron: {
enabled: true,
tasks: cronTasks,
},
});
Adding or removing cron jobs#
Use strapi.cron.add anywhere in your custom code add CRON jobs to the Strapi instance:
module.exports = () => ({
bootstrap({ strapi }) {
strapi.cron.add({
// runs every second
myJob: {
task: ({ strapi }) => {
console.log("hello from plugin");
},
options: {
rule: "* * * * * *",
},
},
});
},
});
Use strapi.cron.remove anywhere in your custom code to remove CRON jobs from the Strapi instance, passing in the key corresponding to the CRON job you want to remove:
strapi.cron.remove("myJob");
Listing cron jobs#
Use strapi.cron.jobs anywhere in your custom code to list all the cron jobs that are currently running:
strapi.cron.jobs