Project

ar_lock

0.0
No commit activity in last 3 years
No release in over 3 years
Atomic lock model which stores its locks in the database.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies
 Project Readme

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)