Class: Officer::Runner

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

Instance Method Summary collapse

Instance Method Details

#hack_argvObject

HACK: Both the Choice and Daemons gems will parse ARGV so I modify ARGV for Choice and then restore it for Daemons.



14
15
16
17
18
# File 'lib/officer/runner.rb', line 14

def hack_argv
  if ARGV.include? '--'
    @saved_args = ARGV.slice! 0..ARGV.index('--')
  end
end

#runObject



4
5
6
7
8
9
10
# File 'lib/officer/runner.rb', line 4

def run
  hack_argv
  set_choices
  unhack_argv
  set_daemon_options
  run_daemon
end

#run_daemonObject



100
101
102
103
104
# File 'lib/officer/runner.rb', line 100

def run_daemon
  Daemons.run_proc('officer', @daemon_options) do
    Officer::Server.new(@choices).run
  end
end

#set_choicesObject



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

def set_choices

  Choice.options do
    option :host do
      short '-h'
      long '--host=HOST'
      desc 'The hostname or IP to bind to (default: 0.0.0.0)'
    end

    option :socket_type do
      short '-o'
      long '--socket-type=OPTION'
      desc 'TCP or UNIX (default: TCP)'
      default 'TCP'
      validate /^(TCP|UNIX)$/
    end

    option :socket_file do
      short '-f'
      long '--socket-file=FILE'
      desc "Full path and name to the UNIX domain socket file (only used with '-o UNIX', default: /tmp/officer.sock)"
    end

    option :port do
      short '-p'
      long '--port=PORT'
      desc 'The port to listen on (default: 11500)'
      cast Integer
    end

    option :log_level do
      short '-l'
      long '--log-level'
      desc 'Set the log level to debug, info, or error (default: error)'
    end

    option :stats do
      short '-s'
      long '--stats'
      desc 'Log stats every 5 seconds (default: off, required log level: info)'
    end

    option :pid_dir do
      short '-d'
      long '--pid-dir'
      desc "Set directory where pid file will be saved (default: operating system's run directory)"
    end

    option :max_idle do
      short '-m'
      long '--max-idle'
      desc "Maximum idle time (in seconds) to wait before closing a connection that is idle and hasn't sent a keep alive (default: 60)"
    end

    option :help do
      long '--help'
    end
  end

  @choices = Choice.choices
end

#set_daemon_optionsObject



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/officer/runner.rb', line 86

def set_daemon_options
  @daemon_options = {
    :dir_mode   => :system,
    :multiple   => false,
    :monitor    => true,
    :log_output => true
  }

  if @choices[:pid_dir]
    @daemon_options[:dir_mode] = :normal
    @daemon_options[:dir] = @choices[:pid_dir]
  end
end

#unhack_argvObject



20
21
22
# File 'lib/officer/runner.rb', line 20

def unhack_argv
  ARGV.unshift(*@saved_args) if @saved_args
end