-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow custom locking mechanism
This PR adds a new config option to allow using the Metricks gem without also requiring the [with_advisory_lock gem](https://github.com/ClosureTree/with_advisory_lock). Now it's possible to define a Rails initializer that provides a custom proc for performing the lock when a metric is recorded. ```ruby # config/initializers/metricks.rb Rails.application.config.metricks.with_lock = proc do |key, opts, block| opts ||= {} timeout_seconds = opts[:timeout_seconds] || 60 MyCustomLock.with_lock(key: key, timeout_seconds: timeout_seconds, &block) end ``` If the initializer is not defined then the existing with_advisory_lock behaviour continues to work as it does now. The runtime dependency has been removed from `metricks.gemspec` which would previously verify the required presence of the with_advisory_gem in the host Rails app during bundle install. This has been replaced with a runtime check when the Metricks engine is initialized. ActiveRecord and Rails 7.1 have been added to the tests.
- Loading branch information
1 parent
08df95b
commit 052d2ad
Showing
12 changed files
with
210 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,6 @@ gemfiles | |
|
||
## Ignore lock file | ||
Gemfile.lock | ||
|
||
# Ignore all logfiles | ||
/log/* |
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 |
---|---|---|
|
@@ -6,4 +6,5 @@ group :development do | |
gem 'appraisal', '~> 2.4' | ||
gem 'rspec' | ||
gem 'sqlite3', '~> 1.4' | ||
gem 'with_advisory_lock' | ||
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,40 @@ | ||
require 'with_advisory_lock' if defined?(WithAdvisoryLock) | ||
|
||
# The default locking mechanism is to use the with_advisory_lock gem | ||
# But this can be overriden using an initializer in the host Rails application (refer to README.md) | ||
# This is set in lib/metricks/engine.rb | ||
# Because of this, the with_advisory_lock gem is not a hard dependency. | ||
module Metricks | ||
class Lock | ||
|
||
class << self | ||
attr_accessor :with_lock | ||
|
||
def with_lock(key, opts = {}, &block) | ||
with_lock_block = @with_lock || default_with_lock | ||
|
||
instance_exec(key, opts, block, &with_lock_block) | ||
end | ||
|
||
def validate! | ||
return if @with_lock.present? | ||
return if defined?(WithAdvisoryLock) | ||
|
||
|
||
raise Metricks::Error.new( | ||
'ConfigurationMissing', | ||
message: 'By default Metricks requires with_advisory_lock gem to be installed. ' \ | ||
'Alternatively a custom locking mechanism can be configured via config.metricks.with_lock' | ||
) | ||
end | ||
|
||
private | ||
|
||
def default_with_lock | ||
proc do |key, opts, block| | ||
ActiveRecord::Base.with_advisory_lock(key, opts, &block) | ||
end | ||
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
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,35 @@ | ||
require 'spec_helper' | ||
require 'metricks/engine' | ||
|
||
describe Metricks::Engine do | ||
|
||
let(:mock_app) do | ||
Class.new(Rails::Application) do | ||
config.eager_load = false | ||
end | ||
end | ||
|
||
before do | ||
allow(Metricks::Lock).to receive(:validate!).and_call_original | ||
end | ||
|
||
it 'allows with_lock to be configured' do | ||
success = false | ||
|
||
allow(mock_app.config.metricks).to receive(:with_lock) | ||
.and_return(->(result, opts, block) { block.call(result, opts) }) | ||
|
||
expect { | ||
mock_app.initialize! | ||
}.not_to raise_error | ||
|
||
Metricks::Lock.with_lock(true, {}) do |result| | ||
success = result | ||
end | ||
|
||
expect(success).to be(true) | ||
|
||
expect(Metricks::Lock).to have_received(:validate!) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
require 'spec_helper' | ||
require 'with_advisory_lock' | ||
require 'metricks/lock' | ||
|
||
describe Metricks::Lock do | ||
|
||
describe ".with_lock" do | ||
before do | ||
allow(ActiveRecord::Base).to receive(:with_advisory_lock).and_call_original | ||
end | ||
|
||
context "when with_lock is set" do | ||
before do | ||
Metricks::Lock.with_lock = ->(key, opts, block) { block.call(key, opts) } | ||
end | ||
|
||
it "calls the block with the args" do | ||
success = false | ||
passed_opts = {} | ||
|
||
Metricks::Lock.with_lock(true, {hi: 'there'}) do |result, opts| | ||
success = result | ||
passed_opts = opts | ||
end | ||
|
||
expect(success).to be(true) | ||
expect(passed_opts).to eq({hi: 'there'}) | ||
expect(ActiveRecord::Base).not_to have_received(:with_advisory_lock) | ||
end | ||
end | ||
|
||
context "when with_lock is not set" do | ||
before do | ||
Metricks::Lock.with_lock = nil | ||
end | ||
|
||
it "uses with_advisory_lock" do | ||
success = false | ||
|
||
Metricks::Lock.with_lock(true, timeout_seconds: 5) do |result, opts| | ||
success = true | ||
end | ||
|
||
expect(success).to be(true) | ||
expect(ActiveRecord::Base).to have_received(:with_advisory_lock) | ||
.with(true, {timeout_seconds: 5}) | ||
end | ||
end | ||
end | ||
|
||
describe ".validate!" do | ||
context "when with_lock is set" do | ||
before do | ||
Metricks::Lock.with_lock = ->(key, opts, block) { block.call } | ||
hide_const("WithAdvisoryLock") | ||
end | ||
|
||
it "does not raise an error" do | ||
expect { Metricks::Lock.validate! }.not_to raise_error | ||
end | ||
end | ||
|
||
context "when with_lock is not set and WithAdvisoryLock is defined" do | ||
before do | ||
stub_const("WithAdvisoryLock", true) | ||
end | ||
|
||
it "does not raise an error" do | ||
expect { Metricks::Lock.validate! }.not_to raise_error | ||
end | ||
end | ||
|
||
context "when with_lock is not set and WithAdvisoryLock is not defined" do | ||
before do | ||
Metricks::Lock.with_lock = nil | ||
hide_const("WithAdvisoryLock") | ||
end | ||
|
||
it "raises an error" do | ||
expect { Metricks::Lock.validate! }.to raise_error(Metricks::Error) | ||
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