Skip to content

Instantly share code, notes, and snippets.

@jsmpereira
Created August 27, 2010 01:12
Show Gist options
  • Save jsmpereira/552575 to your computer and use it in GitHub Desktop.
Save jsmpereira/552575 to your computer and use it in GitHub Desktop.
require 'active_record'
# Migrating from http://github.com/tobi/delayed_job to http://github.com/collectiveidea/delayed_job
# and with some jobs pending resulted in execution failure due to handler format incompatibility.
#
# This rake task loops through your delayed_jobs table and updates the handler of each job to comply with
# collectiveidea's delayed_job.
#
# Hope this can be useful to someone.
#
# Convert
#
# http://github.com/tobi/delayed_job
# Job handler format:
#
# --- !ruby/struct:Delayed::PerformableMethod
# object: CLASS:Notifier
# method: :deliver_activation_confirmation
# args:
# - AR:User:20
#
# to
#
# http://github.com/collectiveidea/delayed_job
# Job handler format:
#
# --- !ruby/struct:Delayed::PerformableMethod
# object: !ruby/class Notifier
# method: :deliver_activation_confirmation
# args: !ruby/ActiveRecord:User
# attributes:
# id: "20"
# created_at: 2009-04-03 01:12:28
# state: active
# etc ...
namespace :dj_migrate_format do
task :start_task => :environment do
Delayed::Job.all.each do |job|
handler_yaml = YAML.load(job.handler)
object = handler_yaml[:object].split(":")[1].constantize
method = handler_yaml[:method]
args_model = handler_yaml[:args].first.split(":")[1]
args_id = handler_yaml[:args].first.split(":")[2]
args = args_model.constantize.find args_id
new_handler = Delayed::PerformableMethod.new object, method, args
new_handler_yaml = new_handler.to_yaml
job.handler = new_handler_yaml
job.save
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment