Move `MigrateScheme` to new Onceoff thing

This commit is contained in:
Robin Ward 2016-04-07 15:31:32 -04:00
parent 3f7ced9236
commit 9d8db11cf3
3 changed files with 36 additions and 48 deletions

View File

@ -10,6 +10,7 @@ class Jobs::Onceoff < Jobs::Base
job_name = self.class.name_for(self.class)
if args[:force] || !OnceoffLog.where(job_name: job_name).exists?
return if $redis.exists(self.class.name)
DistributedMutex.synchronize(self.class.name) do
execute_onceoff(args)
OnceoffLog.create(job_name: job_name)

View File

@ -0,0 +1,35 @@
module Jobs
class MigrateScheme < Jobs::Onceoff
def execute(args)
return unless SiteSetting.migrate_to_new_scheme
# clean up failed uploads
Upload.where("created_at < ?", 1.hour.ago)
.where("LENGTH(COALESCE(url, '')) = 0")
.destroy_all
# migrate uploads to new scheme
problems = Upload.migrate_to_new_scheme
problems.each do |hash|
upload_id = hash[:upload].id
Discourse.handle_job_exception(hash[:ex], error_context(args, "Migrating upload id #{upload_id}", upload_id: upload_id))
end
# clean up failed optimized images
OptimizedImage.where("LENGTH(COALESCE(url, '')) = 0").destroy_all
# Clean up orphan optimized images
OptimizedImage.where("upload_id NOT IN (SELECT id FROM uploads)").destroy_all
# migrate optimized_images to new scheme
problems = OptimizedImage.migrate_to_new_scheme
problems.each do |hash|
optimized_image_id = hash[:optimized_image].id
Discourse.handle_job_exception(hash[:ex], error_context(args, "Migrating optimized_image id #{optimized_image_id}", optimized_image_id: optimized_image_id))
end
end
end
end

View File

@ -1,48 +0,0 @@
module Jobs
class MigrateScheme < Jobs::Scheduled
every 10.minutes
sidekiq_options retry: false
MIGRATE_SCHEME_KEY ||= "migrate_to_new_scheme"
def execute(args)
begin
return unless SiteSetting.migrate_to_new_scheme
return if $redis.exists(MIGRATE_SCHEME_KEY)
# use a mutex to make sure this job is only run once
DistributedMutex.synchronize(MIGRATE_SCHEME_KEY) do
# clean up failed uploads
Upload.where("created_at < ?", 1.hour.ago)
.where("LENGTH(COALESCE(url, '')) = 0")
.destroy_all
# migrate uploads to new scheme
problems = Upload.migrate_to_new_scheme
problems.each do |hash|
upload_id = hash[:upload].id
Discourse.handle_job_exception(hash[:ex], error_context(args, "Migrating upload id #{upload_id}", upload_id: upload_id))
end
# clean up failed optimized images
OptimizedImage.where("LENGTH(COALESCE(url, '')) = 0").destroy_all
# Clean up orphan optimized images
OptimizedImage.where("upload_id NOT IN (SELECT id FROM uploads)").destroy_all
# migrate optimized_images to new scheme
problems = OptimizedImage.migrate_to_new_scheme
problems.each do |hash|
optimized_image_id = hash[:optimized_image].id
Discourse.handle_job_exception(hash[:ex], error_context(args, "Migrating optimized_image id #{optimized_image_id}", optimized_image_id: optimized_image_id))
end
end
rescue => e
puts e.message
puts e.backtrace.join("\n")
end
end
end
end