Giter Site home page Giter Site logo

0x000000 / lockfile Goto Github PK

View Code? Open in Web Editor NEW

This project forked from joshnesbitt/lockfile

2.0 2.0 0.0 110 KB

A simple implementation of a lock file class in Ruby

Home Page: http://blog.josh-nesbitt.net/2009/09/08/implementing-a-simple-lockfile-system-in-ruby/

License: MIT License

Ruby 100.00%

lockfile's Introduction

LockFile

A simple Lock File class written in Ruby.

en.wikipedia.org/wiki/Lock_file#Lock_files

Installation

From Gemcutter:

gem install simple_lockfile

The project page is located at gemcutter.org/gems/simple_lockfile.

Usage:

require 'lockfile'

l = LockFile.new({:path => "/Users/joshnesbitt/Desktop", :filename => "test.lock"}) # => "/Users/joshnesbitt/Desktop/test.lock"

l.locked? # => false

l.lock! # => Integer value

l.locked? # => true

l.process_id # => PID

l.unlock! # => Integer value

l.locked? # => false

I commonly use it for checking whether an expensive task is being executed:

refresh_search_index unless @lockfile.locked?

And in the method something like this:

def refresh_search_index
  @lockfile.lock!

  # ... some heavy operation ...

  @lockfile.unlock!
end

Expiration

You can check if your .lock files are expired, by passing :expire_after argument into initializer

LockFile.new({:expire_after => "1 hour"})
LockFile.new({:expire_after => "5 hours"})
LockFile.new({:expire_after => "1 minute"})
LockFile.new({:expire_after => "100 minutes"})
LockFile.new({:expire_after => "2 hours and 10 minutes"})

You can check that with expired? method:

l1 = LockFile.new({:expire_after => "1 hour"})
l1.locked?  # => false
l1.lock!
l1.expired? # => false

# more than hour passed and somewhere else

l2 = LockFile.new({:expire_after => "1 hour"})
l2.locked?  # => true
l2.expired? # => true

Testing

To install necessary gems, please try

$ gem install bundler

then

$ bundle

Specs are run using Watchr (github.com/mynyml/watchr) to observe the /lib and /spec directories for any changes. Once installed simply do:

watchr spec/watch.rb

You don’t have to use Watchr to run the specs, I just prefer to use it over autotest as its lightweight, quick and works. You could just run the specs normally.

Note on Patches/Pull Requests

  • Fork the project.

  • Make your feature addition or bug fix.

  • Add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)

  • Send me a pull request. Bonus points for topic branches.

Contributors

Copyright © 2010 Josh Nesbitt <[email protected]>. See LICENSE for details.

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.