ArLock for Rails3

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

Install

Inside your Gemfile:

gem "ar_record"

and then run:

  • bundle install

Gem Dependencies

Please check if all those requirements are satisfied on your environment.

  • rails >= 3.0.0

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):

Lock.get :lock_name

or

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'

or

Lock.get [:lock_name1, :lock_name2], :value => 'value'

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

Lock.get :lock_name, :blocking => true

or

Lock.get [:lock_name1, :lock_name2], :blocking => true

Release lock(s):

Lock.release :lock_name

or

Lock.release [:lock_name1, :lock_name2]

Only release a lock, if the value matches:

Lock.release :lock_name, :value => 'value'

or

Lock.release [:lock_name1, :lock_name2], :value => 'value'

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

Copyright © 2011 Philip Kurmann. See LICENSE for details.