From e42b11d2274bcea52eced5c8b875a19cf60664b0 Mon Sep 17 00:00:00 2001 From: Pierre Wizla Date: Thu, 27 Oct 2022 15:26:33 +0200 Subject: [PATCH 1/3] Update cron task code to v4 --- .../latest/guides/scheduled-publication.md | 38 +++++++++++++------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/docs/developer-docs/latest/guides/scheduled-publication.md b/docs/developer-docs/latest/guides/scheduled-publication.md index 5676d3f2f2..f288a465e6 100644 --- a/docs/developer-docs/latest/guides/scheduled-publication.md +++ b/docs/developer-docs/latest/guides/scheduled-publication.md @@ -59,22 +59,36 @@ Then we will update the `published_at` of all these articles. **Path —** `./config/functions/cron.js` ```js +// path: ./config/cron-tasks.js + module.exports = { + /** + * Scheduled publication workflow. + * Checks every minute if there are draft articles to publish. + */ + '*/1 * * * *': async () => { - // fetch articles to publish - const draftArticleToPublish = await strapi.api.article.services.article.find({ - _publicationState: 'preview', // preview returns both draft and published entries - published_at_null: true, // so we add another condition here to filter entries that have not been published - publish_at_lt: new Date(), + console.log('1 minute later'); + // fetch articles to publish; + const draftArticleToPublish = await strapi.entityService.findMany('api::article.article', { + publicationState: 'preview', // preview returns both draft and published entries + filters: { + publishedAt: { + $null: true, // so we add another condition here to filter entries that have not been published + }, + publish_at: { + $lt: new Date() // and we keep only articles with a 'publish_at' datetime value that is lower than the current datetime + } + } }); - - // update published_at of articles + // update the publish_at of articles previously fetched await Promise.all(draftArticleToPublish.map(article => { - return strapi.api.article.services.article.update( - { id: article.id }, - { published_at: new Date() } - ); - })); + return strapi.entityService.update('api::article.article', article.id, { + data: { + publishedAt: new Date(), + } + }); + })) }, }; ``` From ae915cbc8a16261e3ea87e775567227a496acc62 Mon Sep 17 00:00:00 2001 From: Pierre Wizla Date: Thu, 27 Oct 2022 15:35:14 +0200 Subject: [PATCH 2/3] WIP --- docs/developer-docs/latest/guides/scheduled-publication.md | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/docs/developer-docs/latest/guides/scheduled-publication.md b/docs/developer-docs/latest/guides/scheduled-publication.md index f288a465e6..87bdca9a90 100644 --- a/docs/developer-docs/latest/guides/scheduled-publication.md +++ b/docs/developer-docs/latest/guides/scheduled-publication.md @@ -8,11 +8,7 @@ canonicalUrl: https://docs.strapi.io/developer-docs/latest/guides/scheduled-publ !!!include(developer-docs/latest/guides/snippets/guide-not-updated.md)!!! -This guide will explain how to create an article schedule system. - -## Introduction - -What we want here is to be able to set a publication date for an article, and at this date, switch the `draft` state to `published`. +This guide will help you set a publication date for an article, and at this date, switch the `draft` state to `published`. ## Example From b33b3eed9e04f0b225049368f0d8c39492edea89 Mon Sep 17 00:00:00 2001 From: Pierre Wizla Date: Fri, 28 Oct 2022 18:21:08 +0200 Subject: [PATCH 3/3] Remove scheduled publication guide Updated to v4 and moved to the forum: https://forum.strapi.io/t/schedule-publications/23184 --- docs/.vuepress/config/sidebar-developer.js | 1 - docs/.vuepress/redirects | 1 + .../latest/guides/scheduled-publication.md | 92 ------------------- .../saving-and-publishing-content.md | 6 +- 4 files changed, 6 insertions(+), 94 deletions(-) delete mode 100644 docs/developer-docs/latest/guides/scheduled-publication.md diff --git a/docs/.vuepress/config/sidebar-developer.js b/docs/.vuepress/config/sidebar-developer.js index 2984cfe253..4b305863d6 100644 --- a/docs/.vuepress/config/sidebar-developer.js +++ b/docs/.vuepress/config/sidebar-developer.js @@ -499,7 +499,6 @@ const developer = [ // ['/developer-docs/latest/guides/error-catching', 'Error catching'], // ['/developer-docs/latest/guides/external-data', 'Fetching external data'], ['/developer-docs/latest/guides/jwt-validation', 'JWT validation'], - ['/developer-docs/latest/guides/scheduled-publication', 'Scheduled publication'], // ['/developer-docs/latest/guides/secure-your-app', 'Secure your application'], // ['/developer-docs/latest/guides/send-email', 'Send email programmatically'], // ['/developer-docs/latest/guides/client', 'Setup a third party client'], diff --git a/docs/.vuepress/redirects b/docs/.vuepress/redirects index a68f8361bb..af40dd14ef 100644 --- a/docs/.vuepress/redirects +++ b/docs/.vuepress/redirects @@ -10,3 +10,4 @@ /developer-docs/latest/guides/unit-testing.html /developer-docs/latest/developer-resources/unit-testing.html /developer-docs/latest/guides/auth-request.html /developer-docs/latest/plugins/users-permissions.html /developer-docs/latest/guides/registering-a-field-in-admin.html /developer-docs/latest/development/custom-fields.html +/developer-docs/latest/guides/scheduled-publication.html /user-docs/latest/content-manager/saving-and-publishing-content.html diff --git a/docs/developer-docs/latest/guides/scheduled-publication.md b/docs/developer-docs/latest/guides/scheduled-publication.md deleted file mode 100644 index 87bdca9a90..0000000000 --- a/docs/developer-docs/latest/guides/scheduled-publication.md +++ /dev/null @@ -1,92 +0,0 @@ ---- -title: Scheduled Publication - Strapi Developer Docs -description: Learn in this guide how to create an article schedule system. -canonicalUrl: https://docs.strapi.io/developer-docs/latest/guides/scheduled-publication.html ---- - -# Scheduled publication - -!!!include(developer-docs/latest/guides/snippets/guide-not-updated.md)!!! - -This guide will help you set a publication date for an article, and at this date, switch the `draft` state to `published`. - -## Example - -For this example, we will have to add a `publish_at` attribute to the **Article** Content Type. - -- Click on the Content-type Builder link in the left menu -- Select the **Article** Content Type -- Add another field - - `date` attribute named `publish_at` with `datetime` type - -And add some data with different dates and state to be able to see the publication happen. -Make sure to create some entries with a draft state and a `publish_at` that is before the current date. - -The goal will be to check every minute if there are draft articles that have a `publish_at` lower that the current date. - -## Create a CRON task - -To execute a function every minutes, we will use a CRON task. - -Here is the [full documentation](/developer-docs/latest/setup-deployment-guides/configurations/optional/cronjobs.md) 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` - -```js -module.exports = { - '*/1 * * * *': () => { - console.log('1 minute later'); - }, -}; -``` - -Make sure the enabled cron config is set to true in `./config/server.js` file. - -::: tip -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. -::: - -## Business logic - -Now we can start writing the publishing logic. The code that will fetch all `draft` **Articles** with a `publish_at` that is before the current date. - -Then we will update the `published_at` of all these articles. - -**Path —** `./config/functions/cron.js` - -```js -// path: ./config/cron-tasks.js - -module.exports = { - /** - * Scheduled publication workflow. - * Checks every minute if there are draft articles to publish. - */ - - '*/1 * * * *': async () => { - console.log('1 minute later'); - // fetch articles to publish; - const draftArticleToPublish = await strapi.entityService.findMany('api::article.article', { - publicationState: 'preview', // preview returns both draft and published entries - filters: { - publishedAt: { - $null: true, // so we add another condition here to filter entries that have not been published - }, - publish_at: { - $lt: new Date() // and we keep only articles with a 'publish_at' datetime value that is lower than the current datetime - } - } - }); - // update the publish_at of articles previously fetched - await Promise.all(draftArticleToPublish.map(article => { - return strapi.entityService.update('api::article.article', article.id, { - data: { - publishedAt: new Date(), - } - }); - })) - }, -}; -``` - -And tada! diff --git a/docs/user-docs/latest/content-manager/saving-and-publishing-content.md b/docs/user-docs/latest/content-manager/saving-and-publishing-content.md index 50041a724d..a072286219 100644 --- a/docs/user-docs/latest/content-manager/saving-and-publishing-content.md +++ b/docs/user-docs/latest/content-manager/saving-and-publishing-content.md @@ -32,6 +32,10 @@ When a content is not a draft anymore, but has been published, it is indicated o ![Editing published version](../assets/content-manager/editing_published_version.png) +::: tip +To schedule publication, i.e. convert a draft to a published entry at a given date and time, you can follow [this technical guide](https://forum.strapi.io/t/schedule-publications/23184) which requires adding custom code to the Strapi application. +::: + ### Unpublishing content Published contents can be unpublished, switching back to being drafts again. @@ -51,4 +55,4 @@ You can delete entries from the list view of a collection type, by clicking on t ::: caution If the [Internationalization plugin](/user-docs/latest/plugins/strapi-plugins.md#internationalization-plugin) is installed, entries can only be deleted one locale at the time. -::: \ No newline at end of file +:::