lib/asir/main.rb in asir-1.2.3 vs lib/asir/main.rb in asir-1.2.5

- old
+ new

@@ -7,11 +7,11 @@ attr_accessor :env, :args, :exit_code # Delegate getter/setters to @env. [ :verb, :adjective, :object, :identifier, :config_rb, :verbose, - :options, + :options, :fork_process, :log_dir, :log_file, :pid_dir, :pid_file, ]. map{|g| [ g, :"#{g}=" ]}. flatten.each do | m | @@ -111,20 +111,22 @@ when /^start_([^_]+)_worker!$/ _start_worker! when /^status_([^_]+)_([^_]+)!$/ pid = server_pid puts "#{log_str} pid #{pid}" - system("ps -fw -p #{pid}") + system("ps -fw -p #{pid}") if pid when /^log_([^_]+)_([^_]+)!$/ puts log_file when /^taillog_([^_]+)_([^_]+)!$/ exec "tail -f #{log_file.inspect}" when /^pid_([^_]+)_([^_]+)!$/ - pid = _alive? + pid = server_pid + alive = !! _alive? puts "#{pid_file} #{pid || :NA} #{alive}" when /^alive_([^_]+)_([^_]+)!$/ - pid = _alive? + pid = server_pid + alive = !! _alive? puts "#{pid_file} #{pid || :NA} #{alive}" if @verbose self.exit_code += 1 unless alive when /^stop_([^_]+)_([^_]+)!$/ kill_server! else @@ -145,10 +147,11 @@ OPTIONS: config_rb=file.rb ($ASIR_LOG_DIR) pid_dir=dir/ ($ASIR_PID_DIR) log_dir=dir/ ($ASIR_LOG_DIR) verbose=[0-9] + fork_process=[01] ($ASIR_FORK_PROCESS) VERBS: start stop restart @@ -199,10 +202,11 @@ end log "start_conduit! #{type}" config!(:environment) self.transport = config!(:transport) fork_server! do + ENV['ASIR_CONDUIT'] = '1' transport.start_conduit! :fork => false end end def _start_worker! type = adjective @@ -226,41 +230,64 @@ _run_workers! end end def fork_server! cmd = nil, &blk - pid = Process.fork do + fork_process! do run_server! cmd, &blk end - log "forked pid #{pid}" - Process.detach(pid) # Forks a Thread? We are gonna exit anyway. - File.open(pid_file, "w+") { | o | o.puts pid } - File.chmod(0666, pid_file) rescue nil - # Wait and check if process still exists. - sleep 3 - unless process_running? pid - raise "Server process #{pid} died to soon?" + self + end + + def fork_process! &blk + if fork_process + pid = Process.fork &blk + log "forked pid #{pid}" + Process.detach(pid) # Forks a Thread? We are gonna exit anyway. + write_pid_file! pid + + # Wait and check if process still exists. + sleep 3 + unless process_running? pid + raise "Server process #{pid} died to soon?" + end + else + pid = $$ + log "running pid #{pid}" + write_pid_file! pid + yield end + pid + end - self + def write_pid_file! pid + File.open(pid_file, "w+") { | o | o.puts pid } + File.chmod(0666, pid_file) rescue nil end def run_server! cmd = nil + nf = File.open("/dev/null") + nf.sync = true + STDIN.reopen(nf) + STDIN.sync = true + $stdin.reopen(nf) if $stdin.object_id != STDIN.object_id + $stdin.sync = true + lf = File.open(log_file, "a+") lf.sync = true File.chmod(0666, log_file) rescue nil - $stdin.close rescue nil - STDIN.close rescue nil + STDOUT.reopen(lf) STDOUT.sync = true $stdout.reopen(lf) if $stdout.object_id != STDOUT.object_id $stdout.sync = true STDERR.reopen(lf) STDERR.sync = true $stderr.reopen(lf) if $stderr.object_id != STDERR.object_id $stderr.sync = true + # Process.daemon rescue nil # Ruby 1.9.x only. lf.puts "#{log_str} starting pid #{$$}" begin if cmd exec(cmd) @@ -280,28 +307,31 @@ end def kill_server! log "#{log_str} kill" pid = server_pid - stop_pid! pid + case stop_pid! pid + when :not_running + File.unlink(pid_file) rescue nil + end rescue ::Exception => exc log "#{log_str} ERROR\n#{exc.inspect}\n #{exc.backtrace * "\n "}", :stderr raise end def log msg, to_stderr = false if to_stderr - $stderr.puts "#{log_str_no_time} #{msg}" + $stderr.puts "#{log_str_no_time} #{msg}" rescue nil end File.open(log_file, "a+") do | log | log.puts "#{log_str} #{msg}" end end def server_pid - pid = File.read(pid_file).chomp! - pid.to_i + File.exist?(pid_file) && + File.read(pid_file).chomp!.to_i end def _create_transport default_class config!(:environment) case transport = config!(:transport) @@ -316,11 +346,10 @@ (@worker_pids ||= { })[adjective] ||= { } end def _run_workers! $0 = "#{progname} #{adjective} #{object} #{identifier}" - worker_id = 0 transport.prepare_server! worker_processes = transport[:worker_processes] || 1 (worker_processes - 1).times do wid = worker_id += 1 @@ -330,17 +359,18 @@ Process.setgprp(pid, 0) rescue nil worker_pids[wid] = pid log "forked #{wid} pid #{pid}" end - _run_transport_server! + _run_transport_server! 0 ensure log "worker 0 stopped" _stop_workers! end def _run_transport_server! wid = 0 + ENV['ASIR_WORKER'] = "#{identifier}:#{wid}" log "running transport worker #{transport.class} #{wid}" config!(:start) $0 += " #{wid} #{transport.uri rescue nil}" old_arg0 = $0.dup after_receive_message = transport.after_receive_message || lambda { | transport, state | nil } @@ -378,12 +408,14 @@ log "KILL pid #{pid}", :stderr Process.kill('KILL', pid) rescue nil end if process_running? pid log "cant-stop pid #{pid}", :stderr + :cant_stop end else log "not-running? pid #{pid}", :stderr + :not_running end end def process_running? pid case pid