-
-
Notifications
You must be signed in to change notification settings - Fork 277
/
Copy pathuntil_executed.rb
48 lines (41 loc) · 1.13 KB
/
until_executed.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# frozen_string_literal: true
module SidekiqUniqueJobs
class Lock
# Locks jobs until the server is done executing the job
# - Locks on perform_in or perform_async
# - Unlocks after yielding to the worker's perform method
#
# @author Mikael Henriksson <[email protected]>
class UntilExecuted < BaseLock
#
# Locks a sidekiq job
#
# @note Will call a conflict strategy if lock can't be achieved.
#
# @return [String, nil] the locked jid when properly locked, else nil.
#
# @yield to the caller when given a block
#
def lock(&block)
unless (token = locksmith.lock)
reflect(:lock_failed, item)
call_strategy(origin: :client, &block)
return
end
yield if block
token
end
# Executes in the Sidekiq server process
# @yield to the worker class perform method
def execute
executed = locksmith.execute do
yield
ensure
unlock_and_callback
end
reflect(:execution_failed, item) unless executed
nil
end
end
end
end