Skip to main content

Cron 作业

¥Cron jobs

Prerequisites

cron.enabled 配置选项应在 ./config/server.js(或 TypeScript 项目的 ./config/server.tsfile 中设置为 true

¥The cron.enabled configuration option should be set to true in the ./config/server.js (or ./config/server.ts for TypeScript projects) file.

cron 允许安排任意函数在特定日期执行,并具有可选的重复规则。这些函数称为 cron 作业。cron 在任何给定时间仅使用单个计时器,而不是每秒/每分钟重新评估即将到来的作业。

¥cron allows scheduling arbitrary functions for execution at specific dates, with optional recurrence rules. These functions are named cron jobs. cron only uses a single timer at any given time, rather than reevaluating upcoming jobs every second/minute.

此功能由 `node-schedule` 包提供支持。

¥This feature is powered by the `node-schedule` package.

cron 格式包括:

¥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)

要定义 cron 作业并让它们在所需的时间运行:

¥To define cron jobs and have them run at the required times:

  1. 创造 适当的文件。

    ¥Create the appropriate file.

  2. 使能够 服务器配置文件中的 cron 作业。

    ¥Enable the cron jobs in the server configuration file.

提示

或者,可以在 服务器配置文件cron.tasks 键中直接创建 cron 作业。

¥Optionally, cron jobs can be directly created in the cron.tasks key of the server configuration file.

创建一个 cron 作业

¥Creating a cron job

可以使用 对象格式密钥格式 创建 cron 作业。

¥A cron job can be created using the object format or key format.

使用对象格式

¥Using the object format

要使用对象格式定义 cron 作业,请创建具有以下结构的文件:

¥To define a cron job with the object format, create a file with the following structure:

./config/cron-tasks.js
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",
},
},
};
Advanced example #1: Timezones

以下 cron 作业在特定时区运行:

¥The following cron job runs on a specific timezone:

./config/cron-tasks.js
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",
},
},
};
Advanced example #2: One-off cron jobs

以下 cron 作业在给定时间仅运行一次:

¥The following cron job is run only once at a given time:

./config/cron-tasks.js
module.exports = {
myJob: {
task: ({ strapi }) => {
/* Add your own logic here */
},
// only run once after 10 seconds
options: new Date(Date.now() + 10000),
},
};
Advanced example #3: Start and end times

以下 cron 作业使用开始和结束时间:

¥The following cron job uses start and end times:

./config/cron-tasks.js
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),
},
},
};

使用密钥格式

¥Using the key format

警告

使用密钥格式创建匿名 cron 作业,在尝试禁用 cron 作业或使用某些插件时可能会导致问题。推荐使用对象格式。

¥Using the key format creates an anonymous cron job which may cause issues when trying to disable the cron job or with some plugins. It is recommended to use the object format.

要使用密钥格式定义 cron 作业,请创建具有以下结构的文件:

¥To define a cron job with the key format, create a file with the following structure:

./config/cron-tasks.js
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.).
},
};

启用 cron 作业

¥Enabling cron jobs

要启用 cron 作业,请将 服务器配置文件 中的 cron.enabled 设置为 true 并声明作业:

¥To enable cron jobs, set cron.enabled to true in the server configuration file and declare the jobs:

./config/server.js


const cronTasks = require("./cron-tasks");



module.exports = ({ env }) => ({
host: env("HOST", "0.0.0.0"),
port: env.int("PORT", 1337),
cron: {
enabled: true,
tasks: cronTasks,
},
});

添加或删除 cron 任务

¥Adding or removing cron jobs

在自定义代码的任何地方使用 strapi.cron.add 将 CRON 作业添加到 Strapi 实例:

¥Use strapi.cron.add anywhere in your custom code add CRON jobs to the Strapi instance:

./src/plugins/my-plugin/strapi-server.js
module.exports = () => ({
bootstrap({ strapi }) {
strapi.cron.add({
// runs every second
myJob: {
task: ({ strapi }) => {
console.log("hello from plugin");
},
options: {
rule: "* * * * * *",
},
},
});
},
});

在自定义代码的任何地方使用 strapi.cron.remove 从 Strapi 实例中删除 CRON 作业,并传入与要删除的 CRON 作业对应的键:

¥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");
注意

无法删除使用 key 作为规则 的 Cron 作业。

¥Cron jobs that are using the key as the rule can not be removed.

列出 cron 任务

¥Listing cron jobs

在自定义代码的任何地方使用 strapi.cron.jobs 列出当前正在运行的所有 CRON 作业:

¥Use strapi.cron.jobs anywhere in your custom code to list all the cron jobs that are currently running:

strapi.cron.jobs