-
-
Notifications
You must be signed in to change notification settings - Fork 273
/
simulate_lock.rb
54 lines (44 loc) · 1.49 KB
/
simulate_lock.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
# frozen_string_literal: true
require "concurrent/array"
module SimulateLock
extend self
@items = Concurrent::Array.new
def lock_until_executed(digest, jid, ttl = nil)
item = get_item(digest: digest, jid: jid, lock_type: :until_executed, ttl: ttl)
lock(item)
end
def lock_until_expired(digest, jid, ttl)
item = get_item(digest: digest, jid: jid, lock_type: :until_expired, ttl: ttl)
lock(item)
end
def lock_until_and_while_executing(digest, jid, ttl = nil)
item = get_item(digest: digest, jid: jid, lock_type: :until_expired, ttl: ttl)
lock(item)
end
def lock_while_executing(digest, jid, ttl = nil)
item = get_item(digest: "#{digest}:RUN", jid: jid, lock_type: :while_executing, ttl: ttl)
lock(item)
end
def runtime_lock(digest, jid, ttl = nil)
item = get_item(digest: digest, jid: jid, lock_type: :while_executing, ttl: ttl)
lock(item)
item = get_item(digest: "#{digest}:RUN", jid: "randomjid", lock_type: :while_executing, ttl: ttl)
lock(item)
end
def lock(item)
SidekiqUniqueJobs::Locksmith.new(item).lock
end
def unlock(item)
SidekiqUniqueJobs::Locksmith.new(item).unlock
end
def get_item(digest: "randomdigest", jid: "randomjid", lock_type: :until_executed, ttl: nil)
item = {
SidekiqUniqueJobs::UNIQUE_DIGEST_KEY => digest,
SidekiqUniqueJobs::JID_KEY => jid,
SidekiqUniqueJobs::LOCK_EXPIRATION_KEY => ttl,
SidekiqUniqueJobs::LOCK_KEY => lock_type,
}
@items << item
item
end
end