Sha256: e14f8ac3e95a3f342e0c0003338f47d4f801c3c4a5454b4632bb9f3ab55d73f0

Contents?: true

Size: 1.02 KB

Versions: 3

Compression:

Stored size: 1.02 KB

Contents

class Fanforce::Worker

  def self.run(init_params, &worker_code)
    self.new(init_params, &worker_code)
  end

  def initialize(init_params, &worker_code)
    require_dependencies
    run_validations

    @init_params = init_params
    @worker_code = worker_code

    msg = iron_mq.queue(init_params['worker_id']).get #(timeout: 3600)

    (msg.nil?) ? (return) : run_worker(msg, 0)
  end


  def require_dependencies
    require '.pluginenv'
    require 'fanforce/api'
    require 'iron_mq'
  end

  def run_validations

  end

  def iron_mq
    @iron_mq ||= IronMQ::Client.new
  end

  def run_worker(msg, retries)
    params = Fanforce.decode_json(msg.body)
    @worker_code.call(params, retries)
    msg.delete
  rescue => e
    params = params.is_a?(Hash) ? {original_params: params} : {original_body: msg.body}
    params[:retries] = retries + 1
    params[:error_message] = e.message
    params[:error_backtrace] = e.backtrace
    Fanforce::Workers.iron.queue "#{@init_params['worker_id']}-ERRORS", params
    msg.delete
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
fanforce-workers-0.1.0 lib/fanforce/workers/worker.rb
fanforce-workers-0.0.4 lib/fanforce/workers/worker.rb
fanforce-workers-0.0.4.pre5 lib/fanforce/workers/worker.rb