DaemonSpawnを使用しているクラスで、デーモン化するかどうかを選べるようにする

デーモン化するプログラムで、普段はデーモン化したいがデバッグなどでデーモン化したくない時等にオプションで選択出来るようにしてみました。

継承するクラスをARGVの値をみて動的に変えました。

デーモン起動:ruby program.rb start

デーモン終了: ruby program.rb stop

通常起動: ruby program.rb nodaemon

 

def inheritance
  if ARGV[0] == "nodaemon"
    Object
  else
    DaemonSpawn::Base
  end
end

class Klass < inheritance
  def hoge
  end
  
  def start
    # ループするプログラム
  end
  
  # 使用しなくても必須
  def stop
  end
end


if ARGV[0]=="nodaemon"
  tfb = CollectTweet.new
  tfb.start ARGV
else
  CollectTweet.spawn!({
      :working_dir => Rails.root,
      :pid_file => pid_file,
      :log_file => log_file,
      :sync_log => true,
      :singleton => true
  })
end