Coder Social home page Coder Social logo

cron's Introduction

GoDoc Build Status

cron

Essentially it's a fork of robfig cron with distributed lock feature.

This cron package is helpful when you scale your cron instances and want to run same jobs once at a time.

There is built-in implementation with redsync distributed lock (Redis-based distributed mutual exclusion lock).

โš ๏ธ Be careful and make sure to understand how the redlock algorithm works. Check this discussion.

Moreover, you are capable to provide your own locker implementation. Check the feature example.

Getting started

To download the package, run:

go get github.com/hanagantig/cron

Import it in your program as:

import "github.com/hanagantig/cron"

A simple usage:

myCron := cron.New(cron.WithLogger(logger), cron.WithRedsyncLocker(redisPool))
myCron.AddFunc("*/15 * * * *", "myTaskKey", myTaskFunc)
myCron.Start()

Refer to the documentation here: http://godoc.org/github.com/hanagantig/cron

Features

Spec parsers

Standard cron spec parsing by default (first field is "minute"), with an easy way to opt into the seconds field (quartz-compatible). Although, note that the year field (optional in Quartz) is not supported.

// Seconds field, required
cron.New(cron.WithSeconds())

// Seconds field, optional
cron.New(cron.WithParser(cron.NewParser(
	cron.SecondOptional | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow | cron.Descriptor,
)))

Logging

Extensible, key/value logging via an interface that complies with the https://github.com/go-logr/logr project. Configure a logger:

cron.New(
    cron.WithLogger(cron.VerbosePrintfLogger(logger))
)

Interceptors

Chain & JobWrapper types allow you to install "interceptors" to add cross-cutting behavior like the following:

  • Recover any panics from jobs
  • Delay a job's execution if the previous run hasn't completed yet
  • Skip a job's execution if the previous run hasn't completed yet
  • Log each job's invocations
  • Notification when jobs are completed
  • Panic recovery and configure the panic logger
cron.New(cron.WithChain(
  cron.Recover(logger),  // or use cron.DefaultLogger
))

Job distributed lock

You can use built-in redsync locker to lock your jobs or provide a custom locker.

// with built-in redsync locker
cron.New(
    cron.WithRedsyncLocker(pool)
)

// with your own locker
cron.New(
    cron.WithLocks(locker)
)

Check the documentation for WithRedsyncLocker option.

Background - Cron spec format

There are two cron spec formats in common usage:

The original version of this package included an optional "seconds" field, which made it incompatible with both of these formats. Now, the "standard" format is the default format accepted, and the Quartz format is opt-in.

cron's People

Contributors

hanagantig avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

cron's Issues

Consul locker

In addition to the current redis locker implement a locker with consul distributed lock

Configure lockTTL

For now we have hardcoded lock time and it's lockTTL = 10 * time.Second
It should be configurable with cron options

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.