Ruby on rails: delayed_job does not execute a function from a module

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

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 the sentence handle_asynchronouslyfrom the module and use delay.get_data(@query), both are not executed without interference (without work with delay_job)

+5
source share
2 answers

. , , , DelayedJobs, , . , . (, SiteRequest).

class MyModuleName < Struct.new(:query)

  def perform
     # TODO
  end

end

get_data(query) , :

Delayed::Job.enqueue(MyModuleName.new(query))
+1

. :

  • Ruby 2.1.7
  • Rails 4.2.6
  • activejob (4.2.6)
  • delayed_job (4.1.2)
  • delayed_job_active_record (4.1.1) : . . , ActiveJob . :
Class SiteRequest
  def initialize
  end
  def get_data(query)
     ...
  end
  handle_asynchronously :get_data
end
def index
  ...

  q= SiteRequest.new
  q.get_data(@query)
  flash[:notice] = "Request for data is sent to server."
end

0

All Articles