Skip to content

Commit

Permalink
Use redlock to prevent multiple soffices from running at once. Remove…
Browse files Browse the repository at this point in the history
… retry handler since it wasn't getting called
  • Loading branch information
bbpennel committed Nov 15, 2024
1 parent 72e9025 commit f46d938
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
7 changes: 0 additions & 7 deletions app/jobs/create_derivatives_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,6 @@
class CreateDerivativesJob < Hyrax::ApplicationJob
queue_as :derivatives

sidekiq_retry_in do |count, exception, jobhash|
case exception
when SofficeTimeoutError
:kill
end
end

# @param [FileSet] file_set
# @param [String] file_id identifier for a Hydra::PCDM::File
# @param [String, NilClass] filepath the cached file within the Hyrax.config.working_path
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
# frozen_string_literal: true
# [hyc-override] https://github.com/samvera/hydra-derivatives/blob/v3.8.0/lib/hydra/derivatives/processors/document.rb
class SofficeTimeoutError; end
class SofficeTimeoutError < StandardError; end

Hydra::Derivatives::Processors::Document.class_eval do
LOCK_KEY = "soffice:document_conversion"
LOCK_TIMEOUT = 6 * 60 * 1000
JOB_TIMEOUT_SECONDS = 30
LOCK_MANAGER = Redlock::Client.new([ENV['REDIS_URL']])

# [hyc-override] Trigger kill if soffice process takes too long, and throw a non-retry error if that happens
def self.encode(path, format, outdir, timeout = 30)
def self.encode(path, format, outdir, timeout = JOB_TIMEOUT_SECONDS)
command = "#{Hydra::Derivatives.libreoffice_path} --invisible --headless --convert-to #{format} --outdir #{outdir} #{Shellwords.escape(path)}"
pid = nil
begin
Expand Down Expand Up @@ -32,7 +37,13 @@ def self.encode(path, format, outdir, timeout = 30)
# TODO: file_suffix and options are passed from ShellBasedProcessor.process but are not needed.
# A refactor could simplify this.
def encode_file(_file_suffix, _options = {})
convert_to_format
LOCK_MANAGER.lock(LOCK_KEY, LOCK_TIMEOUT) do |locked|
if locked
convert_to_format
else
raise "Could not acquire lock for document conversion of #{source_path}"
end
end
ensure
FileUtils.rm_f(converted_file)
# [hyc-override] clean up the parent temp dir
Expand Down

0 comments on commit f46d938

Please sign in to comment.