2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-04-08 03:31:32 +08:00
|
|
|
module Jobs
|
|
|
|
|
2019-10-02 12:01:53 +08:00
|
|
|
class MigrateUploadScheme < ::Jobs::Scheduled
|
2016-09-02 11:32:51 +08:00
|
|
|
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)
|
2019-04-24 14:02:32 +08:00
|
|
|
return unless SiteSetting.migrate_to_new_scheme
|
2016-04-08 03:31:32 +08:00
|
|
|
|
|
|
|
# clean up failed uploads
|
|
|
|
Upload.where("created_at < ?", 1.hour.ago)
|
2017-07-28 09:20:09 +08:00
|
|
|
.where("LENGTH(COALESCE(url, '')) = 0")
|
2019-04-09 15:44:53 +08:00
|
|
|
.find_each do |upload|
|
|
|
|
|
|
|
|
upload.destroy!
|
|
|
|
end
|
2016-04-08 03:31:32 +08:00
|
|
|
|
|
|
|
# migrate uploads to new scheme
|
2019-04-24 11:56:48 +08:00
|
|
|
problems = Upload.migrate_to_new_scheme(limit: 50)
|
2019-04-03 07:38:57 +08:00
|
|
|
|
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
|
2019-04-09 15:44:53 +08:00
|
|
|
OptimizedImage
|
|
|
|
.where("LENGTH(COALESCE(url, '')) = 0")
|
|
|
|
.find_each do |optimized_image|
|
|
|
|
|
|
|
|
optimized_image.destroy!
|
|
|
|
end
|
|
|
|
|
2016-04-08 03:31:32 +08:00
|
|
|
# Clean up orphan optimized images
|
2019-04-09 15:44:53 +08: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-08 03:31:32 +08:00
|
|
|
|
2019-04-03 07:38:57 +08: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 14:37:34 +08:00
|
|
|
|
2019-04-03 07:38:57 +08:00
|
|
|
upload = optimized_image.upload
|
|
|
|
optimized_image.destroy!
|
|
|
|
upload.rebake_posts_on_old_scheme
|
2016-04-08 03:31:32 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|