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



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/starling/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)

  FileUtils.mkdir_p(@opts[:path])

end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



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

def logger
  @logger
end

Class Method Details

.loggerObject



105
106
107
# File 'lib/starling/server.rb', line 105

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.



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

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

Instance Method Details

#runObject

Start listening and processing requests.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/starling/server.rb', line 67

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

  if @opts[:syslog_channel]
    begin
      require 'syslog_logger'
      @@logger = SyslogLogger.new(@opts[:syslog_channel])
    rescue LoadError
      # SyslogLogger isn't available, so we're just going to use Logger
    end
  end

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

  begin
    @opts[:queue] = QueueCollection.new(@opts[:path])
  rescue InaccessibleQueuePath => e
    puts "Error: #{e.message}"
    exit 1
  end
  @@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:



117
118
119
120
121
122
123
# File 'lib/starling/server.rb', line 117

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.



113
114
115
# File 'lib/starling/server.rb', line 113

def stop
  EventMachine.stop_event_loop
end