Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby on rails: delayed_job does not execute function from module

I want to use delayed_job to execute a function from controller. The function is stored in module lib/site_request.rb:

module SiteRequest

  def get_data(query)
     ...
  end
  handle_asynchronously :get_data

end

query_controller.rb:

class QueryController < ApplicationController

  include SiteRequest

  def index
    @query = Query.find_or_initialize_by_word(params[:query])
    if @query.new_record?
      @query.save
      get_data(@query)
      flash[:notice] = "Request for data is sent to server."
    end
  end

end

I also tried to remove handle_asynchronously clause from module and use delay.get_data(@query), both do not executed silently (without delayed_job code works)

like image 925
zeliboba7 Avatar asked Nov 13 '22 20:11

zeliboba7


1 Answers

I had trouble trying to use the built-in delay methods myself, too. The pattern I settled on in my own coding was to enqueue DelayedJobs myself, giving them a payload object from which to work. This should work for you too and would seem to make sense even. (This way, you may not even need your SiteRequest module, for example.)

class MyModuleName < Struct.new(:query)

  def perform
     # TODO
  end

end

Then, instead of calling get_data(query) after saving, enqueue with:

Delayed::Job.enqueue(MyModuleName.new(query))
like image 114
pdobb Avatar answered Dec 07 '22 00:12

pdobb