I have a controller action that I would like to be handled asynchronously.
class CollectionsController < ApplicationController
def add
#code
end
handle_asynchronously :add
When this is called I get a: TypeError: can't dump anonymous module
The delayed_job documentation isn't clear whether the method has to be an ActiveRecord model method. I have seen examples where people use other classes to handle this, however my method uses session information. It's unclear to me whether that information would be available to another class.
Any ideas?
Thanks.
Delayed jobs don't have to be an ActiveRecord model, you can add the functionality to a plain old Ruby class, see https://github.com/collectiveidea/delayed_job#custom-jobs
You probably don't want the controller action to be handled asynchronously as this would add an unnecessary delay to the HTTP request. My advice would be to queue up a job in the controller like so:
class CollectionsController < ApplicationController
def add
Delayed::Job.enqueue CollectionBuilderJob.new(@current_user.session_info)
end
end
class CollectionBuilderJob < Struct.new(:session_info)
def perform
#code
end
end
This approach allows you to test your delayed job in isolation
You can't use DJ on a controller method. Move it into a model.
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