Skip to content

Commit

Permalink
Allow lock method name configuration, closes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
rwojsznis committed Dec 7, 2013
1 parent ca50d52 commit 994cda0
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 5 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
## 0.2.0 (in progress)

- ability to globally configure `lock` method name

``` ruby
Sidekiq.configure_server do |config|
config.lock_method = :redis_lock
end
```

## 0.0.1

- Initial release
14 changes: 12 additions & 2 deletions lib/sidekiq/lock.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
require "sidekiq/lock/version"
require "sidekiq/lock/worker"
require "sidekiq/lock/middleware"
require "sidekiq/lock/redis_lock"
require "sidekiq/lock/version"
require "sidekiq/lock/worker"

module Sidekiq

def self.lock_method
@lock_method || :lock
end

def self.lock_method=(method)
@lock_method = method
end

module Lock
THREAD_KEY = :sidekiq_lock
end
Expand All @@ -14,3 +23,4 @@ module Lock
chain.add Sidekiq::Lock::Middleware
end
end

6 changes: 4 additions & 2 deletions lib/sidekiq/lock/worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ module Sidekiq
module Lock
module Worker

def lock
Thread.current[Sidekiq::Lock::THREAD_KEY]
def self.included(base)
base.send(:define_method, Sidekiq.lock_method) do
Thread.current[Sidekiq::Lock::THREAD_KEY]
end
end

end
Expand Down
2 changes: 1 addition & 1 deletion test/lib/middleware_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def thread_variable
before do
Sidekiq.redis = REDIS
Sidekiq.redis { |c| c.flushdb }
Thread.current[Sidekiq::Lock::THREAD_KEY] = nil
clear_lock_variable
end

let(:handler){ Sidekiq::Lock::Middleware.new }
Expand Down
15 changes: 15 additions & 0 deletions test/lib/worker_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,26 @@ module Sidekiq
module Lock
describe Worker do

after { clear_lock_variable }

it 'sets lock method that points to thread variable' do
Thread.current[Sidekiq::Lock::THREAD_KEY] = "test"
assert_equal "test", LockWorker.new.lock
end

it 'allows method name configuration' do
Sidekiq.lock_method = :custom_lock_name

class WorkerWithCustomLockName
include Sidekiq::Worker
include Sidekiq::Lock::Worker
end

Thread.current[Sidekiq::Lock::THREAD_KEY] = "custom_name"

assert_equal "custom_name", WorkerWithCustomLockName.new.custom_lock_name
end

end
end
end
4 changes: 4 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ def redis(command, *args)
c.send(command, *args)
end
end

def clear_lock_variable
Thread.current[Sidekiq::Lock::THREAD_KEY] = nil
end

0 comments on commit 994cda0

Please sign in to comment.