consul_mut_ex will not be maintained in this repository going forward. Please use, create issues on, and make PRs to the fork of consul_mut_ex located here.
An Elixir module for acquiring and releasing locks with Consul and other backends.
If available in Hex, the package can be installed as:
- Add
consul_mut_ex
to your list of dependencies inmix.exs
:
def deps do
[{:consul_mut_ex, "~> 0.1.0"}]
end
- Ensure
consul_mut_ex
is started before your application:
def application do
[applications: [:consul_mut_ex]]
end
iex> require ConsulMutEx
Add to config.exs:
config :consul_mut_ex, :backend, :consul
config :consul_mut_ex, :consul,
host: "http://localhost:8500"
or through environment vars:
config :consul_mut_ex, :consul,
host: { :system, "CONSUL_HOST" }
Pass in a do...else
block:
iex> ConsulMutEx.lock("test_key", max_retries: 0) do
...> :acquired
...> else
...> :failed_to_acquire
...> end
:acquired
Or, call acquire_lock
and release_lock
manually:
iex> {:ok, lock} = ConsulMutEx.acquire_lock("test_key", max_retries: 0)
iex> ConsulMutEx.lock("test_key", max_retries: 0) do
...> :acquired
...> else
...> :failed_to_acquire
...> end
:failed_to_acquire
iex> ConsulMutEx.release_lock(lock)
:ok
Install consul
$ brew install consul
$ brew services start consul
==> Successfully started `consul` (label: homebrew.mxcl.consul)
Test consul works
$ curl -X GET "http://localhost:8500/v1/status/leader"
"127.0.0.1:8300"
Test you can obtain the lock
iex> require ConsulMutEx
iex> {:ok, lock} = ConsulMutEx.acquire_lock("test_key", max_retries: 0)
mix test
mix docs
open docs/index.html
v0.1.0
- Initial lock code works with ETS backend.v0.2.0
- Supporting Consul as a backend.v1.0.0
- Open source release
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request :D