add retry on failure for pdf extraction

This commit is contained in:
Tanmay Deep Sharma
2025-07-08 11:55:07 +07:00
parent 2663ad8e56
commit 6400585bd7
4 changed files with 4 additions and 68 deletions
@@ -1,4 +1,7 @@
class Captain::Documents::PdfExtractionJob < ApplicationJob
retry_on ActiveStorage::FileNotFoundError, attempts: 3, wait: 2.seconds, on: :perform
retry_on Faraday::BadRequestError, attempts: 3, wait: 2.seconds, on: :perform
queue_as :low
def perform(document)
@@ -7,8 +10,6 @@ class Captain::Documents::PdfExtractionJob < ApplicationJob
initialize_document_processing(document)
result = extract_pdf_content(document)
process_extraction_result(document, result)
rescue Captain::Tools::PdfExtractionService::ExtractionError => e
handle_pdf_extraction_error(document, e)
end
private
@@ -7,7 +7,6 @@ class Captain::Tools::PdfExtractionService
include Captain::Tools::PdfExtractionService::SourceHandler
include Captain::Tools::PdfExtractionService::BlobHandler
include Captain::Tools::PdfExtractionService::TextProcessor
include Captain::Tools::PdfExtractionService::ErrorHandler
class ExtractionError < StandardError; end
@@ -29,7 +28,7 @@ class Captain::Tools::PdfExtractionService
validation_result = validate_pdf_source
return validation_result unless validation_result[:success]
extract_pdf_with_error_handling
process_pdf_extraction
end
private
@@ -1,44 +0,0 @@
module Captain::Tools::PdfExtractionService::ErrorHandler
private
def extract_pdf_with_error_handling
process_pdf_extraction
rescue PDF::Reader::MalformedPDFError => e
handle_malformed_pdf_error(e)
rescue PDF::Reader::UnsupportedFeatureError => e
handle_unsupported_feature_error(e)
rescue Down::TimeoutError => e
handle_timeout_error(e)
rescue StandardError => e
handle_general_error(e)
end
def handle_malformed_pdf_error(error)
Rails.logger.error "Malformed PDF (#{pdf_source_type}): #{error.message}"
failure_response(['Invalid or corrupted PDF format'])
end
def handle_unsupported_feature_error(error)
Rails.logger.error "Unsupported PDF feature (#{pdf_source_type}): #{error.message}"
failure_response(['PDF contains unsupported features'])
end
def handle_timeout_error(error)
Rails.logger.error "PDF download timeout (#{pdf_source_type}): #{error.message}"
failure_response(['PDF download timed out'])
end
def handle_general_error(error)
Rails.logger.error "PDF extraction error (#{pdf_source_type}): #{error.message}"
Rails.logger.error error.backtrace.join("\n")
failure_response(['Failed to process PDF'])
end
def success_response(content)
{ success: true, content: content }
end
def failure_response(errors)
{ success: false, errors: errors }
end
end
@@ -20,27 +20,7 @@ module Captain::Tools::PdfValidationConcern
end
def validate_url_format
uri = parse_and_validate_uri
validate_url_scheme(uri)
validate_url_length
end
def parse_and_validate_uri
URI.parse(pdf_source)
rescue URI::InvalidURIError
raise StandardError, 'Malformed URL'
end
def validate_url_scheme(uri)
return if %w[http https].include?(uri.scheme)
raise StandardError, 'Invalid URL scheme'
end
def validate_url_length
return if pdf_source.length <= 2000
raise StandardError, 'URL too long'
end
def validate_file_type_and_size