delayed jobs and scripts
Today I was facing a rather annoying issue with delayed jobs in rails. I had previously worked with them and as far as I remembered, they were really easy to get working.
The difference this time: I was running a library script (which I had placed in the root lib directory) in my rails application. I was loading the files above the class structure and trying to get delayed jobs to work as follows:
Dir[File.expand_path(Rails.root.join 'lib/parse_scripts','**','*.rb')].each {|f| require f}
class Api::V1::PostsController < ApplicationController
.......
.......
ParseScripts.delay.scrape
This was correctly inserting a delayed job entry into my table. But after a bit, I realized that it was silently failing and deleting itself automatically from the table (An issue that was mentioned in the delayed job wiki). The reason being, the delayed job script has no idea about my ParseScripts module which was not being loaded in the current environment!
Hence, the fix was to require the module files as a rails initializer (config/initializers/load_scripts.rb) so that it was available to the delayed job script as well. And voila! everything works as normal again :)











