ExLock

Postgres advisory lock backed elixir Library for locking critical section of code running on multiple machines.

Installation

It can be installed by adding ex_lock to your list of dependencies in mix.exs:

def deps do
  [
    {:ex_lock, "~> 0.1.1"}
  ]
end

Usage

ExLock.execute "lock_key", [], fn ->
  # critical section
end

Parameters

Responses

{:ok, function_result} - In case lock has been acquired and function has executed properly. 
{:error, %ExLock.Error{message: "lock could not be acquired"}} - In case lock has not been acquired
{:error, %ExLock.Error{message: "function timed out"}} - In case function has timed out.

Bang function for execute is also supported.

ExLock.execute! "lock_key", [], fn ->
  # critical section
end

Lock can also be acquired and released from the code.

{:ok, lock_holder} = ExLock.acquire("lock_key", [])

# critical section

ExLock.release(lock_holder, "lock_key")

If lock is getting acquired in this fashion, please make sure it gets released after the work is done.

Second parameter is a keyword list supporting following values.

Bang function for acquire is also supported.

lock_holder = ExLock.acquire!("lock_key", [])

# critical section

ExLock.release(lock_holder, "lock_key")

Check the specs here - ExLock Spec