Class: Dante::Runner
- Inherits:
-
Object
- Object
- Dante::Runner
- Defined in:
- lib/dante/runner.rb
Defined Under Namespace
Classes: Abort
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
-
#execute(&block) ⇒ Object
Executes the runner based on options.
-
#initialize(name, defaults = {}, &block) ⇒ Runner
constructor
A new instance of Runner.
- #parse_options ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
-
#with_options(&block) ⇒ Object
Accepts options for the process.
Constructor Details
#initialize(name, defaults = {}, &block) ⇒ Runner
Returns a new instance of Runner.
29 30 31 32 33 34 35 36 |
# File 'lib/dante/runner.rb', line 29 def initialize(name, defaults={}, &block) @name = name @startup_command = block @options = { :host => '0.0.0.0', :pid_path => "/var/run/#{@name}.pid" }.merge(defaults) end |
Instance Attribute Details
#description ⇒ Object
Returns the value of attribute description.
21 22 23 |
# File 'lib/dante/runner.rb', line 21 def description @description end |
#name ⇒ Object
Returns the value of attribute name.
21 22 23 |
# File 'lib/dante/runner.rb', line 21 def name @name end |
#options ⇒ Object
Returns the value of attribute options.
21 22 23 |
# File 'lib/dante/runner.rb', line 21 def @options end |
Class Method Details
.run(*args, &block) ⇒ Object
24 25 26 |
# File 'lib/dante/runner.rb', line 24 def run(*args, &block) self.new(*args, &block) end |
Instance Method Details
#execute(&block) ⇒ Object
Executes the runner based on options
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/dante/runner.rb', line 47 def execute(&block) if .include?(:kill) kill_pid([:kill] || '*') else # create process Process.euid = [:user] if [:user] Process.egid = [:group] if [:group] @startup_command = block if block_given? [:daemonize] ? daemonize : start end end |
#parse_options ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/dante/runner.rb', line 80 def headline = [@name, @description].compact.join(" - ") OptionParser.new do |opts| opts.summary_width = 25 opts. = [headline, "\n\n", "Usage: #{@name} [-p port] [-P file] [-d] [-k]\n", " #{@name} --help\n"].compact.join("") opts.separator "" opts.on("-p", "--port PORT", Integer, "Specify port", "(default: #{[:port]})") do |v| [:port] = v end opts.on("-P", "--pid FILE", String, "save PID in FILE when using -d option.", "(default: #{[:pid_path]})") do |v| [:pid_path] = File.(v) end opts.on("-d", "--daemon", "Daemonize mode") do |v| [:daemonize] = v end opts.on("-k", "--kill [PORT]", String, "Kill specified running daemons - leave blank to kill all.") do |v| [:kill] = v end opts.on("-u", "--user USER", String, "User to run as") do |user| [:user] = user end opts.on("-G", "--group GROUP", String, "Group to run as") do |group| [:group] = group end opts.on_tail("-?", "--help", "Display this usage information.") do puts "#{opts}\n" exit end # Load options specified through 'with_options' instance_exec(opts, &@with_options) if @with_options end.parse! end |
#start ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/dante/runner.rb', line 60 def start puts "Starting #{@name} service..." trap("INT") { stop exit } trap("TERM"){ stop exit } @startup_command.call(self.) if @startup_command 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 |