# frozen_string_literal: true

# Provides asynchronous generation of image previews for ActiveStorage::Blob records.
class PdfPreviewJob < ApplicationJob
  queue_as :assets

  discard_on StandardError do |job, error|
    asset = Asset.find_by(id: job.arguments.first)
    ActiveRecord::Base.no_touching do
      asset&.update(pdf_preview_processing: false)
    end
    Rails.logger.error("Couldn't generate PDF preview for Asset with id: #{job.arguments.first}. Error:\n #{error}")
  end

  discard_on ActiveRecord::RecordNotFound

  def perform(asset_id)
    asset = Asset.find(asset_id)

    PdfPreviewService.new(asset.file.blob, asset.file_pdf_preview).generate!
  ensure
    asset.update(pdf_preview_processing: false)
  end
end
