Added timezone for cron jobs

Signed-off-by: Shabab Karim <shababkarim93@gmail.com>
This commit is contained in:
Shabab Karim 2020-10-17 21:31:12 +06:00
parent fad9fda318
commit 75c3ff9aab
3 changed files with 41 additions and 3 deletions

View File

@ -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 ### 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. When present, they are loaded to let you customize your database connection instance, for example for adding some plugin, customizing parameters, etc.

View File

@ -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. 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` **Path —** `./config/functions/cron.js`

View File

@ -20,8 +20,25 @@ module.exports = strapi => {
initialize() { initialize() {
if (strapi.config.get('server.cron.enabled', false) === true) { if (strapi.config.get('server.cron.enabled', false) === true) {
_.forEach(_.keys(strapi.config.get('functions.cron', {})), task => { _.forEach(_.keys(strapi.config.get('functions.cron', {})), taskExpression => {
cron.scheduleJob(task, strapi.config.functions.cron[task]); 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']
);
}); });
} }
}, },