Skip to content

Instantly share code, notes, and snippets.

@marckohlbrugge
Created March 7, 2024 11:34
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 marckohlbrugge/f7db398c6ecdf4c78ed90ed65142baca to your computer and use it in GitHub Desktop.
Save marckohlbrugge/f7db398c6ecdf4c78ed90ed65142baca to your computer and use it in GitHub Desktop.
Simple script to move scheduled Sidekiq jobs to Active Job. Not properly tested yet. Use at your own risk!
require "sidekiq/api"
# Fetch scheduled Sidekiq jobs for migration to GoodJob
def fetch_sidekiq_jobs_for_goodjob_migration
raise "Remove this line if you understand this code is not properly tested and you assume the risk of losing data"
puts "Starting to fetch scheduled Sidekiq jobs for migration..."
scheduled_set = Sidekiq::ScheduledSet.new
jobs = scheduled_set.map do |job|
{
"class" => job.klass.constantize, # Ensure this is an ActiveJob class
"args" => job.args,
"run_at" => Time.at(job.at), # The time the job is scheduled to run
"jid" => job.jid
}
end
puts "Fetched #{jobs.count} jobs from Sidekiq scheduled set."
jobs
end
# Migrate jobs to GoodJob and delete them from Sidekiq
def migrate_jobs_to_goodjob(jobs)
puts "Starting migration of #{jobs.count} jobs to GoodJob..."
jobs.each do |job|
next unless job["args"].first["job_class"].constantize <= ActionMailer::MailDeliveryJob
# Schedule the job with GoodJob
job_args = job["args"].first["arguments"]
mailer_class = job_args.first.constantize
mailer_method = job_args.second
args = job_args[3]["args"].collect { GlobalID::Locator.locate(_1.values.first) }
wait_until = job["run_at"]
puts "#{mailer_class}##{mailer_method}(*#{args}) scheduled for #{wait_until}"
mailer_class.public_send(mailer_method, *args).deliver_later(wait_until: wait_until)
# Find the job in the Sidekiq scheduled set and delete it
scheduled_set = Sidekiq::ScheduledSet.new
sidekiq_job = scheduled_set.find_job(job["jid"])
if sidekiq_job
sidekiq_job.delete
puts "Deleted job #{job["jid"]} from Sidekiq scheduled set."
end
puts "Successfully migrated and deleted job: #{job["jid"]}"
rescue => e
# Handle the exception, possibly logging or notifying
puts "An error occurred while migrating job #{job["jid"]}: #{e.message}"
end
puts "Migration process completed."
end
# Example usage
jobs_to_migrate = fetch_sidekiq_jobs_for_goodjob_migration
migrate_jobs_to_goodjob(jobs_to_migrate)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment