2019-09-24 03:07:03 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Provides asynchronous generation of image previews for ActiveStorage::Blob records.
|
|
|
|
class ActiveStorage::PreviewJob < ActiveStorage::BaseJob
|
|
|
|
queue_as :assets
|
|
|
|
|
|
|
|
discard_on StandardError do |job, error|
|
|
|
|
blob = ActiveStorage::Blob.find_by(id: job.arguments.first)
|
|
|
|
blob&.attachments&.take&.record&.update(file_processing: false)
|
|
|
|
Rails.logger.error "Couldn't generate preview for Blob with id: #{job.arguments.first}. Error:\n #{error}"
|
|
|
|
end
|
|
|
|
|
|
|
|
discard_on ActiveRecord::RecordNotFound
|
|
|
|
|
|
|
|
retry_on ActiveStorage::IntegrityError, attempts: 3, wait: :exponentially_longer
|
|
|
|
|
2019-10-10 20:17:02 +08:00
|
|
|
def perform(blob_id)
|
2019-09-24 03:07:03 +08:00
|
|
|
blob = ActiveStorage::Blob.find(blob_id)
|
2019-10-10 20:17:02 +08:00
|
|
|
preview = blob.representation(resize_to_limit: Constants::MEDIUM_PIC_FORMAT).processed
|
|
|
|
Rails.logger.info "Preview for the Blod with id: #{blob.id} - successfully generated.\n" \
|
|
|
|
"Transformations applied: #{preview.variation.transformations}"
|
2019-09-24 03:07:03 +08:00
|
|
|
|
2019-10-10 20:17:02 +08:00
|
|
|
preview = blob.representation(resize_to_limit: Constants::LARGE_PIC_FORMAT).processed
|
2019-09-24 03:07:03 +08:00
|
|
|
Rails.logger.info "Preview for the Blod with id: #{blob.id} - successfully generated.\n" \
|
|
|
|
"Transformations applied: #{preview.variation.transformations}"
|
2019-10-10 20:17:02 +08:00
|
|
|
|
|
|
|
blob.attachments.take.record.update(file_processing: false)
|
2019-09-24 03:07:03 +08:00
|
|
|
end
|
|
|
|
end
|