Search code examples
apicontent-management-systemstrapi

Strapi Cron doesn't work after migrating to version strapi-3.0.0


Cron usually worked in the strapi-3.0.0-beta.20 version but It doesn't work after migrating to version strapi-3.0.0

Strapi-3.0.0-beta.20

./config/environments/{env}/server.json

{
    "host": "0.0.0.0",
    "port": 1337,
    "proxy": {
        "enabled": false
    },
    "cron": {
        "enabled": true
    },
    "admin": {
        "autoOpen": false
    }
}

Strapi-3.0.0

./config/server.js

module.exports = ({ env }) => ({
    host: '0.0.0.0',
    port: env.int('PORT', '1337'),
    production: env('NODE_ENV') === 'production' ? true : false,
    proxy: {
        enabled: false
    },
    cron: {
        enabled: true
    },
    admin: {
        autoOpen: false
    }
})

This is strapi code that uses the server.js

strapi/packages/strapi/lib/middlewares/cron/index.js


  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]);
  });


Solution

  • This is the content registered in the github issue.

    Describe the bug Incorrect information in documentation for new configuration loader

    Expected behavior There is a possibility of misunderstanding in the document regarding the cron setting.

    This is a setting to activate cron (3.0.0.beta.20) ./config/environments/{env}/server.json

    {
        "host": "0.0.0.0",
        "port": 1337,
        "cron": {
            "enabled": true
        }
    }
    

    The documentation on how to migrate guides like this. Migrating

    Server
    
    Your server configuration can move from ./config/environments/{env}/server.json to 
    ./config/server.js like shown here.
    

    Server Available options -> cron

    However, to enable cron in version 3.0.0 must do it in the middleware.js

    ./config/middleware.js

        timeout: 100,
        load: {
            before: ['responseTime', 'logger', 'cors', 'responses', 'gzip'],
            order: [
                "Define the middlewares' load order by putting their name in this array is the right order"
            ],
            after: ['parser', 'router']
        },
        settings: {
           ...
           cron: { enabled: true }
           ...
        }
    
    

    Code snippets After checking the code (strapi/middlewares/index.js), I learned that it should be set in middleware.

    System - Node.js version: v12.14.0 - NPM version: 6.13.6 - Strapi version: 3.0.0 - Database: mongodb - Operating system: window, linux