class JobDispatch::Client

This is a simple class for making synchronous calls to the Job Queue dispatcher.

This is a simple class for making synchronous calls to the Job Queue dispatcher.

This is a simple class for making synchronous calls to the Job Queue dispatcher.

This is a simple class for making synchronous calls to the Job Queue dispatcher.

Public Class Methods

new(connect_address=nil) click to toggle source
# File lib/job_dispatch/client.rb, line 9
def initialize(connect_address=nil)
  @socket = JobDispatch.context.socket(ZMQ::REQ)
  @socket.connect(connect_address || JobDispatch.config.broker[:connect])
end

Public Instance Methods

close() click to toggle source
# File lib/job_dispatch/client.rb, line 37
def close
  if @socket
    @socket.close
    @socket = nil
  end
end
enqueue(job_attrs={}) click to toggle source

Enqueue a job to be processed describe by the passed job attributes.

Required attributes:

target: The target object that will execute the job. typically a class.
method: the message to be sent to the target.

Optional:

parameters: an array of parameters to be passed to the method.
timeout: number of seconds after which the job is considered timed out and failed.
# File lib/job_dispatch/client.rb, line 52
def enqueue(job_attrs={})
  send_request('enqueue', {job: job_attrs})
end
fetch(job_id) click to toggle source

fetch the complete details for hte last job

# File lib/job_dispatch/client.rb, line 67
def fetch(job_id)
  job_or_raise send_request('fetch', {job_id: job_id})
end
last(queue=nil) click to toggle source

as the dispatcher what was the last job enqueued on the given queue (or default)

# File lib/job_dispatch/client.rb, line 62
def last(queue=nil)
  job_or_raise send_request('last', {queue: queue||'default'})
end
method_missing(method, *args, ** kwargs) click to toggle source
# File lib/job_dispatch/client.rb, line 23
def method_missing(method, *args, ** kwargs)
  payload = kwargs
  payload[:parameters] = args
  send_request(method, payload)
end
notify(job_id) click to toggle source

send a message to the dispatcher requesting to be notified when the job completes (or fails).

# File lib/job_dispatch/client.rb, line 57
def notify(job_id)
  send_request('notify', {job_id: job_id})
end
proxy_for(target, options={}) click to toggle source
# File lib/job_dispatch/client.rb, line 29
def proxy_for(target, options={})
  Proxy.new(self, target, options)
end
send_request(command, options={}) click to toggle source
# File lib/job_dispatch/client.rb, line 14
def send_request(command, options={})
  options[:command] = command
  @socket.send(JSON.dump(options))
  json = @socket.recv
  #puts "Received: #{json}"
  response = JSON.parse(json)
  response.is_a?(Hash) ? response.with_indifferent_access : response
end
synchronous_proxy_for(target, options={}) click to toggle source
# File lib/job_dispatch/client.rb, line 33
def synchronous_proxy_for(target, options={})
  SynchronousProxy.new(self, target, options)
end

Private Instance Methods

job_or_raise(response) click to toggle source
# File lib/job_dispatch/client.rb, line 73
def job_or_raise(response)
  if response.is_a?(Hash) && response[:status] == 'success'
    response[:job]
  else
    p response
    raise ClientError, response[:message]
  end
end