2018-10-03 15:22:52 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Scheduler
|
|
|
|
class Daemon
|
|
|
|
attr_reader :pid
|
|
|
|
|
2024-11-19 20:06:47 +01:00
|
|
|
class << self
|
|
|
|
def start
|
|
|
|
new.start
|
|
|
|
end
|
2018-10-03 15:22:52 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
@parent_pid = Process.pid
|
|
|
|
end
|
|
|
|
|
|
|
|
def start
|
|
|
|
return if pid
|
|
|
|
|
|
|
|
@pid = fork do
|
|
|
|
monitor_parent
|
|
|
|
run
|
|
|
|
end
|
|
|
|
monitor_child
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def run
|
2024-11-19 20:06:47 +01:00
|
|
|
load("bin/schedule")
|
2018-10-03 15:22:52 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def monitor_child
|
|
|
|
return if @child_monitor
|
|
|
|
|
|
|
|
@child_monitor = Thread.new do
|
|
|
|
loop do
|
2024-11-19 20:06:47 +01:00
|
|
|
sleep(5)
|
2018-10-03 15:22:52 +02:00
|
|
|
unless alive?(pid)
|
|
|
|
@pid = nil
|
|
|
|
start
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def monitor_parent
|
|
|
|
Thread.new do
|
|
|
|
loop do
|
|
|
|
exit unless alive?(@parent_pid)
|
2024-11-19 20:06:47 +01:00
|
|
|
sleep(1)
|
2018-10-03 15:22:52 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def alive?(pid)
|
|
|
|
Process.getpgid(pid)
|
|
|
|
true
|
|
|
|
rescue Errno::ESRCH
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|