Class: Rocket::Server::Runner

Inherits:
Object
  • Object
show all
Includes:
Daemonize, Helpers
Defined in:
lib/rocket/server/runner.rb

Constant Summary collapse

LOG_MESSAGES =
{
  :starting_server => "Server is listening at %s:%s (CTRL+C to stop)",
  :stopping_server => "Stopping Rocket server..."
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#log

Constructor Details

#initialize(options = {}) ⇒ Runner

Returns a new instance of Runner.



21
22
23
24
25
26
27
28
29
30
# File 'lib/rocket/server/runner.rb', line 21

def initialize(options={})
  @options = options.dup
  @host    = @options[:host] || 'localhost'
  @port    = @options[:port] || 9772
  @pidfile = @options.delete(:pid) || "/var/run/rocket/server.pid"
  @daemon  = @options.delete(:daemon)
  
  # Remove unnecessary options. 
  @options.reject!{|k,v| [:verbose, :quiet, :log, :apps, :plugins].include?(k) }
end

Instance Attribute Details

#daemonObject (readonly)

Returns the value of attribute daemon.



19
20
21
# File 'lib/rocket/server/runner.rb', line 19

def daemon
  @daemon
end

#hostObject (readonly)

Returns the value of attribute host.



16
17
18
# File 'lib/rocket/server/runner.rb', line 16

def host
  @host
end

#optionsObject (readonly)

Returns the value of attribute options.



15
16
17
# File 'lib/rocket/server/runner.rb', line 15

def options
  @options
end

#pidfileObject (readonly)

Returns the value of attribute pidfile.



18
19
20
# File 'lib/rocket/server/runner.rb', line 18

def pidfile
  @pidfile
end

#portObject (readonly)

Returns the value of attribute port.



17
18
19
# File 'lib/rocket/server/runner.rb', line 17

def port
  @port
end

Instance Method Details

#daemonize!(&block) ⇒ Object



63
64
65
66
# File 'lib/rocket/server/runner.rb', line 63

def daemonize!(&block)
  daemonize
  start!(&block)
end

#kill!Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/rocket/server/runner.rb', line 52

def kill!
  if File.exists?(pidfile)
    pid = File.read(pidfile).chomp.to_i
    FileUtils.rm pidfile
    Process.kill(9, pid)
    pid
  end
rescue Object => e
  # nothing to show
end

#save_pidObject



68
69
70
71
72
73
74
# File 'lib/rocket/server/runner.rb', line 68

def save_pid
  FileUtils.mkdir_p(File.dirname(pidfile))
  File.open(pidfile, "w+"){|f| f.write("#{Process.pid}\n")}
rescue Object => e
  puts e.to_s
  exit 1
end

#start!(&block) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rocket/server/runner.rb', line 32

def start!(&block)
  if daemon
    daemonize!(&block)
  else
    EM.epoll
    EM.run do
      trap("TERM") { stop! }
      trap("INT")  { stop! }
      
      log_info(:starting_server, host, port.to_s)
      EM::start_server(host, port, Connection, options, &block)
    end
  end
end

#stop!Object



47
48
49
50
# File 'lib/rocket/server/runner.rb', line 47

def stop!
  log_info(:stopping_server)
  EM.stop
end