README.md in sidekiq-lock-0.2.0 vs README.md in sidekiq-lock-0.3.0
- old
+ new
@@ -56,35 +56,45 @@
So now in your worker class you can call (whenever you need):
- `lock.acquire!` - will try to acquire the lock, if returns false on failure (that means some other process / thread took the lock first)
- `lock.acquired?` - set to `true` when lock is successfully acquired
-- `lock.release!` - deletes the lock (if not already expired / taken by another process)
+- `lock.release!` - deletes the lock (only if it's: acquired by current thread and not already expired)
### Lock options
sidekiq_options lock will accept static values or `Proc` that will be called on argument(s) passed to `perform` method.
- timeout - specified expire time, in milliseconds
- name - name of the redis key that will be used as lock name
+- value - (optional) value of the lock, if not provided it's set to random hex
Dynamic lock example:
``` ruby
class Worker
include Sidekiq::Worker
include Sidekiq::Lock::Worker
sidekiq_options lock: {
timeout: proc { |user_id, timeout| timeout * 2 },
- name: proc { |user_id, timeout| "lock:peruser:#{user_id}" }
+ name: proc { |user_id, timeout| "lock:peruser:#{user_id}" },
+ value: proc { |user_id, timeout| "#{user_id}" }
}
def perform(user_id, timeout)
# ...
# do some work
# only at this point I want to acquire the lock
if lock.acquire!
- # I can do the work
+ begin
+ # I can do the work
+ # ...
+ ensure
+ # You probably want to manually release lock after work is done
+ # This method can be safely called even if lock wasn't acquired
+ # by current worker (thread). For more references see RedisLock class
+ lock.release!
+ end
else
# reschedule, raise an error or do whatever you want
end
end
end