Search code examples
ruby-on-railsrubyruby-on-rails-5ruby-on-rails-6rails-activejob

ActiveJob uninitialized constant


I have a weird problem with ActiveJob.

From a controller I'm executing the following sentence:

ExportJob.set(wait: 5.seconds).perform([A series of parameters, basically strings and integers])

ExportJob.rb

require_relative 'blablabla/resource_manager'

class ExportJob < ActiveJob::Base
   def perform
       ResourceManager.export_process([A series of parameters, basically strings and integers])
   end
end

When the controller/action is executed for the first time the process goes fine, but the second time an error is thrown:

uninitialized constant ExportJob::ResourceManager

The weird thing is that this is not the only job I have in my project, the other ones are being executed without any problem.

I'm Attaching some information of my project:

development/production.rb

config.active_job.queue_adapter = :delayed_job

Gemfile:

gem 'delayed_job'

gem 'delayed_job_active_record'

Any clue would be a help for me.

Thanks in advance!


Solution

  • Constants don't have global scope in Ruby. Constants can be visible from any scope, but you must specify where the constant is to be found.

    Without :: Ruby looks for the ResourceManager constant in lexical scope of the currently executing code (which is ExportJob class, so it looks for ExportJob::ResourceManager).

    The following should work (assuming that ResourceManager is defined as a top level constant (eg not nested under any module/class):

    class ExportJob < ActiveJob::Base
      def perform
        ::ResourceManager.export_process(*args)
      end
    end