DEV: Improve external upload debugging (#28627)
* Do not delete created external upload stubs for 2 days instead of 1 hour if enable_upload_debug_mode is true, this aids with server-side debugging. * If using an API call, return the detailed error message if enable_upload_debug_mode is true. In this case the user is not using the UI, so a more detailed message is appropriate. * Add a prefix to log messages in ExternalUploadHelpers, to make it easier to find these in logster.
This commit is contained in:
parent
a874ac71bc
commit
daa06a1c00
|
@ -21,7 +21,7 @@ class ExternalUploadStub < ActiveRecord::Base
|
||||||
where(
|
where(
|
||||||
"status = ? AND created_at <= ?",
|
"status = ? AND created_at <= ?",
|
||||||
ExternalUploadStub.statuses[:created],
|
ExternalUploadStub.statuses[:created],
|
||||||
CREATED_EXPIRY_HOURS.hours.ago,
|
(SiteSetting.enable_upload_debug_mode ? 48 : CREATED_EXPIRY_HOURS).hours.ago,
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -136,7 +136,8 @@ module ExternalUploadHelpers
|
||||||
ExternalUploadStub.find_by(unique_identifier: unique_identifier, created_by: current_user)
|
ExternalUploadStub.find_by(unique_identifier: unique_identifier, created_by: current_user)
|
||||||
return render_404 if external_upload_stub.blank?
|
return render_404 if external_upload_stub.blank?
|
||||||
|
|
||||||
return render_404 if !multipart_upload_exists?(external_upload_stub)
|
message = check_multipart_upload_exists(external_upload_stub)
|
||||||
|
return render_404(message) if message.present?
|
||||||
|
|
||||||
store = multipart_store(external_upload_stub.upload_type)
|
store = multipart_store(external_upload_stub.upload_type)
|
||||||
|
|
||||||
|
@ -152,7 +153,7 @@ module ExternalUploadHelpers
|
||||||
render json: { presigned_urls: presigned_urls }
|
render json: { presigned_urls: presigned_urls }
|
||||||
end
|
end
|
||||||
|
|
||||||
def multipart_upload_exists?(external_upload_stub)
|
def check_multipart_upload_exists(external_upload_stub)
|
||||||
store = multipart_store(external_upload_stub.upload_type)
|
store = multipart_store(external_upload_stub.upload_type)
|
||||||
begin
|
begin
|
||||||
store.list_multipart_parts(
|
store.list_multipart_parts(
|
||||||
|
@ -161,16 +162,20 @@ module ExternalUploadHelpers
|
||||||
max_parts: 1,
|
max_parts: 1,
|
||||||
)
|
)
|
||||||
rescue Aws::S3::Errors::NoSuchUpload => err
|
rescue Aws::S3::Errors::NoSuchUpload => err
|
||||||
debug_upload_error(
|
return(
|
||||||
err,
|
debug_upload_error(
|
||||||
I18n.t(
|
err,
|
||||||
"upload.external_upload_not_found",
|
I18n.t(
|
||||||
additional_detail: "path: #{external_upload_stub.key}",
|
"upload.external_upload_not_found",
|
||||||
),
|
additional_detail: "path: #{external_upload_stub.key}",
|
||||||
|
),
|
||||||
|
)
|
||||||
)
|
)
|
||||||
return false
|
|
||||||
end
|
end
|
||||||
true
|
|
||||||
|
# Intentional to indicate that there is no error, if there is a message
|
||||||
|
# then there is an error.
|
||||||
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
def abort_multipart
|
def abort_multipart
|
||||||
|
@ -226,7 +231,8 @@ module ExternalUploadHelpers
|
||||||
ExternalUploadStub.find_by(unique_identifier: unique_identifier, created_by: current_user)
|
ExternalUploadStub.find_by(unique_identifier: unique_identifier, created_by: current_user)
|
||||||
return render_404 if external_upload_stub.blank?
|
return render_404 if external_upload_stub.blank?
|
||||||
|
|
||||||
return render_404 if !multipart_upload_exists?(external_upload_stub)
|
message = check_multipart_upload_exists(external_upload_stub)
|
||||||
|
return render_404(message) if message.present?
|
||||||
|
|
||||||
store = multipart_store(external_upload_stub.upload_type)
|
store = multipart_store(external_upload_stub.upload_type)
|
||||||
parts =
|
parts =
|
||||||
|
@ -353,9 +359,14 @@ module ExternalUploadHelpers
|
||||||
end
|
end
|
||||||
|
|
||||||
def debug_upload_error(err, friendly_message)
|
def debug_upload_error(err, friendly_message)
|
||||||
return if !SiteSetting.enable_upload_debug_mode
|
return I18n.t("upload.failed") if !SiteSetting.enable_upload_debug_mode
|
||||||
Discourse.warn_exception(err, message: friendly_message)
|
Discourse.warn_exception(err, message: "[ExternalUploadError] #{friendly_message}")
|
||||||
(Rails.env.development? || Rails.env.test?) ? friendly_message : I18n.t("upload.failed")
|
|
||||||
|
if Rails.env.local? || is_api?
|
||||||
|
friendly_message
|
||||||
|
else
|
||||||
|
I18n.t("upload.failed")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def multipart_store(upload_type)
|
def multipart_store(upload_type)
|
||||||
|
@ -385,7 +396,11 @@ module ExternalUploadHelpers
|
||||||
metadata.permit("sha1-checksum").to_h
|
metadata.permit("sha1-checksum").to_h
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_404
|
def render_404(message = nil)
|
||||||
raise Discourse::NotFound
|
if message
|
||||||
|
render_json_error(message, status: 404)
|
||||||
|
else
|
||||||
|
raise Discourse::NotFound
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -404,4 +404,20 @@ RSpec.describe Jobs::CleanUpUploads do
|
||||||
Jobs::CleanUpUploads.new.execute(nil)
|
Jobs::CleanUpUploads.new.execute(nil)
|
||||||
expect(ExternalUploadStub.pluck(:id)).to contain_exactly(external_stub1.id, external_stub3.id)
|
expect(ExternalUploadStub.pluck(:id)).to contain_exactly(external_stub1.id, external_stub3.id)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "does not delete create external upload stubs for 2 days if debug mode is on" do
|
||||||
|
SiteSetting.enable_upload_debug_mode = true
|
||||||
|
external_stub1 =
|
||||||
|
Fabricate(
|
||||||
|
:external_upload_stub,
|
||||||
|
status: ExternalUploadStub.statuses[:created],
|
||||||
|
created_at: 2.hours.ago,
|
||||||
|
)
|
||||||
|
Jobs::CleanUpUploads.new.execute(nil)
|
||||||
|
expect(ExternalUploadStub.pluck(:id)).to contain_exactly(external_stub1.id)
|
||||||
|
|
||||||
|
SiteSetting.enable_upload_debug_mode = false
|
||||||
|
Jobs::CleanUpUploads.new.execute(nil)
|
||||||
|
expect(ExternalUploadStub.pluck(:id)).to be_empty
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue