Strapi plugin logo for redcron

redcron
Plugin verified by Strapi

A drop in replacement for the Strapi cron plugin that uses Redlock to prevent multiple instances of Strapi from running the same cron job at the same time.

Redcron

A drop in replacement for the Strapi cron plugin that uses Redlock to prevent multiple instances of Strapi from running the same cron job at the same time.

NPM Version Monthly download on NPM

Table of Contents

✨ Features

Drop-in* replacement for the Strapi cron plugin that uses Redlock to prevent multiple instances of Strapi from running the same cron job at the same time.

* requires minimal configuration

🤔 Motivation

Currently, if you horizontally scale Strapi and use the cron feature, you will end up with multiple instances of Strapi running the same cron job at the same time, potentially causing race conditions. This can cause issues with your database or other services that you are trying to integrate with.

🖐 Requirements

Install and configure the Strapi Redis Plugin

This plugin needs to be registered and configured before the cron plugin.

⏳ Installation

# Using Yarn (Recommended)
yarn add strapi-plugin-redcron

# Using npm
npm install strapi-plugin-redcron --save

🔧 Configuration

Minimal Configuration

1module.exports = {
2  redis: {
3    // your redis config
4  },
5  redcron: {
6    enabled: true,
7  },
8}

Full Configuration

1module.exports = {
2  redis: {
3    // your redis config
4  },
5  redcron: {
6    config: {
7      redlockConfig: {
8        driftFactor: 0.01,
9        retryCount: 10,
10        retryDelay: 200,
11        retryJitter: 200,
12      },
13      lockDelay: null,
14      lockTTL: 5000,
15      debug: false,
16    },
17    enabled: true,
18  },
19}

🚚 Usage

Adding the bypassRedcron property to your cron job will bypass the redlock logic and allow multiple instances of Strapi to run the same cron job at the same time.

This plugin requires you to use the object format of the cron config. i.e if you are using the rule as the key, you will need to change it to an object with the rule as a property and the key as a unique name. This is because across your Strapi instances, Redis needs to lock onto a key that is the same across all instances.

If you need assistance understanding the cron syntax check out CronTab Guru.

Example

1// path: ./config/cron-tasks.js
2
3module.exports = {
4  myJob: {
5    task: ({ strapi }) => {/* Add your own logic here */ },
6    bypassRedcron: false, // optional
7    options: {
8      rule: '0 0 1 * * 1',
9    },
10  },
11};

Bootstrap Example

1bootstrap({ strapi }) {
2  strapi.cron.add({
3    myJob: {
4      task: async ({ strapi }) => {
5        console.log("hello from bootstrap")
6      },
7      bypassRedcron: false, // optional
8      options: {
9        rule: '*/10 * * * * *',
10      }
11    },
12  })
13},

Contributing

Feel free to open a PR if you want to contribute to this project.

You can spin up a new Redis cluster for testing by running docker-compose up in the root of the project. You can run Strapi multiple strapi instances at the same time by adding server.js to the root of your wrapper project

1//server.js
2'use strict';
3
4// Start Strapi
5const strapi = require('@strapi/strapi');
6strapi().start();

and running

pm2 start --name="mystrapiapp" server.js -i 2

License

See the LICENSE file for licensing information.

⭐️Did you find this helpful?

If you found this plugin helpful give it a star?

Links

Install now

npm install strapi-plugin-redcron

STATS

10 GitHub stars858 weekly downloads

Last updated

415 days ago

Strapi Version

4.0.0 and above

Author

github profile image for Excl Networks Inc.
Excl Networks Inc.

Useful links

Create your own plugin

Check out the available plugin resources that will help you to develop your plugin or provider and get it listed on the marketplace.