Class: CommunityZero::Server

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

Overview

A single instance of the Community Server.

Author:

Constant Summary collapse

DEFAULT_OPTIONS =

Default options to populate

{
  :host => '127.0.0.1',
  :port => 3389,
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Server

Create a new Community site server.

Parameters:

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

    a list of options to pass in

Options Hash (options):

  • :host (String)

    the host to listen on (default is 0.0.0.0)

  • :port (String, Fixnum)

    the port to listen on (default is 3389)



56
57
58
59
60
# File 'lib/community_zero/server.rb', line 56

def initialize(options = {})
  @options  = DEFAULT_OPTIONS.merge(options)
  @options[:host] = "[#{@options[:host]}]" if @options[:host].include?(':')
  @options.freeze
end

Instance Attribute Details

#optionsHash (readonly)

The list of options passed to the server.

Returns:

  • (Hash)


43
44
45
# File 'lib/community_zero/server.rb', line 43

def options
  @options
end

Instance Method Details

#inspectObject



188
189
190
# File 'lib/community_zero/server.rb', line 188

def inspect
  "#<#{self.class} @url=#{url.inspect}>"
end

#reset!Object

Clear out any existing entires and reset the server’s contents to a clean state.



180
181
182
# File 'lib/community_zero/server.rb', line 180

def reset!
  store.destroy_all
end

#running?Boolean

Boolean method to determine if the server is currently ready to accept requests. This method will attempt to make an HTTP request against the server. If this method returns true, you are safe to make a request.

Returns:

  • (Boolean)

    true if the server is accepting requests, false otherwise



143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/community_zero/server.rb', line 143

def running?
  if @server.nil? || @server.status != :Running
    return false
  end

  uri     = URI.join(url, 'cookbooks')
  headers = { 'Accept' => 'application/json' }

  Timeout.timeout(0.1) { !open(uri, headers).nil? }
rescue SocketError, Errno::ECONNREFUSED, Timeout::Error
  false
end

#start(publish = true) ⇒ nil

Start a Community Zero server in the current thread. You can stop this server by canceling the current thread.

Parameters:

  • publish (Boolean) (defaults to: true)

    publish the server information to STDOUT

Returns:

  • (nil)

    this method will block the main thread until interrupted



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/community_zero/server.rb', line 88

def start(publish = true)
  if publish
    puts <<-EOH.gsub(/^ {10}/, '')
      >> Starting Community Zero (v#{CommunityZero::VERSION})...
      >> WEBrick (v#{WEBrick::VERSION}) on Rack (v#{Rack.release}) is listening at #{url}
      >> Press CTRL+C to stop

    EOH
  end

  thread = start_background

  %w[INT TERM].each do |signal|
    Signal.trap(signal) do
      puts "\n>> Stopping Community Zero..."
      @server.shutdown
    end
  end

  # Move the background process to the main thread
  thread.join
end

#start_background(wait = 5) ⇒ Thread

Start a Community Zero server in a forked process. This method returns the PID to the forked process.

Parameters:

  • wait (Fixnum) (defaults to: 5)

    the number of seconds to wait for the server to start

Returns:

  • (Thread)

    the thread the background process is running in



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/community_zero/server.rb', line 121

def start_background(wait = 5)
  @server = WEBrick::HTTPServer.new(
    :BindAddress => @options[:host],
    :Port        => @options[:port],
    :AccessLog   => [],
    :Logger      => WEBrick::Log.new(StringIO.new, 7)
  )
  @server.mount('/', Rack::Handler::WEBrick, app)

  @thread = Thread.new { @server.start }
  @thread.abort_on_exception = true
  @thread
end

#stop(wait = 5) ⇒ Object

Gracefully stop the Community Zero server.

Parameters:

  • wait (Fixnum) (defaults to: 5)

    the number of seconds to wait before raising force-terminating the server



163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/community_zero/server.rb', line 163

def stop(wait = 5)
  Timeout.timeout(wait) do
    @server.shutdown
    @thread.join(wait) if @thread
  end
rescue Timeout::Error
  if @thread
    $stderr.puts("Community Zero did not stop within #{wait} seconds! Killing...")
    @thread.kill
  end
ensure
  @server = nil
  @thread = nil
end

#storeObject

The data store (by default, this is just a regular store)



65
66
67
# File 'lib/community_zero/server.rb', line 65

def store
  @store ||= Store.new
end

#to_sObject



184
185
186
# File 'lib/community_zero/server.rb', line 184

def to_s
  "#<#{self.class} #{url}>"
end

#urlString

The URL for this Community Zero server.

Returns:

  • (String)


74
75
76
# File 'lib/community_zero/server.rb', line 74

def url
  "http://#{@options[:host]}:#{@options[:port]}"
end