2016-04-08 03:31:32 +08:00
|
|
|
module Jobs
|
|
|
|
|
2016-09-02 11:32:51 +08:00
|
|
|
class MigrateUploadScheme < Jobs::Scheduled
|
|
|
|
every 10.minutes
|
|
|
|
sidekiq_options retry: false
|
2016-04-08 03:31:32 +08:00
|
|
|
|
2016-09-02 11:32:51 +08:00
|
|
|
def execute(args)
|
2016-04-08 03:31:32 +08:00
|
|
|
return unless SiteSetting.migrate_to_new_scheme
|
|
|
|
|
|
|
|
# clean up failed uploads
|
|
|
|
Upload.where("created_at < ?", 1.hour.ago)
|
2017-07-28 09:20:09 +08:00
|
|
|
.where("LENGTH(COALESCE(url, '')) = 0")
|
|
|
|
.destroy_all
|
2016-04-08 03:31:32 +08:00
|
|
|
|
|
|
|
# migrate uploads to new scheme
|
2016-09-02 11:32:51 +08:00
|
|
|
problems = Upload.migrate_to_new_scheme(50)
|
2016-04-08 03:31:32 +08:00
|
|
|
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
|
2016-09-02 11:32:51 +08:00
|
|
|
problems = OptimizedImage.migrate_to_new_scheme(50)
|
2019-03-26 14:37:34 +08:00
|
|
|
|
2016-04-08 03:31:32 +08:00
|
|
|
problems.each do |hash|
|
2019-03-26 14:37:34 +08:00
|
|
|
image = OptimizedImage.find_by(id: hash[:optimized_image].id)
|
|
|
|
image.destroy! if image
|
2016-04-08 03:31:32 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|