Class: StarlingServer::Base

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

Constant Summary collapse

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Base

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

opts is as for start



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/starling/server.rb', line 50

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

  @stats = Hash.new(0)

  FileUtils.mkdir_p(@opts[:path])

end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



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

def logger
  @logger
end

Class Method Details

.loggerObject



93
94
95
# File 'lib/starling/server.rb', line 93

def self.logger
  @@logger
end

.start(opts = {}) ⇒ Object

Initialize a new Starling 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 Starling queue logs. Default is /tmp/starling/
[: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.



39
40
41
42
# File 'lib/starling/server.rb', line 39

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

Instance Method Details

#runObject

Start listening and processing requests.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/starling/server.rb', line 68

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
  @@logger = SyslogLogger.new(@opts[:syslog_channel]) if @opts[:syslog_channel]

  @opts[:queue] = QueueCollection.new(@opts[:path])
  @@logger.level = @opts[:log_level] || Logger::ERROR

  @@logger.info "Starling STARTUP on #{@opts[:host]}:#{@opts[:port]}"

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

  # code here will get executed on shutdown:
  @opts[:queue].close
end

#stats(stat = nil) ⇒ Object

:nodoc:



105
106
107
108
109
110
111
# File 'lib/starling/server.rb', line 105

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.



101
102
103
# File 'lib/starling/server.rb', line 101

def stop
  EventMachine.stop_event_loop
end