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)
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With