Sha256: f6beea3324ed48ee4115cf9be0b900d63341baa83633d4b9fca2cdc52a612c1b

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

require "rest-client"
require "json"
require "multi_json"
require 'socket'

module WorkerArmy
  class Worker
    attr_accessor :queue, :job, :worker_name
    def initialize(worker_name = nil)
      @queue = WorkerArmy::Queue.new
      @worker_name = worker_name
      @host_name = Socket.gethostname
    end

    def process_queue
      raise "No job class set!" unless @job
      list, element = @queue.pop(@job.class.name)
      if list and element
        puts "List: #{list} => #{element}"
        response_data = {}
        job_count = 0
        begin
          data = JSON.parse(element)
          job_count = data['job_count']
          callback_url = data['callback_url']
          if @job and @job.class.name == data['job_class']
            response_data = @job.perform(data)
            response_data.merge!(job_count: job_count, callback_url: callback_url,
              finished_at: Time.now.utc.to_i, host_name: @host_name)
            if @worker_name
              response_data.merge!(worker_name: @worker_name)
            end
          end
        rescue => e
          puts e
        end
        if response_data
          begin
            response = RestClient.post data['callback_url'],
              response_data.to_json, :content_type => :json, :accept => :json
          rescue => e
            puts e
          end
        end
        self.process_queue
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
worker-army-0.2.0 lib/worker_army/worker.rb