Class: Disqualified::Pool

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/disqualified/pool.rb

Constant Summary collapse

CHECK =
:check
QUIT =
:quit
RUN =
:run

Constants included from Logging

Logging::ERROR_CONTEXT_TYPE, Logging::ERROR_HOOK_TYPE

Instance Method Summary collapse

Methods included from Logging

format_log, handle_error

Constructor Details

#initialize(delay_range:, logger:, pool_size:, error_hooks:, &task) ⇒ Pool

Returns a new instance of Pool.



8
9
10
11
12
13
14
15
16
# File 'lib/disqualified/pool.rb', line 8

def initialize(delay_range:, logger:, pool_size:, error_hooks:, &task)
  @delay_range = delay_range
  @logger = logger
  @pool_size = pool_size
  @error_hooks = error_hooks
  @task = task
  @running = Concurrent::AtomicBoolean.new(true)
  @command_queue = Thread::Queue.new
end

Instance Method Details

#clockObject



35
36
37
38
39
40
41
42
43
44
# File 'lib/disqualified/pool.rb', line 35

def clock
  @clock ||= Concurrent::TimerTask.new(run_now: true) do |clock_task|
    @logger.debug { format_log("Disqualified::Pool#clock", "Starting") }
    clock_task.execution_interval = random_interval
    @command_queue.push(Disqualified::Pool::CHECK)
    @logger.debug { format_log("Disqualified::Pool#clock", "Next run in #{clock_task.execution_interval}") }
  rescue => e
    handle_error(@error_hooks, e, {})
  end
end

#poolObject



46
47
48
49
50
51
# File 'lib/disqualified/pool.rb', line 46

def pool
  @pool ||= @pool_size.times.map do |promise_index|
    repeat(promise_index:)
      &.run
  end
end

#repeat(promise_index:) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/disqualified/pool.rb', line 53

def repeat(promise_index:)
  if @running.false?
    return
  end

  @logger.debug { format_log("Disqualified::Pool#repeat(#{promise_index})", "Started") }

  args = {promise_index:}

  Concurrent::Promises
    .future(args) do |args|
      @logger.debug { format_log("Disqualified::Pool#repeat(#{promise_index}) <pre-exec>", "Waiting for command") }
      command = @command_queue.pop
      @logger.debug { format_log("Disqualified::Pool#repeat(#{promise_index}) <pre-exec>", "Command: #{command}") }

      case command
      when Disqualified::Pool::QUIT
        nil
      when Disqualified::Pool::CHECK
        Rails.application.reloader.wrap do
          pending_job_count = Disqualified::Record
            .where(finished_at: nil, run_at: (..Time.now), locked_by: nil)
            .count

          pending_job_count.times do
            @command_queue.push(Disqualified::Pool::RUN)
          end
        end
      when Disqualified::Pool::RUN
        @task.call(args)
      end
    rescue => e
      handle_error(@error_hooks, e, {})
    end
    .then { repeat(promise_index:) }
end

#run!Object



18
19
20
21
22
23
24
25
# File 'lib/disqualified/pool.rb', line 18

def run!
  clock.execute
  Concurrent::Promises
    .zip(*pool)
    .rescue { |error| handle_error(@error_hooks, error, {}) }
    .run
    .value!
end

#shutdownObject



27
28
29
30
31
32
33
# File 'lib/disqualified/pool.rb', line 27

def shutdown
  @running.make_false
  clock.shutdown
  @pool_size.times do
    @command_queue.push(Disqualified::Pool::QUIT)
  end
end