DEV: Housekeeping for CleanUpUploads job (#24361)

Followup to 9db8f00b3d, we
don't need this dead code any more. Also made some minor
improvements and comments.
This commit is contained in:
Martin Brennan 2023-11-20 09:50:09 +10:00 committed by GitHub
parent 96c5a6c9ca
commit 186e415e38
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 53 deletions

View File

@ -7,7 +7,7 @@ module Jobs
def execute(args) def execute(args)
grace_period = [SiteSetting.clean_orphan_uploads_grace_period_hours, 1].max grace_period = [SiteSetting.clean_orphan_uploads_grace_period_hours, 1].max
# always remove invalid upload records # Always remove invalid upload records regardless of clean_up_uploads setting.
Upload Upload
.by_users .by_users
.where( .where(
@ -19,21 +19,13 @@ module Jobs
return unless SiteSetting.clean_up_uploads? return unless SiteSetting.clean_up_uploads?
if c = last_cleanup # Do nothing if the last cleanup was run too recently.
return if (Time.zone.now.to_i - c) < (grace_period / 2).hours last_cleanup_timestamp = last_cleanup
if last_cleanup_timestamp.present? &&
(Time.zone.now.to_i - last_cleanup_timestamp) < (grace_period / 2).hours
return
end end
base_url =
(
if Discourse.store.internal?
Discourse.store.relative_base_url
else
Discourse.store.absolute_base_url
end
)
s3_hostname = URI.parse(base_url).hostname
s3_cdn_hostname = URI.parse(SiteSetting.Upload.s3_cdn_url || "").hostname
result = Upload.by_users result = Upload.by_users
Upload.unused_callbacks&.each { |handler| result = handler.call(result) } Upload.unused_callbacks&.each { |handler| result = handler.call(result) }
result = result =
@ -42,46 +34,16 @@ module Jobs
"uploads.retain_hours IS NULL OR uploads.created_at < current_timestamp - interval '1 hour' * uploads.retain_hours", "uploads.retain_hours IS NULL OR uploads.created_at < current_timestamp - interval '1 hour' * uploads.retain_hours",
) )
.where("uploads.created_at < ?", grace_period.hour.ago) .where("uploads.created_at < ?", grace_period.hour.ago)
# Don't remove any secure uploads.
.where("uploads.access_control_post_id IS NULL") .where("uploads.access_control_post_id IS NULL")
.joins("LEFT JOIN upload_references ON upload_references.upload_id = uploads.id") .joins("LEFT JOIN upload_references ON upload_references.upload_id = uploads.id")
# Don't remove any uploads linked to an UploadReference.
.where("upload_references.upload_id IS NULL") .where("upload_references.upload_id IS NULL")
.with_no_non_post_relations .with_no_non_post_relations
result.find_each do |upload| result.find_each do |upload|
next if Upload.in_use_callbacks&.any? { |callback| callback.call(upload) } next if Upload.in_use_callbacks&.any? { |callback| callback.call(upload) }
upload.sha1.present? ? upload.destroy : upload.delete
if upload.sha1.present?
# TODO: Remove this check after UploadReferences records were created
encoded_sha = Base62.encode(upload.sha1.hex)
if ReviewableQueuedPost
.pending
.where(
"payload->>'raw' LIKE ? OR payload->>'raw' LIKE ?",
"%#{upload.sha1}%",
"%#{encoded_sha}%",
)
.exists?
next
end
if Draft.where(
"data LIKE ? OR data LIKE ?",
"%#{upload.sha1}%",
"%#{encoded_sha}%",
).exists?
next
end
if UserProfile.where(
"bio_raw LIKE ? OR bio_raw LIKE ?",
"%#{upload.sha1}%",
"%#{encoded_sha}%",
).exists?
next
end
upload.destroy
else
upload.delete
end
end end
ExternalUploadStub.cleanup! ExternalUploadStub.cleanup!
@ -89,13 +51,13 @@ module Jobs
self.last_cleanup = Time.zone.now.to_i self.last_cleanup = Time.zone.now.to_i
end end
def last_cleanup=(v) def last_cleanup=(timestamp)
Discourse.redis.setex(last_cleanup_key, 7.days.to_i, v.to_s) Discourse.redis.setex(last_cleanup_key, 7.days.to_i, timestamp.to_s)
end end
def last_cleanup def last_cleanup
v = Discourse.redis.get(last_cleanup_key) timestamp = Discourse.redis.get(last_cleanup_key)
v ? v.to_i : v timestamp ? timestamp.to_i : timestamp
end end
def reset_last_cleanup! def reset_last_cleanup!

View File

@ -7,7 +7,7 @@ class ReviewableQueuedPost < Reviewable
end end
after_save do after_save do
if saved_change_to_payload? && self.status == Reviewable.statuses[:pending] && if saved_change_to_payload? && self.status.to_sym == :pending &&
self.payload&.[]("raw").present? self.payload&.[]("raw").present?
upload_ids = Upload.extract_upload_ids(self.payload["raw"]) upload_ids = Upload.extract_upload_ids(self.payload["raw"])
UploadReference.ensure_exist!(upload_ids: upload_ids, target: self) UploadReference.ensure_exist!(upload_ids: upload_ids, target: self)

View File

@ -12,6 +12,7 @@ Fabricator(:reviewable) do
category category
score 1.23 score 1.23
payload { { list: [1, 2, 3], name: "bandersnatch" } } payload { { list: [1, 2, 3], name: "bandersnatch" } }
status { :pending }
end end
Fabricator(:reviewable_queued_post_topic, class_name: :reviewable_queued_post) do Fabricator(:reviewable_queued_post_topic, class_name: :reviewable_queued_post) do

View File

@ -289,6 +289,7 @@ RSpec.describe Jobs::CleanUpUploads do
payload: { payload: {
raw: "#{upload.short_url}\n#{upload2.short_url}", raw: "#{upload.short_url}\n#{upload2.short_url}",
}, },
status: :pending,
) )
Fabricate( Fabricate(
@ -296,7 +297,7 @@ RSpec.describe Jobs::CleanUpUploads do
payload: { payload: {
raw: "#{upload3.short_url}", raw: "#{upload3.short_url}",
}, },
status: Reviewable.statuses[:rejected], status: :rejected,
) )
Jobs::CleanUpUploads.new.execute(nil) Jobs::CleanUpUploads.new.execute(nil)