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
29
30
31
32
33
34
# 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
    return :ok
  rescue BackgroundQueue::ServerLib::WorkerError => we
    @server.logger.error("Worker Error sending request #{task.id} to worker: #{we.message}")
    return :worker_error
  rescue BackgroundQueue::ServerLib::ThreadManager::ForcedStop => fe
    @server.logger.error("Thread stop while sending request #{task.id} to worker: #{fe.message}")
    return :stop
  rescue Exception=>e
    @server.logger.error("Error sending request #{task.id} to worker: #{e.message}")
    @server.logger.debug(e.backtrace.join("\n"))
    return :fatal_error
  end
end