redis - Persist job results with Resque / Rails -


in rails app, i'm trying take working api calls , have them handled background workers.

i have following in app/jobs/api_request_job.rb:

class apirequestjob   def self.perform(params)     query.new(params).start      end end 

the query class httparty requests being executed (there lots of methods different query types same basic format parks method:

require 'ostruct'  class query   include foursquare    attr_reader   :results,                 :first_address,                 :second_address,                 :queries,                  :radius    def initialize(params)     @results        = openstruct.new     @queries        = params["query"]     @first_address  = params["first_address"]     @second_address = params["second_address"]      @radius         = params["radius"].to_f    end    def start     queries.keys.each |query|       results[query] = self.send(query)     end     results   end  def parks   category_id = "4bf58dd8d48988d163941735"   first_address_results = foursquare.send_request(@first_address, radius_to_meters, category_id)["response"]["venues"]   second_address_results = foursquare.send_request(@second_address, radius_to_meters, category_id)["response"]["venues"]   response = [first_address_results, second_address_results] end 

and, finally, controller. before trying farm action out background workers, line working fine: @results = query.new(params).start

class comparisonscontroller < applicationcontroller   attr_reader :first_address, :second_address    def new   end    def show     @first_address = address.new(params["first_address"])     @second_address = address.new(params["second_address"])     if @first_address.invalid?       flash[:notice] = @first_address.errors.full_messages       redirect_to :back         elsif query.new(params).queries.nil?        flash[:notice] = "you must choose @ least 1 criteria comparison."       redirect_to comparisons_new_path(request.params)     else       @queries = params["query"].keys       @results = resque.enqueue(apirequestjob, params)  # <-- i'm stuck     end   end  end 

i'm running redis, have resque installed, , running task/starting workers. current value being returned @results true instead of hash of results need back. there way have results of resque job persist , return data instead of true? missing how background workers return same type of data regular api calls returning?

many in advance!

the true receiving means job scheduled enqueued successfully. worker pick , run on background asynchronously, means, not @ same time thread enqueued it. there's no way retrieve returned value job.

if need value process, have run controller without worker. also, wouldn't gain pushing work done process web process have wait response keep going anyway.

if need returned value right away , doing performance reasons, other forms of concurrency, having thread doing request , grabbing result when need on view like:

class asyncvalue   def initialize(&block)     @thr = thread.new(&block)   end    def value     @thr.join   end end 

on controller

@results = asyncvalue.new { query.new(params).start  } 

and on view

<%= @results.value.each .... %> 

but you'd still have work around error handling can pretty complicated, doable.

personally, i'd make request in place, know domain better me.


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -