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) ⇒ Runner

Create a new Runner

Parameters:

  • argv (Array)

    command line arguments

  • service (Object)

    service to execute



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/raad/runner.rb', line 16

def initialize(argv, service)
  @argv = argv.dup # lets keep a copy for jruby double-launch
  create_options_parser(service).parse!(argv)

  # start/stop 
  @options[:command] = argv[0].to_s.downcase
  unless ['start', 'stop', 'post_fork'].include?(options[:command])
    puts(">> start|stop command is required")
    exit!(false)
  end

  @service = service
  @service_name = nil
  @logger_options = nil
  @pid_file = nil

  @stop_signaled = false
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



10
11
12
# File 'lib/raad/runner.rb', line 10

def options
  @options
end

#pid_fileObject

Returns the value of attribute pid_file.



10
11
12
# File 'lib/raad/runner.rb', line 10

def pid_file
  @pid_file
end

#serviceObject

Returns the value of attribute service.



10
11
12
# File 'lib/raad/runner.rb', line 10

def service
  @service
end

Instance Method Details

#runObject



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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/raad/runner.rb', line 35

def run
  # first load config if present
  Configuration.load(options[:config] || File.expand_path("./config/#{default_service_name}.rb"))

  # settings which depends on configuration
  @service_name = options[:name] || Configuration.daemon_name || default_service_name

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

  # check for stop command, @pid_file must be set
  if 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, KILL -9 will be sent.
    success = send_signal('TERM', @stop_timeout + 2) 
    exit(success)
  end

  # setup logging
  Logger.setup(@logger_options)
  Logger.level = Configuration.log_level if Configuration.log_level

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

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