Class: SolidQueue::Semaphore::Proxy

Inherits:
Object
  • Object
show all
Defined in:
app/models/solid_queue/semaphore.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(job) ⇒ Proxy

Returns a new instance of Proxy.



44
45
46
# File 'app/models/solid_queue/semaphore.rb', line 44

def initialize(job)
  @job = job
end

Class Method Details

.signal_all(jobs) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'app/models/solid_queue/semaphore.rb', line 34

def self.signal_all(jobs)
  # Guard against incrementing a semaphore's value beyond its limit. Jobs can
  # have different limits, so group them and cap each group with `value < limit`.
  jobs.group_by { |job| job.concurrency_limit || 1 }.each do |limit, grouped_jobs|
    Semaphore.where(key: grouped_jobs.map(&:concurrency_key))
      .where(value: ...limit)
      .update_all("value = value + 1")
  end
end

Instance Method Details

#signalObject



56
57
58
# File 'app/models/solid_queue/semaphore.rb', line 56

def signal
  attempt_increment
end

#waitObject



48
49
50
51
52
53
54
# File 'app/models/solid_queue/semaphore.rb', line 48

def wait
  if semaphore = Semaphore.lock.find_by(key: key)
    semaphore.value > 0 && attempt_decrement
  else
    attempt_creation
  end
end