-
-
Notifications
You must be signed in to change notification settings - Fork 276
/
Copy pathuntil_and_while_executing.rb
71 lines (61 loc) · 1.97 KB
/
until_and_while_executing.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# frozen_string_literal: true
module SidekiqUniqueJobs
class Lock
# Locks jobs while the job is executing in the server process
# - Locks on perform_in or perform_async (see {UntilExecuting})
# - Unlocks before yielding to the worker's perform method (see {UntilExecuting})
# - Locks before yielding to the worker's perform method (see {WhileExecuting})
# - Unlocks after yielding to the worker's perform method (see {WhileExecuting})
#
# See {#lock} for more information about the client.
# See {#execute} for more information about the server
#
# @author Mikael Henriksson <[email protected]>
class UntilAndWhileExecuting < 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(origin: :client, &block)
unless (token = locksmith.lock)
reflect(:lock_failed, item)
call_strategy(origin: origin, &block)
return
end
yield if block
token
end
# Executes in the Sidekiq server process
# @yield to the worker class perform method
def execute
if locksmith.unlock
# ensure_relocked do
runtime_lock.execute { return yield }
# end
else
reflect(:unlock_failed, item)
end
rescue Exception # rubocop:disable Lint/RescueException
reflect(:execution_failed, item)
locksmith.lock(wait: 2)
raise
end
private
def ensure_relocked
yield
rescue Exception # rubocop:disable Lint/RescueException
reflect(:execution_failed, item)
locksmith.lock
raise
end
def runtime_lock
@runtime_lock ||= SidekiqUniqueJobs::Lock::WhileExecuting.new(item.dup, callback, redis_pool)
end
end
end
end