Revert "Timeout slow libreoffice preview generation [SCI-10809] (#7678)"

This reverts commit 1265f29a9e.
This commit is contained in:
Martin Artnik 2024-08-06 10:58:50 +02:00 committed by GitHub
parent 83eee9c72b
commit b6af1031f5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 67 additions and 65 deletions

View file

@ -16,9 +16,39 @@ class PdfPreviewJob < ApplicationJob
def perform(asset_id)
asset = Asset.find(asset_id)
blob = asset.file.blob
blob.open(tmpdir: tempdir) do |input|
work_dir = File.dirname(input.path)
preview_filename = "#{File.basename(input.path, '.*')}.pdf"
preview_file = File.join(work_dir, preview_filename)
Rails.logger.info "Starting preparing document preview for file #{blob.filename.sanitized}..."
PdfPreviewService.new(asset.file.blob, asset.file_pdf_preview).generate!
ensure
asset.update(pdf_preview_processing: false)
ActiveRecord::Base.transaction do
success = system(
libreoffice_path, '--headless', '--invisible', '--convert-to', 'pdf', '--outdir', work_dir, input.path
)
unless success && File.file?(preview_file)
raise StandardError, "There was an error generating PDF preview, blob id: #{blob.id}"
end
ActiveRecord::Base.no_touching do
asset.file_pdf_preview.attach(io: File.open(preview_file), filename: "#{blob.filename.base}.pdf")
asset.update(pdf_preview_processing: false)
end
Rails.logger.info("Finished preparing PDF preview for file #{blob.filename.sanitized}.")
end
ensure
File.delete(preview_file) if File.file?(preview_file)
end
end
private
def tempdir
Rails.root.join('tmp')
end
def libreoffice_path
ENV['LIBREOFFICE_PATH'] || 'soffice'
end
end

View file

@ -15,7 +15,39 @@ module Reports
def perform(report_id)
report = Report.find(report_id)
PdfPreviewService.new(report.docx_file.blob, report.docx_preview_file).generate!
blob = report.docx_file.blob
blob.open(tmpdir: tempdir) do |input|
work_dir = File.dirname(input.path)
preview_filename = "#{File.basename(input.path, '.*')}.pdf"
preview_file = File.join(work_dir, preview_filename)
Rails.logger.info "Starting preparing document preview for file #{blob.filename.sanitized}..."
ActiveRecord::Base.transaction do
success = system(
libreoffice_path, '--headless', '--invisible', '--convert-to', 'pdf', '--outdir', work_dir, input.path
)
unless success && File.file?(preview_file)
raise StandardError, "There was an error generating PDF preview, blob id: #{blob.id}"
end
ActiveRecord::Base.no_touching do
report.docx_preview_file.attach(io: File.open(preview_file), filename: "#{blob.filename.base}.pdf")
end
Rails.logger.info("Finished preparing PDF preview for file #{blob.filename.sanitized}.")
end
ensure
File.delete(preview_file) if File.file?(preview_file)
end
end
private
def tempdir
Rails.root.join('tmp')
end
def libreoffice_path
ENV['LIBREOFFICE_PATH'] || 'soffice'
end
end
end

View file

@ -1,52 +0,0 @@
# frozen_string_literal: true
class PdfPreviewService
def initialize(blob, attached)
@blob = blob
@attached = attached
end
def generate!
preview_file = nil
@blob.open(tmpdir: tempdir) do |input|
work_dir = File.dirname(input.path)
preview_filename = "#{File.basename(input.path, '.*')}.pdf"
preview_file = File.join(work_dir, preview_filename)
Rails.logger.info "Starting preparing document preview for file #{@blob.filename.sanitized}..."
ActiveRecord::Base.transaction do
success = system(
'timeout',
Constants::PREVIEW_TIMEOUT_SECONDS.to_s,
libreoffice_path,
'--headless',
'--invisible',
'--convert-to',
'pdf', '--outdir',
work_dir, input.path
)
unless success && File.file?(preview_file)
raise StandardError, "There was an error generating PDF preview, blob id: #{@blob.id}"
end
ActiveRecord::Base.no_touching do
attached.attach(io: File.open(preview_file), filename: "#{@blob.filename.base}.pdf")
end
Rails.logger.info("Finished preparing PDF preview for file #{@blob.filename.sanitized}.")
end
end
ensure
File.delete(preview_file) if File.file?(preview_file)
end
private
def tempdir
Rails.root.join('tmp')
end
def libreoffice_path
ENV['LIBREOFFICE_PATH'] || 'soffice'
end
end

View file

@ -328,9 +328,6 @@ class Constants
PREVIEWABLE_FILE_TYPES = TEXT_EXTRACT_FILE_TYPES
# default preview timeout to 15 minutes
PREVIEW_TIMEOUT_SECONDS = ENV['PREVIEW_TIMEOUT_SECONDS'] ? ENV['PREVIEW_TIMEOUT_SECONDS'].to_i : 900
WHITELISTED_IMAGE_TYPES = [
'gif', 'jpeg', 'pjpeg', 'png', 'x-png', 'svg+xml', 'bmp', 'tiff', 'jpg'
].freeze

View file

@ -22,12 +22,7 @@ module ActiveStorage
begin
success = system(
'timeout',
Constants::PREVIEW_TIMEOUT_SECONDS.to_s,
libreoffice_path,
'--headless', '--invisible', '--convert-to', 'png', '--outdir',
work_dir,
input.path
libreoffice_path, '--headless', '--invisible', '--convert-to', 'png', '--outdir', work_dir, input.path
)
unless success && File.file?(preview_file)