Class: Waxx::Process::Runner
- Inherits:
-
Object
- Object
- Waxx::Process::Runner
- Defined in:
- waxx/process.rb
Constant Summary collapse
- MAX_START_TRIES =
5
Instance Attribute Summary collapse
-
#description ⇒ Object
Returns the value of attribute description.
-
#name ⇒ Object
Returns the value of attribute name.
-
#options ⇒ Object
Returns the value of attribute options.
-
#verify_options_hook ⇒ Object
Returns the value of attribute verify_options_hook.
Class Method Summary collapse
Instance Method Summary collapse
-
#daemon_running? ⇒ Boolean
Returns running for the daemonized process self.daemon_running?.
-
#daemon_stopped? ⇒ Boolean
Returns true if process is not running.
- #daemonize ⇒ Object
-
#execute(opts = {}, &block) ⇒ Object
Executes the runner based on options `@runner.execute` `@runner.execute { … }`.
-
#initialize(name, defaults = {}, &block) ⇒ Runner
constructor
A new instance of Runner.
- #interrupt ⇒ Object
- #restart ⇒ Object
- #start ⇒ Object
-
#stop(kill_arg = nil) ⇒ Object
Stops a daemonized process.
-
#with_options(&block) ⇒ Object
Accepts options for the process `@runner.with_options { |opts| opts.on(…) }`.
Constructor Details
#initialize(name, defaults = {}, &block) ⇒ Runner
Returns a new instance of Runner
19 20 21 22 23 24 25 26 27 28 |
# File 'waxx/process.rb', line 19 def initialize(name, defaults={}, &block) @name = name @startup_command = block @options = { :host => '0.0.0.0', :pid_path => "/var/run/#{@name}.pid", :log_path => false, :debug => true }.merge(defaults) end |
Instance Attribute Details
#description ⇒ Object
Returns the value of attribute description
11 12 13 |
# File 'waxx/process.rb', line 11 def description @description end |
#name ⇒ Object
Returns the value of attribute name
11 12 13 |
# File 'waxx/process.rb', line 11 def name @name end |
#options ⇒ Object
Returns the value of attribute options
11 12 13 |
# File 'waxx/process.rb', line 11 def @options end |
#verify_options_hook ⇒ Object
Returns the value of attribute verify_options_hook
11 12 13 |
# File 'waxx/process.rb', line 11 def @verify_options_hook end |
Class Method Details
.run(*args, &block) ⇒ Object
14 15 16 |
# File 'waxx/process.rb', line 14 def run(*args, &block) self.new(*args, &block) end |
Instance Method Details
#daemon_running? ⇒ Boolean
Returns running for the daemonized process self.daemon_running?
148 149 150 151 152 153 154 |
# File 'waxx/process.rb', line 148 def daemon_running? return false unless File.exist?([:pid_path]) Process.kill 0, File.read([:pid_path]).to_i true rescue Errno::ESRCH false end |
#daemon_stopped? ⇒ Boolean
Returns true if process is not running
142 143 144 |
# File 'waxx/process.rb', line 142 def daemon_stopped? ! self.daemon_running? end |
#daemonize ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'waxx/process.rb', line 68 def daemonize return log("Process is already started") if self.daemon_running? # daemon already started if ![:log_path] [:log_path] = "/var/log/#{@name}.log" end # Start process pid = fork do exit if fork Process.setsid exit if fork store_pid(Process.pid) File.umask 0000 redirect_output! start end Process.waitpid pid # Ensure process is running if until_true(MAX_START_TRIES) { self.daemon_running? } log "Daemon has started successfully" true else # Failed to start log "Daemonized process couldn't be started" false end end |
#execute(opts = {}, &block) ⇒ Object
Executes the runner based on options `@runner.execute` `@runner.execute { … }`
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'waxx/process.rb', line 39 def execute(opts={}, &block) self..merge!(opts) @verify_options_hook.call(self.) if @verify_options_hook if .include?(:kill) self.stop else # create process self.stop if .include?(:restart) # If a username, uid, groupname, or gid is passed, # drop privileges accordingly. if [:group] gid = [:group].is_a?(Integer) ? [:group] : Etc.getgrnam([:group]).gid Process::GID.change_privilege(gid) end if [:user] uid = [:user].is_a?(Integer) ? [:user] : Etc.getpwnam([:user]).uid Process::UID.change_privilege(uid) end @startup_command = block if block_given? [:daemonize] ? daemonize : start end end |
#interrupt ⇒ Object
132 133 134 135 136 137 138 139 |
# File 'waxx/process.rb', line 132 def interrupt if [:debug] raise Interrupt sleep 1 else log "Interrupt received; stopping #{@name}" end end |
#restart ⇒ Object
127 128 129 130 |
# File 'waxx/process.rb', line 127 def restart self.stop self.start end |
#start ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'waxx/process.rb', line 96 def start log "Starting #{@name} service..." if log_path = [:log_path] && [:daemonize].nil? redirect_output! end trap("INT") { interrupt exit } trap("TERM"){ log "Trying to stop #{@name}..." exit } @startup_command.call(self.) if @startup_command end |
#stop(kill_arg = nil) ⇒ Object
Stops a daemonized process
117 118 119 120 121 122 123 124 125 |
# File 'waxx/process.rb', line 117 def stop(kill_arg=nil) if self.daemon_running? kill_pid(kill_arg || [:kill]) until_true(MAX_START_TRIES) { self.daemon_stopped? } else # not running log "No #{@name} processes are running" false end end |
#with_options(&block) ⇒ Object
Accepts options for the process `@runner.with_options { |opts| opts.on(…) }`
32 33 34 |
# File 'waxx/process.rb', line 32 def (&block) @with_options = block end |