lib/baleen/server.rb in baleen-0.1.1 vs lib/baleen/server.rb in baleen-0.1.2
- old
+ new
@@ -6,61 +6,83 @@
module Baleen
class Server
include Celluloid::IO
+ include Default
+
finalizer :shutdown
- def initialize(docker_host: "127.0.0.1", docker_port: 4243, port: 5533)
+ def initialize(docker_host, docker_port, port, project_file)
Docker.url = "http://#{docker_host}:#{docker_port}"
+ Baleen::Project.load_project(project_file)
@server = TCPServer.new("0.0.0.0", port)
async.run
end
def run
loop { async.handle_connection @server.accept }
end
def shutdown
- @server.close if @server
+ begin
+ @server.close
+ rescue IOError
+ BL.info "Shutting down baleen-server..."
+ end
end
def handle_connection(socket)
loop { handle_request(socket) }
rescue Exception => ex
case ex
when IOError; nil # when trying to close already closed socket
else
- warn "Unknown exception occured"
puts ex.inspect
raise ex
end
end
def handle_request(socket)
- message = socket.gets
+ json_request = socket.gets
- if message.nil?
+ if json_request.nil?
socket.close
return
end
- msg = parse_request(message)
- case msg
- when Message::Request::ClientDisconnect
- socket.close
- when Message::Request::Cucumber
- manager = RunnerManager.new(socket, msg)
- manager.run
+ conn = Connection.new(socket)
+ request = parse_request(json_request)
+
+ begin
+ if request.is_a? Baleen::Task::RunProject
+ task = find_project(request.project, conn).task
else
- warn "Received unknown request"
- puts msg.inspect
+ task = request # request itself is a task
+ end
+ rescue Baleen::Error::ProjectNotFound
+ return
end
+
+ RunnerManager.new(conn, task).run do |response|
+ conn.respond(response)
+ end
end
- def parse_request(message)
- Baleen::Message::Decoder.new(message).decode
+ def parse_request(request)
+ Serializable.deserialize(request)
end
+
+ def find_project(name, conn)
+ project = Baleen::Project.find_project_by_name(name.to_sym)
+
+ unless project
+ conn.notify_exception("No project found: #{name}")
+ raise Baleen::Error::ProjectNotFound
+ end
+
+ project
+ end
end
+end
-end
\ No newline at end of file