diff --git a/docs/v3.x/concepts/configurations.md b/docs/v3.x/concepts/configurations.md index 05f3f456f3..57cea84568 100644 --- a/docs/v3.x/concepts/configurations.md +++ b/docs/v3.x/concepts/configurations.md @@ -297,6 +297,27 @@ module.exports = { }; ``` +If your CRON task is required to run based on a specific timezone then you can configure the task like below: + +```js +module.exports = { + /** + * CRON task 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 + */ + + '0 0 1 * * 1': { + task: () => { + // Add your own logic here (e.g. send a queue of email, create a database backup, etc.). + }, + options: { + tz: 'Asia/Dhaka', + }, + }, +}; +``` + ### Database ORM customization When present, they are loaded to let you customize your database connection instance, for example for adding some plugin, customizing parameters, etc. diff --git a/docs/v3.x/guides/scheduled-publication.md b/docs/v3.x/guides/scheduled-publication.md index 6c1b78a76b..634486f099 100644 --- a/docs/v3.x/guides/scheduled-publication.md +++ b/docs/v3.x/guides/scheduled-publication.md @@ -24,7 +24,7 @@ The goal will be to check every minute if there are draft articles that have a ` To execute a function every minutes, we will use a CRON task. -Here is the [full documentation](../concepts/configurations.md#cron-tasks) of this feature. +Here is the [full documentation](../concepts/configurations.md#cron-tasks) of this feature. If your CRON task requires to run based on a specific timezone then do look into the full documentation. **Path —** `./config/functions/cron.js` diff --git a/packages/strapi/lib/middlewares/cron/index.js b/packages/strapi/lib/middlewares/cron/index.js index cd3dbdb390..5dda1cf2d7 100644 --- a/packages/strapi/lib/middlewares/cron/index.js +++ b/packages/strapi/lib/middlewares/cron/index.js @@ -20,8 +20,25 @@ module.exports = strapi => { initialize() { if (strapi.config.get('server.cron.enabled', false) === true) { - _.forEach(_.keys(strapi.config.get('functions.cron', {})), task => { - cron.scheduleJob(task, strapi.config.functions.cron[task]); + _.forEach(_.keys(strapi.config.get('functions.cron', {})), taskExpression => { + const taskValue = strapi.config.functions.cron[taskExpression]; + const isFunctionValue = _.isFunction(taskValue); + + if (isFunctionValue) { + cron.scheduleJob(taskExpression, strapi.config.functions.cron[taskExpression]); + + return; + } + + const options = _.get(strapi.config.functions.cron[taskExpression], 'options', {}); + + cron.scheduleJob( + { + rule: taskExpression, + ...options, + }, + strapi.config.functions.cron[taskExpression]['task'] + ); }); } },