Class: Skiplock::Dispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/skiplock/dispatcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(master: true) ⇒ Dispatcher

Returns a new instance of Dispatcher.



3
4
5
6
7
8
# File 'lib/skiplock/dispatcher.rb', line 3

def initialize(master: true)
  @executor = Concurrent::ThreadPoolExecutor.new(min_threads: Settings['min_threads'], max_threads: Settings['max_threads'], max_queue: Settings['max_threads'], idletime: 60, auto_terminate: true, fallback_policy: :discard)
  @master = master
  @next_schedule_at = Time.now.to_f
  @running = true
end

Instance Method Details

#runObject



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
# File 'lib/skiplock/dispatcher.rb', line 10

def run
  Thread.new do
    Rails.application.reloader.wrap do
      sleep(0.1) while @running && !Rails.application.initialized?
      ActiveRecord::Base.connection_pool.with_connection do |connection|
        connection.exec_query('LISTEN skiplock')
        if @master
          # reset retries schedules on startup
          Job.where('scheduled_at > NOW() AND executions IS NOT NULL AND expired_at IS NULL AND finished_at IS NULL').update_all(scheduled_at: nil, updated_at: Time.now)
          Cron.setup
        end
        error = false
        while @running
          begin
            if error
              unless connection.active?
                connection.reconnect!
                sleep(0.5)
                connection.exec_query('LISTEN skiplock')
                @next_schedule_at = Time.now
              end
              error = false
            end
            if Time.now.to_f >= @next_schedule_at && @executor.remaining_capacity > 0
              @executor.post { do_work }
            end
            notifications = []
            connection.raw_connection.wait_for_notify(0.1) do |channel, pid, payload|
              notifications << payload if payload
              loop do
                payload = connection.raw_connection.notifies
                break unless @running && payload
                notifications << payload[:extra]
              end
              notifications.each do |n|
                op, id, queue, priority, time = n.split(',')
                if time.to_f <= Time.now.to_f
                  @next_schedule_at = Time.now.to_f
                elsif time.to_f < @next_schedule_at
                  @next_schedule_at = time.to_f
                end
              end
            end
          rescue Exception => ex
            # TODO: Report exception
            error = true
            timestamp = Time.now
            while @running
              sleep(0.5)
              break if Time.now - timestamp > 10
            end              
          end
          sleep(0.1)
        end
        connection.exec_query('UNLISTEN *')
      end
    end
  end
end

#shutdown(wait: true) ⇒ Object



70
71
72
73
74
# File 'lib/skiplock/dispatcher.rb', line 70

def shutdown(wait: true)
  @running = false
  @executor.shutdown
  @executor.wait_for_termination if wait
end