ArLock for Rails3

<img src=“https://codeclimate.com/github/InWork/ar_lock.png” />

This Rails3 Gem implements an atomic locking model based on ActiveRecord.

Install

Inside your Gemfile:

gem "ar_lock"

and then run:

  • bundle install

Inside your Application:

Geting a lock is always an atomic operation. If you try to get more than one lock (by specifying an array), this is also executed atomic.

Get lock(s):

Get one lock:

Lock.get :lock_name

Get multiple locks as one atomic operation:

Lock.get [:lock_name1, :lock_name2]

optional you can specify a value (for example the user, which acquired the lock):

Lock.get :lock_name, value: 'value'

You can let Lock.get block, until the lock was acquired:

Lock.get :lock_name, blocking: true

If you do not want a lock which was already acquired will block your code:

Lock.get :lock_name, blocking: true, successful_if: :value_matches

It is possible to force a lock. Values of already acquired locks will be overwriten by the given value.

Lock.get :lock_name, value: 'value', force: true

Release lock(s):

Release one Lock:

Lock.release :lock_name

Release multiple locks:

Lock.release [:lock_name1, :lock_name2]

Only release a lock, if the value matches:

Lock.release :lock_name, value: 'value'

You can also release all locks at once:

Lock.release_all

Get the value of a lock:

To read the value of a lock:

Lock.get_value :lock_name

Generators

  • rails generate ar_lock:migration

Database Setup

Use

rails g ar_lock:migration

This will create a database migration:

TIMESTAMP_add_ar_lock_table.rb

License

This project is licenced under the MIT license.

Author

Philip Kurmann (philip (at) kman.ch)