Class: Juggernaut::Runner

Inherits:
Object
  • Object
show all
Includes:
Miscel
Defined in:
lib/juggernaut/runner.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Miscel

#config_path, #log_path, #logger, #options, #options=, #pid_path

Constructor Details

#initialize(argv = ARGV) ⇒ Runner

Returns a new instance of Runner.



15
16
17
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
# File 'lib/juggernaut/runner.rb', line 15

def initialize(argv = ARGV)
  self.options = {
    :host => "0.0.0.0",
    :port => 5001,
    :debug => false,
    :cleanup_timer => 2,
    :timeout => 10,
    :store_messages => false
  }
  
  self.options.merge!({
    :pid_path => pid_path,
    :log_path => log_path,
    :config_path => config_path
  })
  
  parse_options(argv)
  
  if !File.exists?(config_path)
    puts "You must generate a config file (juggernaut -g filename.yml or rake juggernaut:install)"
    exit
  end
  
  config_options = YAML::load(ERB.new(IO.read(config_path)).result)
  config_options = config_options[ENV['RAILS_ENV']] if config_options.has_key?(ENV['RAILS_ENV'])
  options.merge!(config_options)
  
  if options.include?(:kill)
    kill_pid(options[:kill] || '*')
  end
  
  Process.euid = options[:user] if options[:user]
  Process.egid = options[:group] if options[:group]
  
  if !options[:daemonize]
    start
  else
    daemonize
  end
end

Class Method Details

.run(argv = ARGV) ⇒ Object



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

def run(argv = ARGV)
  self.new(argv)
end

Instance Method Details

#parse_options(argv) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/juggernaut/runner.rb', line 87

def parse_options(argv)
  OptionParser.new do |opts|
    opts.summary_width = 25
    opts.banner = "Juggernaut (#{VERSION})\n\n",
                  "Usage: juggernaut [-h host] [-p port] [-P file]\n",
                  "               [-d] [-k port] [-l file] [-e]\n",
                  "       juggernaut --help\n",
                  "       juggernaut --version\n"
    
    opts.separator ""
    opts.separator ""; opts.separator "Configuration:"
    
    opts.on("-g", "--generate FILE", String, "Generate config file", "(default: #{options[:config_path]})") do |v|
      options[:config_path] = File.expand_path(v) if v
      generate_config_file
    end
    
    opts.on("-c", "--config FILE", String, "Path to configuration file.", "(default: #{options[:config_path]})") do |v|
      options[:config_path] = File.expand_path(v)
    end
    
    opts.separator ""; opts.separator "Network:"
    
    opts.on("-h", "--host HOST", String, "Specify host", "(default: #{options[:host]})") do |v|
      options[:host] = v
    end
    
    opts.on("-p", "--port PORT", Integer, "Specify port", "(default: #{options[:port]})") do |v|
      options[:port] = v
    end

    opts.on("-s", "--fdsize SIZE", Integer, "Set the file descriptor size an user epoll() on Linux", "(default: use select() which is limited to 1024 clients)") do |v|
      options[:descriptor_table_size] = v
    end
    
    opts.separator ""; opts.separator "Daemonization:"
    
    opts.on("-P", "--pid FILE", String, "save PID in FILE when using -d option.", "(default: #{options[:pid_path]})") do |v|
      options[:pid_path] = File.expand_path(v)
    end
    
    opts.on("-d", "--daemon", "Daemonize mode") do |v|
      options[:daemonize] = v
    end

    opts.on("-k", "--kill PORT", String, :OPTIONAL, "Kill specified running daemons - leave blank to kill all.") do |v|
      options[:kill] = v
    end
    
    opts.separator ""; opts.separator "Logging:"
    
    opts.on("-l", "--log [FILE]", String, "Path to print debugging information.", "(default: #{options[:log_path]})") do |v|
      options[:log_path] = File.expand_path(v)
    end
    
    opts.on("-e", "--debug", "Run in debug mode", "(default: #{options[:debug]})") do |v|
      options[:debug] = v
    end
    
    opts.separator ""; opts.separator "Permissions:"
    
    opts.on("-u", "--user USER", Integer, "User to run as") do |user|
      options[:user] = user
    end

    opts.on("-G", "--group GROUP", String, "Group to run as") do |group|
      options[:group] = group
    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", "Display version") do |v|
      puts "Juggernaut #{VERSION}"
      exit
    end
  end.parse!(argv)
  options
end

#startObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/juggernaut/runner.rb', line 56

def start
  puts "Starting Juggernaut server #{Juggernaut::VERSION} on port: #{options[:port]}..."
  
  trap("INT") {
    stop
    exit
  }
  trap("TERM"){
    stop
    exit
  }

  if options[:descriptor_table_size]
    EM.epoll
    new_size = EM.set_descriptor_table_size( options[:descriptor_table_size] )
    logger.debug "New descriptor-table size is #{new_size}"
  end
  
  EventMachine::run {
    EventMachine::add_periodic_timer( options[:cleanup_timer] || 2 ) { Juggernaut::Client.send_logouts_after_timeout }
    EventMachine::start_server(options[:host], options[:port].to_i, Juggernaut::Server)
    EM.set_effective_user( options[:user] ) if options[:user]
  }
end

#stopObject



81
82
83
84
85
# File 'lib/juggernaut/runner.rb', line 81

def stop
  puts "Stopping Juggernaut server"
  Juggernaut::Client.send_logouts_to_all_clients
  EventMachine::stop
end