Class: Waves::Server

Inherits:
Application show all
Defined in:
lib/runtime/server.rb

Overview

You can run the Waves::Server via the waves-server command or via rake cluster:start. Run waves-server --help for options on the waves-server command. The cluster:start task uses the mode environment variable to determine the active configuration. You can define port to run on a single port, or ports (taking an array) to run on multiple ports.

Example

Assume that ports is set in the development configuration like this:

ports [ 2020, 2021, 2022 ]

Then you could start up instances on all three ports using:

rake cluster:start mode=development

This is the equivalent of running:

waves-server -c development -p 2020 -d
waves-server -c development -p 2021 -d
waves-server -c development -p 2022 -d

The cluster:stop task stops all of the instances.

Instance Attribute Summary collapse

Attributes inherited from Application

#options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Application

#cache, #config, #debug?, #initialize, #mapping, #mode, #reload

Constructor Details

This class inherits a constructor from Waves::Application

Instance Attribute Details

#threadObject (readonly)

Access the server thread.



25
26
27
# File 'lib/runtime/server.rb', line 25

def thread
  @thread
end

Class Method Details

.method_missing(*args) ⇒ Object

Allows us to access the Waves::Server instance.



84
85
86
# File 'lib/runtime/server.rb', line 84

def method_missing(*args)
  @server.send(*args)
end

.run(options = {}) ⇒ Object

Start or restart the server.



79
80
81
# File 'lib/runtime/server.rb', line 79

def run( options={} )
  @server.stop if @server; @server = new( options ); @server.start
end

.synchronize(&block) ⇒ Object

– Probably wouldn’t need this if I added a block parameter to method_missing.



89
# File 'lib/runtime/server.rb', line 89

def synchronize(&block) ; @server.synchronize(&block) ; end

Instance Method Details

#daemonizeObject

Run the server as a daemon. Corresponds to the -d switch on waves-server.



34
35
36
37
38
39
# File 'lib/runtime/server.rb', line 34

def daemonize
  pwd = Dir.pwd
  Daemonize.daemonize( Waves::Logger.output )
  Dir.chdir(pwd)
  File.write( :log / "#{port}.pid", $$ )
end

#hostObject

Access the host we’re binding to (set via the configuration).



28
# File 'lib/runtime/server.rb', line 28

def host ; options[:host] || config.host ; end

#logObject

Start and / or access the Waves::Logger instance.



47
48
49
# File 'lib/runtime/server.rb', line 47

def log
  @log ||= Waves::Logger.start
end

#portObject

Access the port we’re listening on (set via the configuration).



31
# File 'lib/runtime/server.rb', line 31

def port ; options[:port] || config.port ; end

#startObject

Start the server.



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

def start
  daemonize if options[:daemon]
  start_debugger if options[:debugger]
  log.info "** Waves Server starting  on #{host}:#{port}"
  handler, options = config.handler
  handler.run( config.application.to_app, options ) do |server|
    @server = server
    self.trap('INT') { puts; stop } if @server.respond_to? :stop
  end
end

#stopObject

Stop the server.



64
65
66
67
68
69
70
71
# File 'lib/runtime/server.rb', line 64

def stop
  log.info "** Waves Server Stopping ..."
  if options[:daemon]
    pid_file = :log / $$ + '.pid'; FileUtils.rm( pid_file ) if File.exist?( pid_file )
  end
  @server.stop
  log.info "** Waves Server Stopped"
end

#synchronize(&block) ⇒ Object

Provides access to the server mutex for thread-safe operation.



74
# File 'lib/runtime/server.rb', line 74

def synchronize( &block ) ; ( @mutex ||= Mutex.new ).synchronize( &block ) ; end

#trap(signal) ⇒ Object



41
42
43
44
# File 'lib/runtime/server.rb', line 41

def trap(signal)
  Kernel::trap(signal) { yield }
  Thread.new { loop {sleep 1} } if RUBY_PLATFORM =~ /mswin32/
end