-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Skip mysql create db * Cleanup mysql adapter * Add DBLock.with_lock * Update README * Rubocop
- Loading branch information
Showing
13 changed files
with
167 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
inherit_from: .rubocop_todo.yml | ||
|
||
AllCops: | ||
NewCops: enable | ||
TargetRubyVersion: 2.7 | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,25 @@ | ||
require 'active_support' | ||
require 'digest/md5' | ||
|
||
module DBLock | ||
extend self | ||
|
||
autoload :Adapter, 'db_lock/adapter' | ||
autoload :Lock, 'db_lock/lock' | ||
autoload :Locking, 'db_lock/locking' | ||
|
||
extend Locking | ||
|
||
class AlreadyLocked < StandardError; end | ||
|
||
attr_writer :db_handler | ||
|
||
def db_handler | ||
def self.db_handler | ||
# this must be an active record base object or subclass | ||
@db_handler || ActiveRecord::Base | ||
end | ||
|
||
custom_deprecator = ActiveSupport::Deprecation.new('1.0.0', 'DBLock') | ||
ActiveSupport::Deprecation.deprecate_methods(DBLock::Lock, get: 'use DBLock.with_lock instead', | ||
deprecator: custom_deprecator) | ||
ActiveSupport::Deprecation.deprecate_methods(DBLock::Lock, locked?: 'will be removed without replacement', | ||
deprecator: custom_deprecator) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
require 'digest/md5' | ||
|
||
module DBLock | ||
module Locking | ||
def with_lock(name, timeout = 0) | ||
timeout = timeout.to_f # catches nil | ||
timeout = 0 if timeout.negative? | ||
|
||
raise ArgumentError, "Invalid lock name: #{name.inspect}" if name.empty? | ||
raise AlreadyLocked, 'Already lock in progress' if locked? | ||
|
||
name = generate_lock_name(name) | ||
|
||
if Adapter.lock(name, timeout) | ||
@locked = true | ||
yield | ||
else | ||
raise AlreadyLocked, "Unable to obtain lock '#{name}' within #{timeout} seconds" unless locked? | ||
end | ||
ensure | ||
Adapter.release(name) if locked? | ||
@locked = false | ||
end | ||
|
||
private | ||
|
||
def locked? | ||
@locked ||= false | ||
end | ||
|
||
def generate_lock_name(name) | ||
name = "#{rails_app_name}.#{Rails.env}#{name}" if name[0] == '.' && defined? Rails | ||
# reduce lock names of > 64 chars in size | ||
# MySQL 5.7 only supports 64 chars max, there might be similar limitations elsewhere | ||
name = "#{name.chars.first(15).join}-#{Digest::MD5.hexdigest(name)}-#{name.chars.last(15).join}" if name.length > 64 | ||
name | ||
end | ||
|
||
def rails_app_name | ||
if Gem::Version.new(Rails.version) >= Gem::Version.new('6.0.0') | ||
Rails.application.class.module_parent_name | ||
else | ||
Rails.application.class.parent_name | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
require 'spec_helper' | ||
|
||
module DBLock | ||
RSpec.describe '.with_lock' do | ||
let(:name) { "custom_lock:db_lock:#{(0...8).map { rand(65..90).chr }.join}" } | ||
let(:timeout) { 5 } | ||
|
||
before do | ||
allow(Adapter).to receive(:lock).and_return(true) | ||
allow(Adapter).to receive(:release).and_return(true) | ||
end | ||
|
||
it 'uses the Adapter to receive and release the lock' do | ||
DBLock.with_lock(name, timeout) { sleep 0 } | ||
expect(Adapter).to have_received(:lock).with(name, timeout) | ||
expect(Adapter).to have_received(:release).with(name) | ||
end | ||
|
||
it 'limits lock names to 64 characters' do | ||
DBLock.with_lock("lock.name.exceeding.#{'asdf' * 10}.sixtyfour.characters", timeout) { sleep 0 } | ||
short_name = 'lock.name.excee-9782cc3fe0258bd32022ddfd0a24c8d4-four.characters' | ||
expect(Adapter).to have_received(:lock).with(short_name, timeout) | ||
expect(Adapter).to have_received(:release).with(short_name) | ||
end | ||
|
||
context 'when using dynamic lock names based on Rails app name' do | ||
# rubocop:disable RSpec/MessageChain | ||
before do | ||
allow(Rails).to receive_message_chain(:application, :class, :parent_name).and_return('Dummy') | ||
allow(Rails).to receive_message_chain(:application, :class, :module_parent_name).and_return('Dummy') | ||
end | ||
# rubocop:enable RSpec/MessageChain | ||
|
||
it 'supports lock names from rails app name' do | ||
DBLock.with_lock('.custom_lock', timeout) { sleep 0 } | ||
expect(Adapter).to have_received(:lock).with('Dummy.test.custom_lock', timeout) | ||
expect(Adapter).to have_received(:release).with('Dummy.test.custom_lock') | ||
end | ||
end | ||
|
||
context 'when the lock can be achieved' do | ||
before do | ||
allow(Adapter).to receive(:lock).and_return(true) | ||
end | ||
|
||
it 'executes the block' do | ||
x = 0 | ||
DBLock.with_lock(name) { x += 1 } | ||
expect(x).to eq(1) | ||
end | ||
|
||
it 'passes through errors but still frees the lock' do | ||
expect do | ||
DBLock.with_lock(name, timeout) { raise 'something happened' } | ||
end.to raise_error(RuntimeError) | ||
expect(Adapter).to have_received(:release) | ||
end | ||
end | ||
|
||
context 'when the lock can not be achieved' do | ||
before do | ||
allow(Adapter).to receive(:lock).and_return(false) | ||
end | ||
|
||
it 'raises an error and does not execute the block' do | ||
x = 0 | ||
expect { DBLock.with_lock(name, 0) { x += 1 } }.to raise_error(DBLock::AlreadyLocked) | ||
expect(x).to eq(0), 'the block was executed' | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
MssqlA = Class.new(ActiveRecord::Base) | ||
MssqlB = Class.new(ActiveRecord::Base) | ||
MysqlA = Class.new(ActiveRecord::Base) | ||
MysqlB = Class.new(ActiveRecord::Base) | ||
ModelMysql = Class.new(ActiveRecord::Base) | ||
ModelPostgres = Class.new(ActiveRecord::Base) |