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)
handle_asynchronously
delay.get_data(@query)
. , , , DelayedJobs, , . , . (, SiteRequest).
SiteRequest
class MyModuleName < Struct.new(:query) def perform # TODO end end
get_data(query) , :
get_data(query)
Delayed::Job.enqueue(MyModuleName.new(query))
. :
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