Class: Moneta::Server

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

Overview

Moneta server to be used together with Moneta::Adapters::Client

Defined Under Namespace

Classes: Connection

Constant Summary collapse

TIMEOUT =
1
MAXSIZE =
0x100000

Instance Method Summary collapse

Constructor Details

#initialize(store, options = {}) ⇒ Server

Returns a new instance of Server.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :port (Integer) — default: 9000

    TCP port

  • :socket (String)

    Alternative Unix socket file name



158
159
160
161
162
163
164
165
166
# File 'lib/moneta/server.rb', line 158

def initialize(store, options = {})
  @store = store
  @server = start(options)
  @ios = [@server]
  @reads = @ios.dup
  @writes = []
  @connections = {}
  @running = false
end

Instance Method Details

#runObject

Note:

This method blocks!

Run the server



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/moneta/server.rb', line 178

def run
  raise 'Already running' if running?
  @stop = false
  @running = true
  begin
    mainloop until @stop
  ensure
    @running = false
    @server.close unless @server.closed?
    @ios
      .reject { |io| io == @server }
      .each { |io| close_connection(io) }
    File.unlink(@socket) if @socket rescue nil
  end
end

#running?Boolean

Is the server running

Returns:

  • (Boolean)

    true if the server is running



171
172
173
# File 'lib/moneta/server.rb', line 171

def running?
  @running
end

#stopObject

Stop the server



195
196
197
198
199
200
# File 'lib/moneta/server.rb', line 195

def stop
  raise 'Not running' unless running?
  @stop = true
  @server.close
  nil
end