Skip to content

Commit

Permalink
Prep job for uploads ingest
Browse files Browse the repository at this point in the history
  • Loading branch information
jefftmarks committed Dec 26, 2024
1 parent 420516c commit 547d026
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 24 deletions.
6 changes: 2 additions & 4 deletions app/assets/javascripts/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,12 @@ $(function() {
processData: false,
contentType: false
});
if (uploadId === null) {
uploadId = response.upload_id;
};
console.log(response);
uploadId = response.upload_id;
} catch (error) {
console.error(error);
}
}
window.location.href = `/uploads/${uploadId}`;
};

generateMultipleFiles();
Expand Down
33 changes: 15 additions & 18 deletions app/controllers/uploads_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,22 @@ def create
end

# To avoid timeout, custom logic in upload.js breaks file into smaller blobs and uploads each blob
# separately via ajax to simulate multiple file upload.
# separately to simulate multiple file upload.
# Reassemble blob into single file and load data via async job.
def create_async
begin
async_params = upload_params[:async]
previous_upload = Upload.find_by(id: async_params[:upload_id])
previous_upload.update(upload_file: merged_params[:upload_file]) if previous_upload
# Create and update only one upload record to track multiple blob uploads
previous_upload = Upload.find_by(id: async_params[:upload_id])&.tap do |upload|
upload.update(upload_file: merged_params[:upload_file])
end
# Create and update only one upload record to track blob content across multiple uploads
@upload = previous_upload || Upload.create(merged_params)
@upload.create_or_update_blob
@upload.create_or_concat_blob

current_upload = async_params[:count][:current]
total_uploads = async_params[:count][:total]

if async_params[:count][:current] == async_params[:count][:total]
# Queue async load data job
end
respond_to do |format|
format.js do
if current_upload < total_uploads
render json: { upload_id: @upload.id }
else
render json: { upload_id: @upload.id }
# redirect_to @upload
end
end
format.js { render json: { upload_id: @upload.id } }
end
rescue StandardError => e
@upload = Upload.from_csv_type(merged_params[:csv_type])
Expand Down Expand Up @@ -119,7 +112,7 @@ def original_filename
end

def merged_params
upload_params.except(:async).merge(csv: original_filename, user: current_user)
@merged_params ||= upload_params.except(:async).merge(csv: original_filename, user: current_user)
end

def upload_params
Expand All @@ -133,6 +126,10 @@ def upload_params
@upload_params ||= upload_params
end

def async_params
@async_params ||= upload_params[:async]
end

def load_file
return unless @upload.persisted?

Expand Down
2 changes: 1 addition & 1 deletion app/models/upload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def async_upload
OpenStruct.new(upload_settings)
end

def create_or_update_blob
def create_or_concat_blob
upload_content = File.read(upload_file.tempfile)
# Append content to existing blob if it exists
update(blob: blob&.concat(upload_content) || upload_content)
Expand Down
6 changes: 5 additions & 1 deletion app/views/uploads/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@
<strong>Upload Status: </strong>
</div>
<div class="col-xs-3">
<%= @upload.ok? ? 'succeeded' : 'failed' %>
<% if @upload.ok? %>
succeeded
<% else %>
<%= @upload.queued_at ? 'pending' : 'failed' %>
<% end %>
</div>
</div>

Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20241226163620_add_queued_at_to_uploads.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddQueuedAtToUploads < ActiveRecord::Migration[7.1]
def change
add_column :uploads, :queued_at, :datetime, precision: nil
end
end

0 comments on commit 547d026

Please sign in to comment.