Class: RaadTotem::Runner

Inherits:
Object
  • Object
show all
Includes:
Daemonizable
Defined in:
lib/raad_totem/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



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
# File 'lib/raad_totem/runner.rb', line 19

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, RaadTotem.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

  Totem.component = default_service_name(service_class)

  # @pid_file is required to become Daemonizable
  @pid_file = @parsed_options.delete(:pid_file) || default_pid_path
  FileUtils.mkdir_p(File.dirname(@pid_file))

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

Instance Attribute Details

#pid_fileObject

Returns the value of attribute pid_file.



13
14
15
# File 'lib/raad_totem/runner.rb', line 13

def pid_file
  @pid_file
end

Instance Method Details

#runObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/raad_totem/runner.rb', line 53

def run
  # check for stop command, @pid_file must be set
  if @parsed_options[:command] == 'stop'
    puts(">> RaadTotem 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("./")))

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