Class: AlchemyServer::Runner

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRunner

Returns a new instance of Runner.



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

def initialize
  parse_options

  @process = ProcessHelper.new(options[:log_file], options[:pid_file], options[:user], options[:group])

  pid = @process.running?
  if pid
    STDERR.puts "There is already a alchemy process running (pid #{pid}), exiting."
    exit(1)
  elsif pid.nil?
    STDERR.puts "Cleaning up stale pidfile at #{options[:pid_file]}."
  end

  start
end

Class Method Details

.runObject



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

def self.run
  new
end

Instance Method Details

#drop_privilegesObject



124
125
126
127
# File 'lib/alchemy/runner.rb', line 124

def drop_privileges
  Process.euid = options[:user] if options[:user]
  Process.egid = options[:group] if options[:group]
end

#parse_optionsObject



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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/alchemy/runner.rb', line 32

def parse_options
  self.options = { :host => '127.0.0.1',
                   :port => 22122,
                   :path => File.join(%w( / var spool alchemy )),
                   :log_level => 0,
                   :daemonize => false,
                   :pid_file => File.join(%w( / var run alchemy.pid )) }

  OptionParser.new do |opts|
    opts.summary_width = 25

    opts.banner = "alchemy (#{AlchemyServer::VERSION})\n\n",
                  "usage: alchemy [-v] [-q path] [-h host] [-p port]\n",
                  "                [-d [-P pidfile]] [-u user] [-g group] [-l log]\n",
                  "       alchemy --help\n",
                  "       alchemy --version\n"

    opts.separator ""
    opts.separator "Configuration:"
    opts.on("-q", "--list_path PATH",
            :REQUIRED,
            "Path to store alchemy list logs", "(default: #{options[:path]})") do |list_path|
      options[:path] = list_path
    end

    opts.separator ""; opts.separator "Network:"

    opts.on("-hHOST", "--host HOST", "Interface on which to listen (default: #{options[:host]})") do |host|
      options[:host] = host
    end

    opts.on("-pHOST", "--port PORT", Integer, "TCP port on which to listen (default: #{options[:port]})") do |port|
      options[:port] = port
    end

    opts.separator ""; opts.separator "Process:"

    opts.on("-d", "Run as a daemon.") do
      options[:daemonize] = true
    end

    opts.on("-PFILE", "--pid FILE", "save PID in FILE when using -d option.", "(default: #{options[:pid_file]})") do |pid_file|
      options[:pid_file] = pid_file
    end

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

    opts.on("-gGROUP", "--group GROUP", "Group to run as") do |group|
      options[:group] = group
    end

    opts.separator ""; opts.separator "Logging:"

    opts.on("-l", "--log [FILE]", "Path to print debugging information.") do |log_path|
      options[:log] = log_path
    end

    opts.on("-v", "Increase logging verbosity.") do
      options[:log_level] += 1
    end

    opts.separator ""; opts.separator "Miscellaneous:"

    opts.on_tail("-?", "--help", "Display this usage information.") do
      puts "#{opts}\n"
      exit
    end

    opts.on_tail("-V", "--version", "Print version number and exit.") do
      puts "alchemy #{AlchemyServer::VERSION}\n\n"
      exit
    end
  end.parse!
end

#setup_signal_trapsObject



140
141
142
143
# File 'lib/alchemy/runner.rb', line 140

def setup_signal_traps
  Signal.trap("INT") { shutdown }
  Signal.trap("TERM") { shutdown }
end

#shutdownObject



129
130
131
132
133
134
135
136
137
138
# File 'lib/alchemy/runner.rb', line 129

def shutdown
  begin
    STDOUT.puts "Shutting down."
    @server.logger.info "Shutting down."
    @server.stop
  rescue Object => e
    STDERR.puts "There was an error shutting down: #{e}"
    exit(70)
  end
end

#startObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/alchemy/runner.rb', line 109

def start
  drop_privileges

  @process.daemonize if options[:daemonize]

  setup_signal_traps
  @process.write_pid_file

  STDOUT.puts "Starting at #{options[:host]}:#{options[:port]}."
  @server = AlchemyServer::Base.new(options)
  @server.run

  @process.remove_pid_file
end