Created
June 29, 2012 16:43
-
-
Save mikz/3019092 to your computer and use it in GitHub Desktop.
October Daemon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module October | |
class Daemon | |
attr_reader :child, :root, :env | |
def initialize(root, env = nil) | |
@root = root | |
@env = env | |
end | |
def spawn! | |
@child = fork do | |
ENV['OCTOBER_ENV'] ||= env | |
require File.join(root, "boot") | |
require 'october' | |
@bot = October::Base.new | |
@bot.start | |
end | |
end | |
def loop! | |
loop do | |
begin | |
Process.wait(spawn!) | |
warn "process #{child} ended" | |
rescue Errno::ECHILD | |
warn "process #{child} died or was killed" | |
end | |
warn "spawning new one in next iteration" | |
sleep(1) # throttle spawning in case anything fails | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment