Rails 3 delayed_job "TypeError: cannot reset anonymous module"

I have a controller action with which I would like to process asynchronously.

class CollectionsController < ApplicationController def add #code end handle_asynchronously :add 

When this is called, I get: TypeError: cannot reset anonymous module

The documentation for delayed_job is unclear whether the method should be an ActiveRecord model method. I have seen examples where people use other classes to handle this, however my method uses session information. It is not clear to me whether this information will be available for another class.

Any ideas?

Thanks.

+6
source share
2 answers

The given task does not have to be an ActiveRecord model, you can add functionality to the plain old Ruby class, see https://github.com/collectiveidea/delayed_job#custom-jobs

You probably do not want the controller action to be processed asynchronously, as this would add unnecessary delay to the HTTP request. My advice would be to set the task in the dispatcher like this:

 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 isolate pending work

+8
source

You cannot use DJ using the controller method. Move it to the model.

+5
source

Source: https://habr.com/ru/post/952743/


All Articles