Class: Raad::Runner

Inherits:
Object
  • Object
show all
Includes:
Daemonizable
Defined in:
lib/raad/runner.rb

Constant Summary collapse

SECOND =
1
STOP_TIMEOUT =
60 * SECOND

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Daemonizable

#daemonize, #force_kill_and_remove_pid_file, #pid, #post_fork_setup, #read_pid_file, #remove_pid_file, #remove_stale_pid_file, #send_signal, #write_pid_file

Constructor Details

#initialize(argv, service_class) ⇒ Runner

Create a new Runner

Parameters:

  • argv (Array)

    command line arguments

  • service_class (Class)

    service class



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
# File 'lib/raad/runner.rb', line 18

def initialize(argv, service_class)
  @argv = argv.dup # lets keep a copy for jruby double-launch
  @service_class = service_class
  @parsed_options = nil

  @stop_lock = Mutex.new
  @stop_signaled = false

  # parse command line options and set @parsed_options
  options_parser = service_class.respond_to?(:options_parser) ? service_class.options_parser(create_options_parser, Raad.custom_options) : create_options_parser
  begin
    options_parser.parse!(argv)
  rescue OptionParser::InvalidOption => e
    puts(">> #{e.message}")
    exit!(false)
  end

  # grab what's left after options, which should be the start/stop command
  @parsed_options[:command] = argv[0].to_s.downcase
  unless ['start', 'stop', 'post_fork'].include?(@parsed_options[:command])
    puts(">> start|stop command is required")
    exit!(false)
  end

  # load config if present
  Configuration.load(@parsed_options[:config] || File.expand_path("./config/#{default_service_name(service_class)}.rb"))

  # default service name 
  @service_name = @parsed_options[:name] || Configuration.daemon_name || default_service_name(service_class)

  # @pid_file is required to become Daemonizable
  @pid_file = @parsed_options.delete(:pid_file) || "./#{@service_name}.pid"

  # default stop timeout
  @stop_timeout = (@parsed_options.delete(:stop_timeout) || Configuration.stop_timeout || STOP_TIMEOUT).to_i

  # logger options
  @parsed_options[:log_file] = (@parsed_options[:daemonize] ? File.expand_path("#{@service_name}.log") : nil) unless @parsed_options[:log_file]
  @parsed_options[:log_stdout] = !@parsed_options[:daemonize] unless @parsed_options[:log_stdout]
  @logger_options = {
    :file => @parsed_options.delete(:log_file) || Configuration.log_file,
    :stdout => @parsed_options.delete(:log_stdout) || Configuration.log_stdout,
    :verbose => @parsed_options.delete(:verbose) || Configuration.verbose,
    :pattern => @parsed_options.delete(:log_pattern) || Configuration.log_pattern,
  }
end

Instance Attribute Details

#pid_fileObject

Returns the value of attribute pid_file.



12
13
14
# File 'lib/raad/runner.rb', line 12

def pid_file
  @pid_file
end

Instance Method Details

#runObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/raad/runner.rb', line 65

def run
  # check for stop command, @pid_file must be set
  if @parsed_options[:command] == 'stop'
    puts(">> Raad service wrapper v#{VERSION} stopping")
    # first send the TERM signal which will invoke the daemon wait_or_will method which will timeout after @stop_timeout
    # if still not stopped afer @stop_timeout + 2 seconds, KILL -9 will be sent.
    success = send_signal('TERM', @stop_timeout + (2 * SECOND)) 
    exit(success)
  end

  Dir.chdir(File.expand_path(File.dirname("./"))) unless Raad.test?

  if @parsed_options[:command] == 'post_fork'
    # we've been spawned and re executed, finish setup
    post_fork_setup(@service_name, @parsed_options[:redirect])
    start_service
  else
    puts(">> Raad service wrapper v#{VERSION} starting")
    @parsed_options[:daemonize] ? daemonize(@argv, @service_name, @parsed_options[:redirect]) {start_service} : start_service
  end
end