Class: Dante::Runner
- Inherits:
-
Object
- Object
- Dante::Runner
- Defined in:
- lib/dante/runner.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.
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.
-
#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.
Constructor Details
#initialize(name, defaults = {}, &block) ⇒ Runner
Returns a new instance of Runner.
28 29 30 31 32 33 34 35 36 |
# File 'lib/dante/runner.rb', line 28 def initialize(name, defaults={}, &block) @name = name @startup_command = block @options = { :host => '0.0.0.0', :pid_path => "/var/run/#{@name}.pid", :debug => true }.merge(defaults) end |
Instance Attribute Details
#description ⇒ Object
Returns the value of attribute description.
20 21 22 |
# File 'lib/dante/runner.rb', line 20 def description @description end |
#name ⇒ Object
Returns the value of attribute name.
20 21 22 |
# File 'lib/dante/runner.rb', line 20 def name @name end |
#options ⇒ Object
Returns the value of attribute options.
20 21 22 |
# File 'lib/dante/runner.rb', line 20 def @options end |
Class Method Details
.run(*args, &block) ⇒ Object
23 24 25 |
# File 'lib/dante/runner.rb', line 23 def run(*args, &block) self.new(*args, &block) end |
Instance Method Details
#daemon_running? ⇒ Boolean
Returns running for the daemonized process self.daemon_running?
130 131 132 133 134 135 136 |
# File 'lib/dante/runner.rb', line 130 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
124 125 126 |
# File 'lib/dante/runner.rb', line 124 def daemon_stopped? ! self.daemon_running? end |
#daemonize ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/dante/runner.rb', line 62 def daemonize return log("Process is already started") if self.daemon_running? # daemon already started # Start process pid = fork do exit if fork Process.setsid exit if fork store_pid(Process.pid) File.umask 0000 STDIN.reopen "/dev/null" STDOUT.reopen "/dev/null", "a" STDERR.reopen STDOUT start end # 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
47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/dante/runner.rb', line 47 def execute(opts={}, &block) self..merge!(opts) if .include?(:kill) self.stop else # create process self.stop if .include?(:restart) Process.euid = [:user] if [:user] Process.egid = [:group] if [:group] @startup_command = block if block_given? [:daemonize] ? daemonize : start end end |
#interrupt ⇒ Object
118 119 120 121 |
# File 'lib/dante/runner.rb', line 118 def interrupt raise Interrupt sleep(1) end |
#restart ⇒ Object
113 114 115 116 |
# File 'lib/dante/runner.rb', line 113 def restart self.stop self.start end |
#start ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/dante/runner.rb', line 87 def start log "Starting #{@name} service..." trap("INT") { interrupt exit } trap("TERM"){ interrupt exit } @startup_command.call(self.) if @startup_command end |
#stop(kill_arg = nil) ⇒ Object
Stops a daemonized process
103 104 105 106 107 108 109 110 111 |
# File 'lib/dante/runner.rb', line 103 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
40 41 42 |
# File 'lib/dante/runner.rb', line 40 def (&block) @with_options = block end |