class Legion::Process
Attributes
options[R]
quit[R]
service[R]
Public Class Methods
new(options)
click to toggle source
# File lib/legion/process.rb, line 11 def initialize(options) @options = options options[:logfile] = File.expand_path(logfile) if logfile? options[:pidfile] = File.expand_path(pidfile) if pidfile? end
run!(options)
click to toggle source
# File lib/legion/process.rb, line 5 def self.run!(options) Legion::Process.new(options).run! end
Public Instance Methods
check_pid()
click to toggle source
# File lib/legion/process.rb, line 85 def check_pid if pidfile? case pid_status(pidfile) when :running, :not_owned exit(1) when :dead File.delete(pidfile) end end false end
daemonize()
click to toggle source
daemonize?()
click to toggle source
# File lib/legion/process.rb, line 17 def daemonize? options[:daemonize] end
info(msg)
click to toggle source
# File lib/legion/process.rb, line 37 def info(msg) puts "[#{::Process.pid}] [#{Time.now}] #{msg}" end
logfile()
click to toggle source
# File lib/legion/process.rb, line 21 def logfile options[:logfile] end
logfile?()
click to toggle source
# File lib/legion/process.rb, line 29 def logfile? !logfile.nil? end
pid_status(pidfile)
click to toggle source
# File lib/legion/process.rb, line 97 def pid_status(pidfile) return :exited unless File.exist?(pidfile) pid = ::File.read(pidfile).to_i return :dead if pid.zero? ::Process.kill(0, pid) :running rescue Errno::ESRCH :dead rescue Errno::EPERM :not_owned end
pidfile()
click to toggle source
# File lib/legion/process.rb, line 25 def pidfile options[:pidfile] end
pidfile?()
click to toggle source
# File lib/legion/process.rb, line 33 def pidfile? !pidfile.nil? end
run!()
click to toggle source
# File lib/legion/process.rb, line 41 def run! start_time = Time.now @options[:time_limit] = @options[:time_limit].to_i if @options.key? :time_limit @quit = false check_pid daemonize if daemonize? write_pid trap_signals until quit sleep(1) @quit = true if @options.key?(:time_limit) && Time.now - start_time > @options[:time_limit] end Legion::Logging.info('Legion is shutting down!') Legion.shutdown Legion::Logging.info('Legion has shutdown. Goodbye!') exit end
trap_signals()
click to toggle source
# File lib/legion/process.rb, line 111 def trap_signals trap('SIGTERM') do info 'sigterm' end trap('SIGHUP') do info 'sithup' end trap('SIGINT') do @quit = true end end
write_pid()
click to toggle source
# File lib/legion/process.rb, line 72 def write_pid if pidfile? begin File.open(pidfile, ::File::CREAT | ::File::EXCL | ::File::WRONLY) { |f| f.write(::Process.pid.to_s) } at_exit { File.delete(pidfile) if File.exist?(pidfile) } rescue Errno::EEXIST check_pid retry end end false end