npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

strapi-plugin-redcron

v0.1.0

Published

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.

Downloads

6,631

Readme

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

module.exports = {
  redis: {
    // your redis config
  },
  redcron: {
    enabled: true,
  },
}

Full Configuration

module.exports = {
  redis: {
    // your redis config
  },
  redcron: {
    config: {
      redlockConfig: {
        driftFactor: 0.01,
        retryCount: 10,
        retryDelay: 200,
        retryJitter: 200,
      },
      lockDelay: null,
      lockTTL: 5000,
      debug: false,
    },
    enabled: true,
  },
}

🚚 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

// path: ./config/cron-tasks.js

module.exports = {
  myJob: {
    task: ({ strapi }) => {/* Add your own logic here */ },
    bypassRedcron: false, // optional
    options: {
      rule: '0 0 1 * * 1',
    },
  },
};

Bootstrap Example

bootstrap({ strapi }) {
  strapi.cron.add({
    myJob: {
      task: async ({ strapi }) => {
        console.log("hello from bootstrap")
      },
      bypassRedcron: false, // optional
      options: {
        rule: '*/10 * * * * *',
      }
    },
  })
},

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

//server.js
'use strict';

// Start Strapi
const strapi = require('@strapi/strapi');
strapi().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