Class: Legion::Process
- Inherits:
-
Object
- Object
- Legion::Process
- Defined in:
- lib/legion/process.rb
Class Attribute Summary collapse
-
.quit_flag ⇒ Object
Returns the value of attribute quit_flag.
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#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
- #quit ⇒ Object
- #retrap_after_puma ⇒ Object
- #run! ⇒ Object
- #trap_signals ⇒ Object
- #write_pid ⇒ Object
Constructor Details
#initialize(options) ⇒ Process
Returns a new instance of Process.
18 19 20 21 22 |
# File 'lib/legion/process.rb', line 18 def initialize() @options = [:logfile] = File.(logfile) if logfile? [:pidfile] = File.(pidfile) if pidfile? end |
Class Attribute Details
.quit_flag ⇒ Object
Returns the value of attribute quit_flag.
9 10 11 |
# File 'lib/legion/process.rb', line 9 def quit_flag @quit_flag end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
16 17 18 |
# File 'lib/legion/process.rb', line 16 def @options end |
#service ⇒ Object (readonly)
Returns the value of attribute service.
16 17 18 |
# File 'lib/legion/process.rb', line 16 def service @service end |
Class Method Details
Instance Method Details
#check_pid ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/legion/process.rb', line 100 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
79 80 81 82 83 84 |
# File 'lib/legion/process.rb', line 79 def daemonize exit if fork ::Process.setsid exit if fork Dir.chdir '/' end |
#daemonize? ⇒ Boolean
28 29 30 |
# File 'lib/legion/process.rb', line 28 def daemonize? [:daemonize] end |
#info(msg) ⇒ Object
48 49 50 |
# File 'lib/legion/process.rb', line 48 def info(msg) puts "[#{::Process.pid}] [#{Time.now}] #{msg}" end |
#logfile ⇒ Object
32 33 34 |
# File 'lib/legion/process.rb', line 32 def logfile [:logfile] end |
#logfile? ⇒ Boolean
40 41 42 |
# File 'lib/legion/process.rb', line 40 def logfile? !logfile.nil? end |
#pid_status(pidfile) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/legion/process.rb', line 112 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 => e Legion::Logging.debug "Process#pid_status: pid=#{pid} is dead: #{e.}" if defined?(Legion::Logging) :dead rescue Errno::EPERM => e Legion::Logging.debug "Process#pid_status: pid=#{pid} not owned: #{e.}" if defined?(Legion::Logging) :not_owned end |
#pidfile ⇒ Object
36 37 38 |
# File 'lib/legion/process.rb', line 36 def pidfile [:pidfile] end |
#pidfile? ⇒ Boolean
44 45 46 |
# File 'lib/legion/process.rb', line 44 def pidfile? !pidfile.nil? end |
#quit ⇒ Object
24 25 26 |
# File 'lib/legion/process.rb', line 24 def quit @quit.is_a?(Concurrent::AtomicBoolean) ? @quit.true? : !!@quit end |
#retrap_after_puma ⇒ Object
146 147 148 149 150 151 152 153 154 |
# File 'lib/legion/process.rb', line 146 def retrap_after_puma @retrap_thread = Thread.new do 15.times do sleep 1 trap('SIGINT') { @quit.make_true } trap('SIGTERM') { @quit.make_true } end end end |
#run! ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/legion/process.rb', line 52 def run! start_time = Time.now @options[:time_limit] = @options[:time_limit].to_i if @options.key? :time_limit @quit = Concurrent::AtomicBoolean.new(false) self.class.quit_flag = @quit check_pid daemonize if daemonize? write_pid trap_signals retrap_after_puma until quit sleep(1) @quit.make_true if @options.key?(:time_limit) && Time.now - start_time > @options[:time_limit] end @retrap_thread&.kill Legion::Logging.info('Legion is shutting down!') Legion.shutdown Legion::Logging.info('Legion has shutdown. Goodbye!') exit end |
#trap_signals ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/legion/process.rb', line 128 def trap_signals trap('SIGTERM') do Legion::Logging.info '[Process] received SIGTERM, shutting down' if defined?(Legion::Logging) @quit.make_true end trap('SIGHUP') do Legion::Logging.info '[Process] received SIGHUP, triggering reload' if defined?(Legion::Logging) info 'sighup: triggering reload' Thread.new { Legion.reload } end trap('SIGINT') do Legion::Logging.info '[Process] received SIGINT, shutting down' if defined?(Legion::Logging) @quit.make_true end end |
#write_pid ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/legion/process.rb', line 86 def write_pid if pidfile? begin File.open(pidfile, ::File::CREAT | ::File::EXCL | ::File::WRONLY) { |f| f.write(::Process.pid.to_s) } Legion::Logging.info "[Process] PID #{::Process.pid} written to #{pidfile}" if defined?(Legion::Logging) at_exit { FileUtils.rm_f(pidfile) } rescue Errno::EEXIST check_pid retry end end false end |