mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2024-12-29 03:35:25 +08:00
Merge pull request #1311 from okriuchykhin/ok_SCI_2738
Improve delayed jobs for TempFile deletion and Asset text extraction [SCI-2738]
This commit is contained in:
commit
112343e65d
4 changed files with 31 additions and 23 deletions
|
@ -39,7 +39,7 @@ class TeamsController < ApplicationController
|
|||
)
|
||||
|
||||
if @temp_file.save
|
||||
@temp_file.destroy_obsolete
|
||||
TempFile.destroy_obsolete(@temp_file.id)
|
||||
respond_to do |format|
|
||||
format.json do
|
||||
render json: {
|
||||
|
|
|
@ -221,48 +221,52 @@ class Asset < ApplicationRecord
|
|||
Rails.logger.info "Asset #{id}: Creating extract text job"
|
||||
# The extract_asset_text also includes
|
||||
# estimated size calculation
|
||||
delay(queue: :assets, run_at: 20.minutes.from_now)
|
||||
.extract_asset_text(team)
|
||||
Asset.delay(queue: :assets, run_at: 20.minutes.from_now)
|
||||
.extract_asset_text(id)
|
||||
else
|
||||
# Update asset's estimated size immediately
|
||||
update_estimated_size(team)
|
||||
end
|
||||
end
|
||||
|
||||
def extract_asset_text(team = nil)
|
||||
return if file.blank?
|
||||
def self.extract_asset_text(asset_id)
|
||||
asset = find_by_id(asset_id)
|
||||
return unless asset.present? && asset.file.present?
|
||||
|
||||
begin
|
||||
file_path = file.path
|
||||
file_path = asset.file.path
|
||||
|
||||
if file.is_stored_on_s3?
|
||||
fa = file.fetch
|
||||
if asset.file.is_stored_on_s3?
|
||||
fa = asset.file.fetch
|
||||
file_path = fa.path
|
||||
end
|
||||
|
||||
if (!Yomu.class_eval('@@server_pid'))
|
||||
Yomu.server(:text,nil)
|
||||
unless Yomu.class_eval('@@server_pid')
|
||||
Yomu.server(:text, nil)
|
||||
sleep(5)
|
||||
end
|
||||
y = Yomu.new file_path
|
||||
|
||||
text_data = y.text
|
||||
|
||||
if asset_text_datum.present?
|
||||
if asset.asset_text_datum.present?
|
||||
# Update existing text datum if it exists
|
||||
asset_text_datum.update(data: text_data)
|
||||
asset.asset_text_datum.update(data: text_data)
|
||||
else
|
||||
# Create new text datum
|
||||
AssetTextDatum.create(data: text_data, asset: self)
|
||||
AssetTextDatum.create(data: text_data, asset: asset)
|
||||
end
|
||||
|
||||
Rails.logger.info "Asset #{id}: Asset file successfully extracted"
|
||||
Rails.logger.info "Asset #{asset.id}: Asset file successfully extracted"
|
||||
|
||||
# Finally, update asset's estimated size to include
|
||||
# the data vector
|
||||
update_estimated_size(team)
|
||||
rescue Exception => e
|
||||
Rails.logger.fatal "Asset #{id}: Error extracting contents from asset file #{file.path}: " + e.message
|
||||
asset.update_estimated_size(asset.team)
|
||||
rescue StandardError => e
|
||||
Rails.logger.fatal(
|
||||
"Asset #{asset.id}: Error extracting contents from asset "\
|
||||
"file #{asset.file.path}: #{e.message}"
|
||||
)
|
||||
ensure
|
||||
File.delete file_path if fa
|
||||
end
|
||||
|
|
|
@ -4,10 +4,14 @@ class TempFile < ApplicationRecord
|
|||
has_attached_file :file
|
||||
do_not_validate_attachment_file_type :file
|
||||
|
||||
def destroy_obsolete
|
||||
destroy! if self
|
||||
end
|
||||
class << self
|
||||
def destroy_obsolete(temp_file_id)
|
||||
temp_file = find_by_id(temp_file_id)
|
||||
return unless temp_file.present?
|
||||
temp_file.destroy!
|
||||
end
|
||||
|
||||
handle_asynchronously :destroy_obsolete,
|
||||
run_at: proc { 7.days.from_now }
|
||||
handle_asynchronously :destroy_obsolete,
|
||||
run_at: proc { 7.days.from_now }
|
||||
end
|
||||
end
|
||||
|
|
|
@ -36,7 +36,7 @@ module ImportRepository
|
|||
)
|
||||
|
||||
if temp_file.save
|
||||
temp_file.destroy_obsolete
|
||||
TempFile.destroy_obsolete(temp_file.id)
|
||||
return temp_file
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue