2020-01-20 15:44:47 +01:00
# Scheduled publication
2020-01-15 16:25:51 +01:00
This guide will explain how to create an article schedule system.
## Introduction
2020-01-15 16:48:30 +01:00
This guide will use this [draft system ](./draft.md ) guide. You should review it first to understand the draft/published status and the **Article** API that we will use in this guide.
2020-01-15 16:25:51 +01:00
2020-01-15 16:48:30 +01:00
What we want here is to be able to set a publication date for an article, and at this date, switch the `draft` status to `published` .
2020-01-15 16:25:51 +01:00
## Example
2020-01-15 16:48:30 +01:00
For this example, we will have to add a `publish_at` attribute to the **Article** Content Type.
2020-01-15 16:25:51 +01:00
- Click on the Content Type Builder link in the left menu
2020-01-15 16:48:30 +01:00
- Select the **Article** Content Type
2020-01-15 16:25:51 +01:00
- Add another field
2020-01-15 16:48:30 +01:00
- `date` attribute named `publish_at` with `datetime` type
2020-01-15 16:25:51 +01:00
2020-01-15 16:48:30 +01:00
And add some data with different dates and status to be able to see the publication happen.
Make sure to create some entries with a draft `status` and a `published_at` that is before the current date.
2020-01-15 16:25:51 +01:00
2020-01-15 16:48:30 +01:00
The goal will be to check every minute if there is `draft` articles that have a `publish_at` lower that the current date.
2020-01-15 16:25:51 +01:00
## Create a CRON task
2020-01-15 16:48:30 +01:00
To execute a function every minutes, we will use a CRON task.
2020-01-15 16:25:51 +01:00
Here is the [full documentation ](../concepts/configurations.md#cron-tasks ) of this feature.
**Path —** `./config/functions/cron.js`
```js
module.exports = {
'*/1 * * * * ': () => {
console.log('1 minute later');
},
};
```
2020-01-15 16:48:30 +01:00
Make sure the enabled cron config is set to true in `./config/environments/**/server.json` file.
2020-01-15 16:25:51 +01:00
2020-01-15 16:49:44 +01:00
::: tip
2020-01-15 16:48:30 +01:00
Please note that Strapi's built in CRON feature will not work if you plan to use `pm2` or node based clustering. You will need to execute these CRON tasks outside of Strapi.
:::
2020-01-15 16:25:51 +01:00
2020-01-15 16:48:30 +01:00
## Business logic
Now we can start writting the publishing logic. The code that will fetch all `draft` **Articles** with a `published_at` that is before the current date.
2020-01-15 16:25:51 +01:00
Then we will update the `status` of all these articles to `published` .
**Path —** `./config/functions/cron.js`
```js
module.exports = {
'*/1 * * * * ': async () => {
// fetch articles to publish
const draftArticleToPublish = await strapi.api.article.services.article.find({
status: 'draft',
2020-01-15 16:48:30 +01:00
publish_at_lt: new Date()
2020-01-15 16:25:51 +01:00
});
// update status of articles
draftArticleToPublish.forEach(async (article) => {
await strapi.api.article.services.article.update(
{id: article.id},
{status: 'published'}
);
});
},
};
```
And tada!