All Projects → yuryroot → sequel-pg_advisory_lock

yuryroot / sequel-pg_advisory_lock

Licence: MIT license
Extension for ruby Sequel library that allows using PostgreSQL advisory locks for application-level mutexes.

Programming Languages

ruby
36898 projects - #4 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to sequel-pg advisory lock

Will paginate
Pagination library for Rails, Sinatra, Merb, DataMapper, and more
Stars: ✭ 5,621 (+28005%)
Mutual labels:  sequel
Sequel Ace
MySQL/MariaDB database management for macOS
Stars: ✭ 3,989 (+19845%)
Mutual labels:  sequel
discord-against-humanity
cards against humanity, integrated into discord
Stars: ✭ 21 (+5%)
Mutual labels:  sequel
doorkeeper-sequel
Doorkeeper Sequel ORM
Stars: ✭ 12 (-40%)
Mutual labels:  sequel
sinator
Sinatra application generator
Stars: ✭ 19 (-5%)
Mutual labels:  sequel
sqlike
Golang Sequel ORM that supports Enum, JSON, Spatial, and many more
Stars: ✭ 18 (-10%)
Mutual labels:  sequel
CopyAsMarkdownForSequelProBundle
Copy selected rows as Markdown. It's so easy to paste in GitHub issue.
Stars: ✭ 23 (+15%)
Mutual labels:  sequel
sequel-batches
Sequel extension for querying large datasets in batches
Stars: ✭ 16 (-20%)
Mutual labels:  sequel
sequel-seed
A Sequel extension to make seeds/fixtures manageable like migrations
Stars: ✭ 25 (+25%)
Mutual labels:  sequel

sequel-pg_advisory_lock

Build Status Gem Version

Gem sequel-pg_advisory_lock is an extension for ruby Sequel library that allows using PostgreSQL advisory locks for application-level mutexes.

Installation

Add this line to your application's Gemfile:

gem 'sequel-pg_advisory_lock'

and then execute:

$ bundle

Or install it yourself as:

$ gem install sequel-pg_advisory_lock

Usage

First, you should load an extension for Sequel::Database instance:

DB.extension :pg_advisory_lock

Then, you should register new lock by specifying unique name:

DB.register_advisory_lock(:my_lock)

By default, pg_advisory_lock PostgreSQL function will be associated with registered lock.

It's also possible to specify different function in second parameter of register_advisory_lock method, for example:

DB.register_advisory_lock(:my_lock, :pg_try_advisory_lock)

All supported lock functions are described here.

Finally, you can use registered lock:

DB.with_advisory_lock(:my_lock) do
  # do something
  # this block works like application-level mutex, 
  # so code inside block is protected from concurrent execution 
end

An optional 4-bytes integer parameter can be passed to with_advisory_lock method call:

DB.with_advisory_lock(:my_lock, 1) do
  # do something
  # this block works like application-level mutex, 
  # so code inside block is protected from concurrent execution 
end

Available types of locks

There are 4 supported PostgreSQL lock functions which can be used in register_advisory_lock:

  • pg_advisory_lock (default)

    Waits of lock releasing if someone already owns requested lock.

  • pg_try_advisory_lock

    Doesn't wait of lock releasing, returns nil if someone already owns requested lock.

  • pg_advisory_xact_lock

    Waits of lock releasing if someone already owns requested lock.
    Releases lock immediately after database transaction ends.
    Requires manually opened transaction before using this lock.

  • pg_try_advisory_xact_lock

    Doesn't wait of lock releasing, returns nil if someone already owns requested lock.
    Releases lock immediately after database transaction ends.
    Requires manually opened transaction before using this lock.

For more information see PostgreSQL documentation.

Contributing

  1. Fork the project (https://github.com/yuryroot/sequel-pg_advisory_lock).
  2. Create your feature branch (git checkout -b my-new-feature).
  3. Implement your feature or bug fix.
  4. Add tests for your feature or bug fix.
  5. Run rake to make sure all tests pass.
  6. Commit your changes (git commit -am 'Add new feature').
  7. Push to the branch (git push origin my-new-feature).
  8. Create new pull request.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].