Coder Social home page Coder Social logo

prashanthpai / redis_cron Goto Github PK

View Code? Open in Web Editor NEW
8.0 3.0 0.0 13 KB

Cron job scheduler redis module, inspired by pg_cron.

License: MIT License

Rust 100.00%
redis cron module redis-module rust redis-scheduler scheduler redismodule-rs postgres pg-cron rustlang cron-expression-parser schedule rust-lang redis-modules

redis_cron's Introduction

redis_cron

redis_cron is a simple cron expression based job scheduler for Redis that runs inside redis as a module. It uses similar syntax as a regular cron and allows you to schedule redis commands directly on redis. This project is inspired from PostgreSQL extension pg_cron.

redis_cron runs scheduled jobs sequentially in a single thread since redis commands can only be run by one thread at a time inside redis.

Install

$ cargo build
$ # Mac:
$ redis-server --loadmodule ./target/debug/libredis_cron.dylib
$ # Linux:
$ redis-server --loadmodule ./target/debug/libredis_cron.so

Usage

Available commands:

CRON.SCHEDULE <cron-expression> <redis-command>
CRON.UNSCHEDULE <job-id>
CRON.LIST

Example

$ redis-cli
127.0.0.1:6379> CRON.SCHEDULE "1/10 * * * * *" EVAL "return redis.call('set','foo','bar')" 0
"f04fefb1-ebf1-4d47-a582-f04963df994b"
127.0.0.1:6379> CRON.SCHEDULE "1/5 * * * * *" HINCRBY myhash field 1
"be428e43-5501-4eed-83c3-2c9ef3c52f6f"
127.0.0.1:6379> CRON.LIST
1) 1) f04fefb1-ebf1-4d47-a582-f04963df994b
   2) 1/10 * * * * *
   3) EVAL return redis.call('set','foo','bar') 0
2) 1) be428e43-5501-4eed-83c3-2c9ef3c52f6f
   2) 1/5 * * * * *
   3) HINCRBY myhash field 1
127.0.0.1:6379> CRON.UNSCHEDULE be428e43-5501-4eed-83c3-2c9ef3c52f6f
(integer) 1
127.0.0.1:6379> CRON.LIST
1) 1) f04fefb1-ebf1-4d47-a582-f04963df994b
   2) 1/10 * * * * *
   3) EVAL return redis.call('set','foo','bar') 0
127.0.0.1:6379> CRON.UNSCHEDULE f04fefb1-ebf1-4d47-a582-f04963df994b
(integer) 1
127.0.0.1:6379> CRON.LIST
(empty array)

Logs:

66004:M 06 Mar 2021 15:33:22.814 # Server initialized
66004:M 06 Mar 2021 15:33:22.815 * Module 'cron' loaded from ./target/debug/libredis_cron.dylib
66004:M 06 Mar 2021 15:33:22.815 * Ready to accept connections
66004:M 06 Mar 2021 15:34:01.462 * <module> redis_cron: run: job_id=f04fefb1-ebf1-4d47-a582-f04963df994b; schedule=1/10 * * * * *; cmd=EVAL;
66004:M 06 Mar 2021 15:34:06.486 * <module> redis_cron: run: job_id=be428e43-5501-4eed-83c3-2c9ef3c52f6f; schedule=1/5 * * * * *; cmd=HINCRBY;
66004:M 06 Mar 2021 15:34:11.000 * <module> redis_cron: run: job_id=f04fefb1-ebf1-4d47-a582-f04963df994b; schedule=1/10 * * * * *; cmd=EVAL;
66004:M 06 Mar 2021 15:34:11.000 * <module> redis_cron: run: job_id=be428e43-5501-4eed-83c3-2c9ef3c52f6f; schedule=1/5 * * * * *; cmd=HINCRBY;
66004:M 06 Mar 2021 15:34:16.017 * <module> redis_cron: run: job_id=be428e43-5501-4eed-83c3-2c9ef3c52f6f; schedule=1/5 * * * * *; cmd=HINCRBY;
66004:M 06 Mar 2021 15:34:21.043 * <module> redis_cron: run: job_id=f04fefb1-ebf1-4d47-a582-f04963df994b; schedule=1/10 * * * * *; cmd=EVAL;
66004:M 06 Mar 2021 15:34:21.043 * <module> redis_cron: run: job_id=be428e43-5501-4eed-83c3-2c9ef3c52f6f; schedule=1/5 * * * * *; cmd=HINCRBY;
66004:M 06 Mar 2021 15:34:26.069 * <module> redis_cron: run: job_id=be428e43-5501-4eed-83c3-2c9ef3c52f6f; schedule=1/5 * * * * *; cmd=HINCRBY;
66004:M 06 Mar 2021 15:34:31.086 * <module> redis_cron: run: job_id=f04fefb1-ebf1-4d47-a582-f04963df994b; schedule=1/10 * * * * *; cmd=EVAL;
66004:M 06 Mar 2021 15:34:41.116 * <module> redis_cron: run: job_id=f04fefb1-ebf1-4d47-a582-f04963df994b; schedule=1/10 * * * * *; cmd=EVAL;

Cron expression syntax

Creating a schedule for a job is done using the FromStr impl for the Schedule type of the cron library.

The scheduling format is as follows:

sec   min   hour   day of month   month   day of week   year
*     *     *      *              *       *             *

Time is specified for UTC and not your local timezone. Note that the year may be omitted.

Comma separated values such as 5,8,10 represent more than one time value. So for example, a schedule of 0 2,14,26 * * * * would execute on the 2nd, 14th, and 26th minute of every hour.

Ranges can be specified with a dash. A schedule of 0 0 * 5-10 * * would execute once per hour but only on day 5 through 10 of the month.

Day of the week can be specified as an abbreviation or the full name. A schedule of 0 0 6 * * Sun,Sat would execute at 6am on Sunday and Saturday.

Reference

This project uses makes use of redismodule-rs

redis_cron's People

Contributors

prashanthpai avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.