Sha256: 052eacc75a6edf6ed60911d5cdf63b382c28e9c39ee1342529b54a49c820f511

Contents?: true

Size: 1.08 KB

Versions: 4

Compression:

Stored size: 1.08 KB

Contents

module BuildMaster
class ServerManager
  attr_reader :status
  
  def initialize(server)
    @server = server
    @status = 'stopped'
  end
  
  def start
    starting_server
    wait_for_condition {@server.running?}
    @status = 'started'
  end
  
  def stop
    stopping_server
    wait_for_condition {not @server.running?}
    @status = 'stopped'
  end

  private
  def starting_server
    @status = 'starting'
    ['INT', 'TERM'].each { |signal|
         trap(signal){ @server.stop} 
    }  
    start_thread {@server.start}
  end
  
  def stopping_server
    @status = 'stopping'
    start_thread {@server.stop}
  end
  
  def start_thread
    Thread.new do
      begin
        yield
      rescue Exception => exception
        @exception = exception
      end
    end
  end
  
  def wait_for_condition
    count = 0
    sleep 1
    while not (result = yield)
      if (@exception)
        error = @exception
        @exception = nil
        @status = 'error'
        raise error
      end
      count = count + 1
      raise 'wait timed out' unless count < 10
      sleep 1
    end
  end

end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
BuildMaster-1.0.6 lib/buildmaster/project/server_manager.rb
BuildMaster-1.0.9 lib/buildmaster/project/server_manager.rb
BuildMaster-1.1.12 lib/buildmaster/project/server_manager.rb
BuildMaster-1.1.9 lib/buildmaster/project/server_manager.rb