Class: Dante::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/dante/runner.rb

Defined Under Namespace

Classes: Abort

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#descriptionObject

Returns the value of attribute description.



21
22
23
# File 'lib/dante/runner.rb', line 21

def description
  @description
end

#nameObject

Returns the value of attribute name.



21
22
23
# File 'lib/dante/runner.rb', line 21

def name
  @name
end

#optionsObject

Returns the value of attribute options.



21
22
23
# File 'lib/dante/runner.rb', line 21

def options
  @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)
  parse_options

  if options.include?(:kill)
    kill_pid(options[:kill] || '*')
  else # create process
    Process.euid = options[:user] if options[:user]
    Process.egid = options[:group] if options[:group]
    @startup_command = block if block_given?
    options[:daemonize] ? daemonize : start
  end
end

#parse_optionsObject



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 parse_options
  headline = [@name, @description].compact.join(" - ")
  OptionParser.new do |opts|
    opts.summary_width = 25
    opts.banner = [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: #{options[:port]})") do |v|
      options[:port] = v
    end

    opts.on("-P", "--pid FILE", String, "save PID in FILE when using -d option.", "(default: #{options[:pid_path]})") do |v|
      options[:pid_path] = File.expand_path(v)
    end

    opts.on("-d", "--daemon", "Daemonize mode") do |v|
      options[:daemonize] = v
    end

    opts.on("-k", "--kill [PORT]", String, "Kill specified running daemons - leave blank to kill all.") do |v|
      options[:kill] = v
    end

    opts.on("-u", "--user USER", String, "User to run as") do |user|
      options[:user] = user
    end

    opts.on("-G", "--group GROUP", String, "Group to run as") do |group|
      options[: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!
  options
end

#startObject



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.options) if @startup_command
end

#stopObject

Raises:



75
76
77
78
# File 'lib/dante/runner.rb', line 75

def stop
  raise Abort
  sleep(1)
end

#with_options(&block) ⇒ Object

Accepts options for the process



40
41
42
# File 'lib/dante/runner.rb', line 40

def with_options(&block)
  @with_options = block
end