|
1 | 1 | # frozen_string_literal: true
|
2 | 2 |
|
3 |
| -class SolidQueue::Semaphore < SolidQueue::Record |
4 |
| - scope :available, -> { where("value > 0") } |
5 |
| - scope :expired, -> { where(expires_at: ...Time.current) } |
| 3 | +module SolidQueue |
| 4 | + class Semaphore < Record |
| 5 | + scope :available, -> { where("value > 0") } |
| 6 | + scope :expired, -> { where(expires_at: ...Time.current) } |
6 | 7 |
|
7 |
| - class << self |
8 |
| - def wait(job) |
9 |
| - Proxy.new(job, self).wait |
10 |
| - end |
| 8 | + class << self |
| 9 | + def wait(job) |
| 10 | + Proxy.new(job).wait |
| 11 | + end |
11 | 12 |
|
12 |
| - def signal(job) |
13 |
| - Proxy.new(job, self).signal |
| 13 | + def signal(job) |
| 14 | + Proxy.new(job).signal |
| 15 | + end |
14 | 16 | end
|
15 |
| - end |
16 | 17 |
|
17 |
| - class Proxy |
18 |
| - def initialize(job, proxied_class) |
19 |
| - @job = job |
20 |
| - @proxied_class = proxied_class |
21 |
| - end |
| 18 | + class Proxy |
| 19 | + def initialize(job) |
| 20 | + @job = job |
| 21 | + @retries = 0 |
| 22 | + end |
22 | 23 |
|
23 |
| - def wait |
24 |
| - if semaphore = proxied_class.find_by(key: key) |
25 |
| - semaphore.value > 0 && attempt_decrement |
26 |
| - else |
27 |
| - attempt_creation |
| 24 | + def wait |
| 25 | + if semaphore = Semaphore.find_by(key: key) |
| 26 | + semaphore.value > 0 && attempt_decrement |
| 27 | + else |
| 28 | + attempt_creation |
| 29 | + end |
28 | 30 | end
|
29 |
| - end |
30 | 31 |
|
31 |
| - def signal |
32 |
| - attempt_increment |
33 |
| - end |
| 32 | + def signal |
| 33 | + attempt_increment |
| 34 | + end |
34 | 35 |
|
35 |
| - private |
36 |
| - attr_reader :job, :proxied_class |
| 36 | + private |
| 37 | + attr_reader :job, :retries |
37 | 38 |
|
38 |
| - def attempt_creation |
39 |
| - proxied_class.create!(key: key, value: limit - 1, expires_at: expires_at) |
40 |
| - true |
41 |
| - rescue ActiveRecord::RecordNotUnique |
42 |
| - attempt_decrement |
43 |
| - end |
| 39 | + def attempt_creation |
| 40 | + Semaphore.create!(key: key, value: limit - 1, expires_at: expires_at) |
| 41 | + true |
| 42 | + rescue ActiveRecord::RecordNotUnique |
| 43 | + attempt_decrement |
| 44 | + end |
44 | 45 |
|
45 |
| - def attempt_decrement |
46 |
| - proxied_class.available.where(key: key).update_all([ "value = value - 1, expires_at = ?", expires_at ]) > 0 |
47 |
| - end |
| 46 | + def attempt_decrement |
| 47 | + Semaphore.available.where(key: key).update_all([ "value = value - 1, expires_at = ?", expires_at ]) > 0 |
| 48 | + rescue ActiveRecord::Deadlocked |
| 49 | + if retriable? then attempt_retry |
| 50 | + else |
| 51 | + raise |
| 52 | + end |
| 53 | + end |
48 | 54 |
|
49 |
| - def attempt_increment |
50 |
| - proxied_class.where(key: key, value: ...limit).update_all([ "value = value + 1, expires_at = ?", expires_at ]) > 0 |
51 |
| - end |
| 55 | + def attempt_increment |
| 56 | + Semaphore.where(key: key, value: ...limit).update_all([ "value = value + 1, expires_at = ?", expires_at ]) > 0 |
| 57 | + end |
52 | 58 |
|
53 |
| - def key |
54 |
| - job.concurrency_key |
55 |
| - end |
| 59 | + def attempt_retry |
| 60 | + self.retries += 1 |
56 | 61 |
|
57 |
| - def expires_at |
58 |
| - job.concurrency_duration.from_now |
59 |
| - end |
| 62 | + if semaphore = Semaphore.find_by(key: key) |
| 63 | + semaphore.value > 0 && attempt_decrement |
| 64 | + end |
| 65 | + end |
60 | 66 |
|
61 |
| - def limit |
62 |
| - job.concurrency_limit |
63 |
| - end |
| 67 | + MAX_RETRIES = 1 |
| 68 | + |
| 69 | + def retriable? |
| 70 | + retries < MAX_RETRIES |
| 71 | + end |
| 72 | + |
| 73 | + def key |
| 74 | + job.concurrency_key |
| 75 | + end |
| 76 | + |
| 77 | + def expires_at |
| 78 | + job.concurrency_duration.from_now |
| 79 | + end |
| 80 | + |
| 81 | + def limit |
| 82 | + job.concurrency_limit |
| 83 | + end |
| 84 | + end |
64 | 85 | end
|
65 | 86 | end
|
0 commit comments