Giter Club home page Giter Club logo

cronlock's Introduction

Cronlock

A tiny utility to help make sure only one cron job (or process) of the same kind is running at any given time.

The problem

As an example, if you have a cron job that depends on a database and hangs when the database is down, next cron jobs will hang as well when they're launched. This can possibly snowball into consuming all of the available resources.

The solution

A good design is to have a circuitbreaker in place.

In our case - fail the next cron job when an existing cron job is already running.

Quick start

Download a binary release here:

https://github.com/jondot/cronlock/releases/tag/1.0.2

Usage

Cronlock is Unixy in that it does one thing and one thing well. Cronlock will

  • Fail (exit(1)) when it cannot acquire an exclusive lock
  • Succeed (exit(0)) when it acquired an exclusive lock

You can exploit these two properties in order to make sure your Cron job doesn't step on itself:

$ cronlock your required command && signal success command

you can also define your own lock with the CRONLOCK_LOCKFILE environment variable:

$ CRONLOCK_LOCKFILE=foobar.lock cronlock your required command && your
success command

Cronlock is build around flock internally and uses just the syscall package to stay lean and mean, which should be stable and reliable for this use case. That's basically it.

Usage (Extended)

You got me. Cronlock isn't only useful for cron, it could have just been called flock.

You can use it anywhere you want to make sure just one unix process is running, in the same way.

Contributing

Fork, implement, add tests, pull request, get my everlasting thanks and a respectable place here :).

Copyright

Copyright (c) 2014 Dotan Nahum @jondot. See MIT-LICENSE for further details.

cronlock's People

Contributors

jondot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

happy-ferret

cronlock's Issues

Lock is released when cronlock exits

I don't understand how this tool is working. Here's an example:

$ (./cronlock && echo Starting A && sleep 10 && echo Finished A) &
[1] 8117
Cronlock: locked [lock.cronlock]. Returning success.
Starting A

$ (./cronlock && echo Starting B && sleep 10 && echo Finished B) &
[2] 8140
Cronlock: locked [lock.cronlock]. Returning success.
Starting B

$ (./cronlock && echo Starting C && sleep 10 && echo Finished C) &
[3] 8157
Cronlock: locked [lock.cronlock]. Returning success.
Starting C

Finished A
Finished B
Finished C

[1]   Done                    ( ./cronlock && echo Starting A && sleep 10 && echo Finished A )
[2]-  Done                    ( ./cronlock && echo Starting B && sleep 10 && echo Finished B )
[3]+  Done                    ( ./cronlock && echo Starting C && sleep 10 && echo Finished C )

I'd expect the second and third commands to fail.

The reason for the issue is that after the lock is acquired, the cronlock process is done and all its locks are released (along with file descriptors, sockets, etc.).

A solution would be to accept the command-line to run as an argument, and to run it in a sub-process. During that sub-process lifetime the lock is still taken because the cronlock process is still running. This is exactly the approach taken by lockrun or Linux's flock.

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.