Skip to main content

定时任务

🌐 Cron jobs

Page summary:

Cron 作业通过 node-schedule 在特定时间安排自定义函数,通过服务器配置和可选任务文件激活。

Prerequisites

cron.enabled 配置选项应在 ./config/server.js(或针对 TypeScript 项目使用 ./config/server.ts文件 中设置为 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 在任意给定时间只使用一个定时器,而不是每秒/每分钟重新评估即将到来的作业。

此功能由 `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. 创建适当的文件。
  2. 在服务器配置文件中启用定时任务。
Tip

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

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

创建一个定时任务

🌐 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",
},
},
};
高级示例 #1:时区

以下 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",
},
},
};
Details

高级示例 #2:一次性 cron 任务 以下 cron 任务仅在指定时间运行一次:

./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),
},
};
高级示例 #3:开始和结束时间

以下 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

Warning

使用键格式会创建一个匿名的定时任务,这可能在尝试禁用定时任务或使用某些插件时引发问题。建议使用对象格式。

🌐 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.).
},
};

启用定时任务

🌐 Enabling cron jobs

要启用定时任务,请在服务器配置文件中将 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,
},
});

添加或删除定时任务

🌐 Adding or removing cron jobs

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

🌐 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");
Note

使用 key as the rule 的定时任务无法删除。

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

列出 cron 任务

🌐 Listing cron jobs

在你的自定义代码中任何地方使用 strapi.cron.jobs 来列出当前正在运行的所有定时任务:

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

strapi.cron.jobs