Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 delayed_job "TypeError: can't dump anonymous module"

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.

like image 213
mstrom Avatar asked Aug 29 '13 14:08

mstrom


2 Answers

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

like image 143
james_bowles Avatar answered Nov 15 '22 14:11

james_bowles


You can't use DJ on a controller method. Move it into a model.

like image 22
Kevin Avatar answered Nov 15 '22 14:11

Kevin