2016-04-07 14:31:32 -05:00
|
|
|
module Jobs
|
|
|
|
|
2016-09-01 22:32:51 -05:00
|
|
|
class MigrateUploadScheme < Jobs::Scheduled
|
|
|
|
every 10.minutes
|
|
|
|
sidekiq_options retry: false
|
2016-04-07 14:31:32 -05:00
|
|
|
|
2016-09-01 22:32:51 -05:00
|
|
|
def execute(args)
|
2016-04-07 14:31:32 -05:00
|
|
|
return unless SiteSetting.migrate_to_new_scheme
|
|
|
|
|
|
|
|
# clean up failed uploads
|
|
|
|
Upload.where("created_at < ?", 1.hour.ago)
|
2017-07-27 20:20:09 -05:00
|
|
|
.where("LENGTH(COALESCE(url, '')) = 0")
|
2019-04-09 02:44:53 -05:00
|
|
|
.find_each do |upload|
|
|
|
|
|
|
|
|
upload.destroy!
|
|
|
|
end
|
2016-04-07 14:31:32 -05:00
|
|
|
|
|
|
|
# migrate uploads to new scheme
|
2016-09-01 22:32:51 -05:00
|
|
|
problems = Upload.migrate_to_new_scheme(50)
|
2019-04-02 18:38:57 -05:00
|
|
|
|
2016-04-07 14:31:32 -05: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
|
2019-04-09 02:44:53 -05:00
|
|
|
OptimizedImage
|
|
|
|
.where("LENGTH(COALESCE(url, '')) = 0")
|
|
|
|
.find_each do |optimized_image|
|
|
|
|
|
|
|
|
optimized_image.destroy!
|
|
|
|
end
|
|
|
|
|
2016-04-07 14:31:32 -05:00
|
|
|
# Clean up orphan optimized images
|
2019-04-09 02:44:53 -05:00
|
|
|
OptimizedImage
|
|
|
|
.joins("LEFT JOIN uploads ON optimized_images.upload_id = uploads.id")
|
|
|
|
.where("uploads.id IS NULL")
|
|
|
|
.find_each do |optimized_image|
|
|
|
|
|
|
|
|
optimized_image.destroy!
|
|
|
|
end
|
2016-04-07 14:31:32 -05:00
|
|
|
|
2019-04-02 18:38:57 -05:00
|
|
|
# Clean up optimized images that needs to be regenerated
|
|
|
|
OptimizedImage.joins(:upload)
|
|
|
|
.where("optimized_images.url NOT LIKE '%/optimized/_X/%'")
|
|
|
|
.where("uploads.url LIKE '%/original/_X/%'")
|
|
|
|
.limit(50)
|
|
|
|
.find_each do |optimized_image|
|
2019-03-26 01:37:34 -05:00
|
|
|
|
2019-04-02 18:38:57 -05:00
|
|
|
upload = optimized_image.upload
|
|
|
|
optimized_image.destroy!
|
|
|
|
upload.rebake_posts_on_old_scheme
|
2016-04-07 14:31:32 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|