Class: Legion::Process
- Inherits:
-
Object
- Object
- Legion::Process
- Defined in:
- lib/legion/process.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#quit ⇒ Object
readonly
Returns the value of attribute quit.
-
#service ⇒ Object
readonly
Returns the value of attribute service.
Class Method Summary collapse
Instance Method Summary collapse
- #check_pid ⇒ Object
-
#daemonize ⇒ Object
DAEMONIZING, PID MANAGEMENT, and OUTPUT REDIRECTION ==========================================================================.
- #daemonize? ⇒ Boolean
- #info(msg) ⇒ Object
-
#initialize(options) ⇒ Process
constructor
A new instance of Process.
- #logfile ⇒ Object
- #logfile? ⇒ Boolean
- #pid_status(pidfile) ⇒ Object
- #pidfile ⇒ Object
- #pidfile? ⇒ Boolean
- #run! ⇒ Object
- #trap_signals ⇒ Object
- #write_pid ⇒ Object
Constructor Details
#initialize(options) ⇒ Process
Returns a new instance of Process.
11 12 13 14 15 |
# File 'lib/legion/process.rb', line 11 def initialize() @options = [:logfile] = File.(logfile) if logfile? [:pidfile] = File.(pidfile) if pidfile? end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
9 10 11 |
# File 'lib/legion/process.rb', line 9 def @options end |
#quit ⇒ Object (readonly)
Returns the value of attribute quit.
9 10 11 |
# File 'lib/legion/process.rb', line 9 def quit @quit end |
#service ⇒ Object (readonly)
Returns the value of attribute service.
9 10 11 |
# File 'lib/legion/process.rb', line 9 def service @service end |
Class Method Details
Instance Method Details
#check_pid ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 |
# 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 ⇒ Object
DAEMONIZING, PID MANAGEMENT, and OUTPUT REDIRECTION
65 66 67 68 69 70 |
# File 'lib/legion/process.rb', line 65 def daemonize exit if fork ::Process.setsid exit if fork Dir.chdir '/' end |
#daemonize? ⇒ Boolean
17 18 19 |
# File 'lib/legion/process.rb', line 17 def daemonize? [:daemonize] end |
#info(msg) ⇒ Object
37 38 39 |
# File 'lib/legion/process.rb', line 37 def info(msg) puts "[#{::Process.pid}] [#{Time.now}] #{msg}" end |
#logfile ⇒ Object
21 22 23 |
# File 'lib/legion/process.rb', line 21 def logfile [:logfile] end |
#logfile? ⇒ Boolean
29 30 31 |
# File 'lib/legion/process.rb', line 29 def logfile? !logfile.nil? end |
#pid_status(pidfile) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 |
# 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 ⇒ Object
25 26 27 |
# File 'lib/legion/process.rb', line 25 def pidfile [:pidfile] end |
#pidfile? ⇒ Boolean
33 34 35 |
# File 'lib/legion/process.rb', line 33 def pidfile? !pidfile.nil? end |
#run! ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# 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 ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 |
# 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 ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 |
# 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 |