Cron 作业
¥Cron jobs
cron.enabled
配置选项应在 ./config/server.js
(或 TypeScript 项目的 ./config/server.ts
)file 中设置为 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:
创造 适当的文件。
¥Create the appropriate file.
使能够 服务器配置文件中的 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
¥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:
- JavaScript
- TypeScript
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",
},
},
};
export default {
/**
* 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:
- JavaScript
- TypeScript
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",
},
},
};
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: ({ 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:
- JavaScript
- TypeScript
module.exports = {
myJob: {
task: ({ strapi }) => {
/* Add your own logic here */
},
// only run once after 10 seconds
options: new Date(Date.now() + 10000),
},
};
export default {
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:
- JavaScript
- TypeScript
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),
},
},
};
export default {
myJob: {
task: ({ 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),
},
},
};
使用密钥格式
¥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:
- JavaScript
- TypeScript
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.).
},
};
export default {
/**
* 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:
- JavaScript
- TypeScript
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,
},
});
import cronTasks from "./cron-tasks";
export default ({ env }) => ({
host: env("HOST", "0.0.0.0"),
port: env.int("PORT", 1337),
cron: {
enabled: true,
tasks: cronTasks,
},
});
要了解有关在代码中使用 CRON 作业的更多信息,请参阅 相应指南。
¥To learn more about using CRON jobs in your code, please refer to the corresponding guide.