Last active
June 24, 2016 08:14
-
-
Save koppen/d0d5184a015b3fecf7dc52e4fe5aaacd to your computer and use it in GitHub Desktop.
Delayed::Job trickery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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