Class: MiniScheduler::Manager::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/mini_scheduler/manager.rb

Instance Method Summary collapse

Constructor Details

#initialize(manager) ⇒ Runner

Returns a new instance of Runner.



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
# File 'lib/mini_scheduler/manager.rb', line 6

def initialize(manager)
  @stopped = false
  @mutex = Mutex.new
  @queue = Queue.new
  @manager = manager
  @reschedule_orphans_thread = Thread.new do
    while !@stopped
      sleep 1.minute
      @mutex.synchronize do
        reschedule_orphans
      end
    end
  end
  @keep_alive_thread = Thread.new do
    while !@stopped
      @mutex.synchronize do
        keep_alive
      end
      sleep (@manager.keep_alive_duration / 2)
    end
  end
  @thread = Thread.new do
    while !@stopped
      process_queue
    end
  end
end

Instance Method Details

#attempts(n) ⇒ Object



153
154
155
156
157
158
159
160
161
# File 'lib/mini_scheduler/manager.rb', line 153

def attempts(n)
  n.times {
    begin
      yield; break
    rescue
      sleep Random.rand
    end
  }
end

#enq(klass) ⇒ Object



138
139
140
# File 'lib/mini_scheduler/manager.rb', line 138

def enq(klass)
  @queue << klass
end

#hostnameObject



46
47
48
49
50
51
52
# File 'lib/mini_scheduler/manager.rb', line 46

def hostname
  @hostname ||= begin
                  `hostname`
                rescue
                  "unknown"
                end
end

#keep_aliveObject



34
35
36
37
38
# File 'lib/mini_scheduler/manager.rb', line 34

def keep_alive
  @manager.keep_alive
rescue => ex
  MiniScheduler.handle_job_exception(ex, message: "Scheduling manager keep-alive")
end

#process_queueObject



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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/mini_scheduler/manager.rb', line 54

def process_queue

  klass = @queue.deq
  return unless klass

  # hack alert, I need to both deq and set @running atomically.
  @running = true
  failed = false
  start = Time.now.to_f
  info = @mutex.synchronize { @manager.schedule_info(klass) }
  stat = nil
  error = nil

  begin
    info.prev_result = "RUNNING"
    @mutex.synchronize { info.write! }

    if @manager.enable_stats
      stat = MiniScheduler::Stat.create!(
        name: klass.to_s,
        hostname: hostname,
        pid: Process.pid,
        started_at: Time.now,
        live_slots_start: GC.stat[:heap_live_slots]
      )
    end

    klass.new.perform
  rescue => e
    MiniScheduler.handle_job_exception(e, message: "Running a scheduled job", job: klass)

    error = "#{e.class}: #{e.message} #{e.backtrace.join("\n")}"
    failed = true
  end
  duration = ((Time.now.to_f - start) * 1000).to_i
  info.prev_duration = duration
  info.prev_result = failed ? "FAILED" : "OK"
  info.current_owner = nil
  if stat
    stat.update!(
      duration_ms: duration,
      live_slots_finish: GC.stat[:heap_live_slots],
      success: !failed,
      error: error
    )
    MiniScheduler.job_ran&.call(stat)
  end
  attempts(3) do
    @mutex.synchronize { info.write! }
  end
rescue => ex
  MiniScheduler.handle_job_exception(ex, message: "Processing scheduled job queue")
ensure
  @running = false
  if defined?(ActiveRecord::Base)
    ActiveRecord::Base.connection_handler.clear_active_connections!
  end
end

#reschedule_orphansObject



40
41
42
43
44
# File 'lib/mini_scheduler/manager.rb', line 40

def reschedule_orphans
  @manager.reschedule_orphans!
rescue => ex
  MiniScheduler.handle_job_exception(ex, message: "Scheduling manager orphan rescheduler")
end

#stop!Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/mini_scheduler/manager.rb', line 113

def stop!
  return if @stopped

  @mutex.synchronize do
    @stopped = true

    @keep_alive_thread.kill
    @reschedule_orphans_thread.kill

    @keep_alive_thread.join
    @reschedule_orphans_thread.join

    enq(nil)

    kill_thread = Thread.new do
      sleep 0.5
      @thread.kill
    end

    @thread.join
    kill_thread.kill
    kill_thread.join
  end
end

#wait_till_doneObject



142
143
144
145
146
147
148
149
150
151
# File 'lib/mini_scheduler/manager.rb', line 142

def wait_till_done
  while !@queue.empty? && !(@queue.num_waiting > 0)
    sleep 0.001
  end
  # this is a hack, but is only used for test anyway
  sleep 0.001
  while @running
    sleep 0.001
  end
end