| 1 |
#!/usr/bin/env ruby |
|---|
| 2 |
|
|---|
| 3 |
RAILS_HOME = File.expand_path(File.join(File.dirname(__FILE__),"..")) |
|---|
| 4 |
BDRB_HOME = File.join(RAILS_HOME,"vendor","plugins","backgroundrb") |
|---|
| 5 |
WORKER_ROOT = File.join(RAILS_HOME,"lib","workers") |
|---|
| 6 |
WORKER_LOAD_ENV = File.join(RAILS_HOME,"script","load_worker_env") |
|---|
| 7 |
|
|---|
| 8 |
["server","server/lib","lib","lib/backgroundrb"].each { |x| $LOAD_PATH.unshift(BDRB_HOME + "/#{x}")} |
|---|
| 9 |
$LOAD_PATH.unshift(WORKER_ROOT) |
|---|
| 10 |
|
|---|
| 11 |
require "rubygems" |
|---|
| 12 |
require "yaml" |
|---|
| 13 |
require "erb" |
|---|
| 14 |
require "logger" |
|---|
| 15 |
require "packet" |
|---|
| 16 |
require "optparse" |
|---|
| 17 |
|
|---|
| 18 |
require "bdrb_config" |
|---|
| 19 |
require RAILS_HOME + "/config/boot" |
|---|
| 20 |
require "active_support" |
|---|
| 21 |
|
|---|
| 22 |
BackgrounDRb::Config.parse_cmd_options ARGV |
|---|
| 23 |
BDRB_CONFIG = BackgrounDRb::Config.read_config("#{RAILS_HOME}/config/backgroundrb.yml") |
|---|
| 24 |
|
|---|
| 25 |
require RAILS_HOME + "/config/environment" |
|---|
| 26 |
require "bdrb_job_queue" |
|---|
| 27 |
require "backgroundrb_server" |
|---|
| 28 |
|
|---|
| 29 |
pid_file = "#{RAILS_HOME}/tmp/pids/backgroundrb_#{BDRB_CONFIG[:backgroundrb][:port]}.pid" |
|---|
| 30 |
SERVER_LOGGER = "#{RAILS_HOME}/log/backgroundrb_debug_#{BDRB_CONFIG[:backgroundrb][:port]}.log" |
|---|
| 31 |
|
|---|
| 32 |
case ARGV[0] |
|---|
| 33 |
when 'start' |
|---|
| 34 |
if fork |
|---|
| 35 |
sleep(5) |
|---|
| 36 |
exit |
|---|
| 37 |
else |
|---|
| 38 |
op = File.open(pid_file, "w") |
|---|
| 39 |
op.write(Process.pid().to_s) |
|---|
| 40 |
op.close |
|---|
| 41 |
if BDRB_CONFIG[:backgroundrb][:log].nil? or BDRB_CONFIG[:backgroundrb][:log] != 'foreground' |
|---|
| 42 |
log_file = File.open(SERVER_LOGGER,"w+") |
|---|
| 43 |
[STDIN, STDOUT, STDERR].each {|desc| desc.reopen(log_file)} |
|---|
| 44 |
end |
|---|
| 45 |
|
|---|
| 46 |
BackgrounDRb::MasterProxy.new() |
|---|
| 47 |
end |
|---|
| 48 |
when 'stop' |
|---|
| 49 |
def kill_process arg_pid_file |
|---|
| 50 |
pid = nil |
|---|
| 51 |
File.open(arg_pid_file, "r") { |pid_handle| pid = pid_handle.gets.strip.chomp.to_i } |
|---|
| 52 |
begin |
|---|
| 53 |
pgid = Process.getpgid(pid) |
|---|
| 54 |
Process.kill('TERM', pid) |
|---|
| 55 |
Process.kill('-TERM', pgid) |
|---|
| 56 |
Process.kill('KILL', pid) |
|---|
| 57 |
rescue Errno::ESRCH => e |
|---|
| 58 |
puts "Deleting pid file" |
|---|
| 59 |
rescue |
|---|
| 60 |
puts $! |
|---|
| 61 |
ensure |
|---|
| 62 |
File.delete(arg_pid_file) if File.exists?(arg_pid_file) |
|---|
| 63 |
end |
|---|
| 64 |
end |
|---|
| 65 |
pid_files = Dir["#{RAILS_HOME}/tmp/pids/backgroundrb_*.pid"] |
|---|
| 66 |
pid_files.each { |x| kill_process(x) } |
|---|
| 67 |
else |
|---|
| 68 |
BackgrounDRb::MasterProxy.new() |
|---|
| 69 |
end |
|---|
| 70 |
|
|---|