Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mkon/db_lock
Obtains manual db locks.
https://github.com/mkon/db_lock
database hacktoberfest mutex ruby
Last synced: 8 days ago
JSON representation
Obtains manual db locks.
- Host: GitHub
- URL: https://github.com/mkon/db_lock
- Owner: mkon
- License: mit
- Created: 2014-08-14T14:41:24.000Z (about 10 years ago)
- Default Branch: main
- Last Pushed: 2024-03-07T23:55:33.000Z (8 months ago)
- Last Synced: 2024-03-15T10:22:23.762Z (8 months ago)
- Topics: database, hacktoberfest, mutex, ruby
- Language: Ruby
- Homepage:
- Size: 110 KB
- Stars: 5
- Watchers: 3
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: MIT-LICENSE
Awesome Lists containing this project
README
# DBLock
[![Gem Version](https://badge.fury.io/rb/db_lock.svg)](https://badge.fury.io/rb/db_lock)
[![Tests](https://github.com/mkon/db_lock/actions/workflows/main.yml/badge.svg)](https://github.com/mkon/db_lock/actions/workflows/main.yml)Gem to obtain and release manual db locks. This can be utilized for example to make sure that certain rake tasks do not run in parallel on the same database (for example when cron jobs run for too long or are accidentally started multiple times). Currently only supports:
- MySQL
- Microsoft SQL Server
- Postgres## Installation
Add this line to your application's Gemfile:
gem 'db_lock'
then run `bundle`
## Usage
```ruby
DBLock.with_lock('name_of_lock', 5) do
# code here
end
```Before the code block is executed, it will attempt to acquire a db lock for X seconds (5 in this example). If this fails it will raise an `DBLock::AlreadyLocked` error. The lock is released after the block is executed, even if the block raised an error itself.
The locking will already fail with an error if the current thread already holds a lock via this gem.
Locks are achieved on the database via:
| Database | Locking method |
|-----------|--------------------|
| MySQL | `GET_LOCK` |
| Postgres | `pg_advisory_lock` |
| SQLServer | `sp_getapplock` |## Dynamic lock name
If you prefix the lock with a `.` in a Rails application, `.` will be automatically replaced with `YourAppName.environment` (production/development/etc).
If the lock name exceeds 64 characters, it will be replaced with a lock name of 64 characters, that consists of a pre- and suffix from the original lock name and a middle MD5 checksum.## Development
Bundle with the adapter you want to use, for example
```bash
$ bundle --with mysql
```Run rspec with the database url env variables set. It will only run the specs it can run and skip the others.
For example
```bash
$ MYSQL_URL=mysql2://root:dummy@localhost/test SQLSERVER_URL=sqlserver://root:dummy@localhost/test rspec
```