ArLock for Rails3
This Rails3 Gem implements an atomic locking model based on ActiveRecord.
Inside your Gemfile:
and then run:
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 one lock:
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 one Lock:
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:
Get the value of a lock:
To read the value of a lock:
rails generate ar_lock:migration
rails g ar_lock:migration
This will create a database migration:
This project is licenced under the MIT license.
Philip Kurmann (philip (at) kman.ch)