Class: CommunityZero::Server
- Inherits:
-
Object
- Object
- CommunityZero::Server
- Defined in:
- lib/community_zero/server.rb
Overview
A single instance of the Community Server.
Constant Summary collapse
- DEFAULT_OPTIONS =
Default options to populate
{ :host => '127.0.0.1', :port => 3389, }.freeze
Instance Attribute Summary collapse
-
#options ⇒ Hash
readonly
The list of options passed to the server.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Server
constructor
Create a new Community site server.
- #inspect ⇒ Object
-
#reset! ⇒ Object
Clear out any existing entires and reset the server’s contents to a clean state.
-
#running? ⇒ Boolean
Boolean method to determine if the server is currently ready to accept requests.
-
#start(publish = true) ⇒ nil
Start a Community Zero server in the current thread.
-
#start_background(wait = 5) ⇒ Thread
Start a Community Zero server in a forked process.
-
#stop(wait = 5) ⇒ Object
Gracefully stop the Community Zero server.
-
#store ⇒ Object
The data store (by default, this is just a regular store).
- #to_s ⇒ Object
-
#url ⇒ String
The URL for this Community Zero server.
Constructor Details
#initialize(options = {}) ⇒ Server
Create a new Community site server.
56 57 58 59 60 |
# File 'lib/community_zero/server.rb', line 56 def initialize( = {}) @options = DEFAULT_OPTIONS.merge() @options[:host] = "[#{@options[:host]}]" if @options[:host].include?(':') @options.freeze end |
Instance Attribute Details
#options ⇒ Hash (readonly)
The list of options passed to the server.
43 44 45 |
# File 'lib/community_zero/server.rb', line 43 def @options end |
Instance Method Details
#inspect ⇒ Object
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.
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.
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.
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.
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 |
#store ⇒ Object
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_s ⇒ Object
184 185 186 |
# File 'lib/community_zero/server.rb', line 184 def to_s "#<#{self.class} #{url}>" end |
#url ⇒ String
The URL for this Community Zero server.
74 75 76 |
# File 'lib/community_zero/server.rb', line 74 def url "http://#{@options[:host]}:#{@options[:port]}" end |