#!/usr/bin/env ruby $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) require 'getoptlong' require 'rubygems' require 'daemons' require 'racoon' require 'csv' def usage puts "Usage: racoon-worker [switches]" puts " --beanstalk <127.0.0.1:11300> csv list of ip:port for beanstalk servers" puts " --tube the beanstalk tube to use" puts " --pid the path to store the pid" puts " --log the path to store the log" puts " --daemon to daemonize the server" puts " --help this message" end def daemonize Daemonize.daemonize(@log_file, 'racoon-worker') open(@pid_file,"w") { |f| f.write(Process.pid) } open(@pid_file,"w") do |f| f.write(Process.pid) File.chmod(0644, @pid_file) end end opts = GetoptLong.new( ["--beanstalk", "-b", GetoptLong::REQUIRED_ARGUMENT], ["--tube", "-t", GetoptLong::REQUIRED_ARGUMENT], ["--pid", "-i", GetoptLong::REQUIRED_ARGUMENT], ["--log", "-l", GetoptLong::REQUIRED_ARGUMENT], ["--help", "-h", GetoptLong::NO_ARGUMENT], ["--daemon", "-d", GetoptLong::NO_ARGUMENT] ) beanstalks = ["127.0.0.1:11300"] tube = 'racoon' @pid_file = '/var/run/racoon-worker.pid' @log_file = '/var/log/racoon-worker.log' daemon = false opts.each do |opt, arg| case opt when '--help' usage exit 1 when '--beanstalk' beanstalks = CSV.parse(arg)[0] when '--tube' tube = arg when '--pid' @pid_file = arg when '--log' @log_file = arg when '--daemon' daemon = true end end Racoon::Config.logger = Logger.new(@log_file) if daemon daemonize else puts "Starting racoon worker." end Racoon::Worker.new(beanstalks, tube).start!