Question

I'm trying to use delayed_job to handle some tasks, but it keeps telling me it cannot see the functions in my library. I've included the library in the model where I'm calling delay on the method call.

Job.delay.save_job_data(job_id)

def self.save_job_data(job_id)
  job = Job.find_by(id:job_id)
  file = Marshal.dump(job.image_data)
  save_file_to_AWS(file,job.file_name)
  ...
end

MyLibray
   def save_file_to_AWS(file,file_name)
     ... 
   end
end

Is there a way for a method call though delay to access other parts of my code?

Was it helpful?

Solution 2

It occurred to me that my issue might be related to the fact that I was calling the method as a procedure call since there was no real structure involved. I changed the call to a call on class by wrapping my utilities in a utility class and then calling them via Utility.method.

Delayed_job was happy with that. I didn't proceed with Abdo solution since I don't need it yet, but I'll keep it noted incase I run into the issue again, maybe then we'll know what change pushed delayed_job over the edg.

OTHER TIPS

I've had this issue before and the way I've "fixed" it is by doing the following:

Created an initializer file config/initializers/load_classes_for_dj.rb and put something like this in it:

MyLib::MyClass1
MyLib::MyClass2

# init all classes inside lib/communication for delayed job
Dir["#{Rails.configuration.root}/lib/communication/**/*.rb"].each do |file|
  require file
  # get the ruby files, remove extension and camelize in order to get the class name
  class_name = File.basename(file, ".rb").camelize
  # evaluate class name; this will notify environment about class's existence
  eval(class_name)
end

If there's a better answer, I'b be happy to see it here!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top