Skip to content

Instantly share code, notes, and snippets.

@koppen
Last active June 24, 2016 08:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koppen/d0d5184a015b3fecf7dc52e4fe5aaacd to your computer and use it in GitHub Desktop.
Save koppen/d0d5184a015b3fecf7dc52e4fe5aaacd to your computer and use it in GitHub Desktop.
Delayed::Job trickery
# Recreate ActiveRecord models from Delayed Job jobs.
# Ensure that we recreate the full model
ActiveRecord::Base.partial_updates = false
Delayed::Job.all.each do |job|
# Deserialize the Delayed Job handler. As we've been using the `.delay` syntax
# and no custom jobs, this returns a Delayed::PerformableMethod
pfm = YAML.load(job.handler)
# Delayed::PerformableMethod#object returns the original object that we called
# `delay` on. That object has a reference to the ActiveRecord model we're
# interested in.
stt = pfm.object.seo_text_template
# Trick ActiveRecord into not believing this record has already been
# persisted.
def stt.new_record?; true; end
# Save the record with its current id unless we've already recreated it
stt.save unless SeoTextTemplate.find_by_id(stt.id)
end
# Re-enqueue failed (well, all) jobs
jobs = Delayed::Job.all
jobs.update_all(:locked_at => nil, :locked_by => nil, :failed_at => nil)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment