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