Class: AlchemyServer::Base

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

Constant Summary collapse

DEFAULT_HOST =
'127.0.0.1'
DEFAULT_PORT =
22122
DEFAULT_PATH =
"/tmp/alchemy/"
DEFAULT_TIMEOUT =
60

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Base

Initialize a new Alchemy server, but do not accept connections or process requests.

opts is as for start



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/alchemy/server.rb', line 49

def initialize(opts = {})
  @opts = { 
    :host    => DEFAULT_HOST,
    :port    => DEFAULT_PORT,
    :path    => DEFAULT_PATH,
    :timeout => DEFAULT_TIMEOUT,
    :server  => self
  }.merge(opts)

  @stats = Hash.new(0)
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



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

def logger
  @logger
end

Class Method Details

.start(opts = {}) ⇒ Object

Initialize a new Alchemy server and immediately start processing requests.

opts is an optional hash, whose valid options are:

[:host]     Host on which to listen (default is 127.0.0.1).
[:port]     Port on which to listen (default is 22122).
[:path]     Path to Alchemy list logs. Default is /tmp/alchemy/
[:timeout]  Time in seconds to wait before closing connections.
[:logger]   A Logger object, an IO handle, or a path to the log.
[:loglevel] Logger verbosity. Default is Logger::ERROR.

Other options are ignored.



38
39
40
41
# File 'lib/alchemy/server.rb', line 38

def self.start(opts = {})
  server = self.new(opts)
  server.run
end

Instance Method Details

#runObject

Start listening and processing requests.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/alchemy/server.rb', line 64

def run
  @stats[:start_time] = Time.now

  @logger = case @opts[:logger]
  when IO, String; Logger.new(@opts[:logger])
  when Logger; @opts[:logger]
  else; Logger.new(STDERR)
  end

  @opts[:list] = Phylactery.new
  @logger.level = @opts[:log_level] || Logger::ERROR

  EventMachine.run do
    EventMachine.epoll
    EventMachine.set_descriptor_table_size(4096)
    EventMachine.start_server(@opts[:host], @opts[:port], Handler, @opts)
  end
end

#stats(stat = nil) ⇒ Object

:nodoc:



91
92
93
94
95
96
97
# File 'lib/alchemy/server.rb', line 91

def stats(stat = nil) #:nodoc:
  case stat
  when nil; @stats
  when :connections; 1
  else; @stats[stat]
  end
end

#stopObject

Stop accepting new connections and shutdown gracefully.



86
87
88
89
# File 'lib/alchemy/server.rb', line 86

def stop
  @opts[:list].close
  EventMachine.stop_event_loop
end