Class: BackgroundQueue::ServerLib::WorkerClient

Inherits:
Object
  • Object
show all
Defined in:
lib/background_queue/server_lib/worker_client.rb

Overview

The client to a worker. Use http to connect to the worker, send the command, and process the streamed response of json encoded status updates.

Instance Method Summary collapse

Constructor Details

#initialize(server) ⇒ WorkerClient

Returns a new instance of WorkerClient.



8
9
10
# File 'lib/background_queue/server_lib/worker_client.rb', line 8

def initialize(server)
  @server = server
end

Instance Method Details

#send_request(worker, task, secret) ⇒ Object

send a request to the specified worker, passing the task and authenticating using the secret



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/background_queue/server_lib/worker_client.rb', line 13

def send_request(worker, task, secret)
  @current_task = task
  req = build_request(worker.uri, task, secret)
  begin
    Net::HTTP.start(worker.uri.host, worker.uri.port)  do |server|
      server.request(req) do |response|
        read_response(worker, response, task)
      end
    end
    true
  rescue Exception=>e
    @server.logger.error("Error sending request #{task.id} to worker: #{e.message}")
    @server.logger.debug(e.backtrace.join("\n"))
    return false
  end
end