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:
Alex Kriuchykhin 2018-09-25 11:49:19 +02:00 committed by GitHub
commit 112343e65d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 23 deletions

View file

@ -39,7 +39,7 @@ class TeamsController < ApplicationController
) )
if @temp_file.save if @temp_file.save
@temp_file.destroy_obsolete TempFile.destroy_obsolete(@temp_file.id)
respond_to do |format| respond_to do |format|
format.json do format.json do
render json: { render json: {

View file

@ -221,48 +221,52 @@ class Asset < ApplicationRecord
Rails.logger.info "Asset #{id}: Creating extract text job" Rails.logger.info "Asset #{id}: Creating extract text job"
# The extract_asset_text also includes # The extract_asset_text also includes
# estimated size calculation # estimated size calculation
delay(queue: :assets, run_at: 20.minutes.from_now) Asset.delay(queue: :assets, run_at: 20.minutes.from_now)
.extract_asset_text(team) .extract_asset_text(id)
else else
# Update asset's estimated size immediately # Update asset's estimated size immediately
update_estimated_size(team) update_estimated_size(team)
end end
end end
def extract_asset_text(team = nil) def self.extract_asset_text(asset_id)
return if file.blank? asset = find_by_id(asset_id)
return unless asset.present? && asset.file.present?
begin begin
file_path = file.path file_path = asset.file.path
if file.is_stored_on_s3? if asset.file.is_stored_on_s3?
fa = file.fetch fa = asset.file.fetch
file_path = fa.path file_path = fa.path
end end
if (!Yomu.class_eval('@@server_pid')) unless Yomu.class_eval('@@server_pid')
Yomu.server(:text,nil) Yomu.server(:text, nil)
sleep(5) sleep(5)
end end
y = Yomu.new file_path y = Yomu.new file_path
text_data = y.text text_data = y.text
if asset_text_datum.present? if asset.asset_text_datum.present?
# Update existing text datum if it exists # Update existing text datum if it exists
asset_text_datum.update(data: text_data) asset.asset_text_datum.update(data: text_data)
else else
# Create new text datum # Create new text datum
AssetTextDatum.create(data: text_data, asset: self) AssetTextDatum.create(data: text_data, asset: asset)
end 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 # Finally, update asset's estimated size to include
# the data vector # the data vector
update_estimated_size(team) asset.update_estimated_size(asset.team)
rescue Exception => e rescue StandardError => e
Rails.logger.fatal "Asset #{id}: Error extracting contents from asset file #{file.path}: " + e.message Rails.logger.fatal(
"Asset #{asset.id}: Error extracting contents from asset "\
"file #{asset.file.path}: #{e.message}"
)
ensure ensure
File.delete file_path if fa File.delete file_path if fa
end end

View file

@ -4,10 +4,14 @@ class TempFile < ApplicationRecord
has_attached_file :file has_attached_file :file
do_not_validate_attachment_file_type :file do_not_validate_attachment_file_type :file
def destroy_obsolete class << self
destroy! if self def destroy_obsolete(temp_file_id)
end temp_file = find_by_id(temp_file_id)
return unless temp_file.present?
temp_file.destroy!
end
handle_asynchronously :destroy_obsolete, handle_asynchronously :destroy_obsolete,
run_at: proc { 7.days.from_now } run_at: proc { 7.days.from_now }
end
end end

View file

@ -36,7 +36,7 @@ module ImportRepository
) )
if temp_file.save if temp_file.save
temp_file.destroy_obsolete TempFile.destroy_obsolete(temp_file.id)
return temp_file return temp_file
end end
end end