Skip to content

Commit

Permalink
Trying to fix the progress bar for uploades (#469)
Browse files Browse the repository at this point in the history
  • Loading branch information
brand-it authored Oct 10, 2024
1 parent 29e0644 commit f3da076
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 23 deletions.
25 changes: 13 additions & 12 deletions app/components/upload_process_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,29 @@
<% end %>
<%= c.with_body do %>
<% if uploadable_video_blobs.any? || uploaded_recently_video_blobs.any? %>
<% uploaded_recently_video_blobs.each do |blob| %>
<% blobs = uploadable_video_blobs.map { [_1, find_job_by_video_blob(_1)] } %>
<% blobs.sort_by { _2.present? ? 1 : 2 }.each do |blob, video_blob_job| %>
<%=
render(
ProgressBarComponent.new(
show_percentage: false,
status: :success,
completed: 100,
message: "Uploaded #{blob.title}"
show_percentage: video_blob_job.present?,
status: :info,
completed: percentage(video_blob_job&.completed, blob.byte_size),
message: video_blob_job ? "Uploading #{blob.title}" : "Pending upload #{blob.title} ##{blob.id}",
eta: (eta(video_blob_job, blob) if video_blob_job)
)
)
%>
<% end %>
<% uploadable_video_blobs.each do |blob| %>

<% uploaded_recently_video_blobs.each do |blob| %>
<%=
video_blob_job = find_job_by_video_blob(blob)
render(
ProgressBarComponent.new(
show_percentage: video_blob_job.present?,
status: :info,
completed: percentage(video_blob_job&.completed, blob.byte_size),
message: video_blob_job ? "Uploading #{blob.title}" : "Pending upload #{blob.title}",
eta: (eta(video_blob_job, blob) if video_blob_job)
show_percentage: false,
status: :success,
completed: 100,
message: "Uploaded #{blob.title}"
)
)
%>
Expand Down
9 changes: 6 additions & 3 deletions app/components/upload_process_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ def dom_id
end

def uploadable_video_blobs
@uploadable_video_blobs ||= VideoBlob.uploadable
.order(updated_at: :desc)
@uploadable_video_blobs ||= VideoBlob.uploadable.order(updated_at: :desc)
end

def percentage(completed, total)
Expand Down Expand Up @@ -47,7 +46,11 @@ def ftp_host
def find_job_by_video_blob(blob)
return if blob.nil? || !job_active?

job if job.metadata['video_blob_id'].to_i == blob.id
jobs.find { _1.metadata['video_blob_id'].to_i == blob.id }
end

def jobs
Job.active.where(name: 'UploadWorker')
end

def eta(job, blob)
Expand Down
9 changes: 4 additions & 5 deletions app/listeners/upload_progress_listener.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ class UploadProgressListener

attr_reader :completed

def upload_progress(chunk_size: nil)
job.completed ||= 0
job.completed += chunk_size
def upload_progress(total_uploaded:)
job.completed = total_uploaded
return if next_update.future?

job.save!
Expand All @@ -27,14 +26,14 @@ def upload_ready
end

def upload_started
job.metadata['completed'] = 0
job.completed = 0
job.metadata['video_blob_id'] = video_blob.id
job.save!
update_component
end

def upload_finished
job.metadata['completed'] = video_blob.byte_size
job.completed = video_blob.byte_size
job.save!
video_blob.update!(uploadable: false, uploaded_on: Time.current)
update_component
Expand Down
4 changes: 3 additions & 1 deletion app/services/ftp/upload_mkv_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ def ftp_create_directory

def ftp_upload_file
broadcast(:upload_started)
total_uploaded = 0
ftp.putbinaryfile(file, video_blob.plex_path) do |chunk|
broadcast(:upload_progress, chunk_size: chunk.size)
total_uploaded += chunk.size
broadcast(:upload_progress, total_uploaded:)
end
end

Expand Down
2 changes: 1 addition & 1 deletion current_version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v5.4.0
v5.4.1
2 changes: 1 addition & 1 deletion spec/listeners/upload_progress_listener_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
subject(:listener) { described_class.new(**args) }

describe '#upload_progress' do
subject(:upload_progress) { listener.upload_progress(chunk_size: 10) }
subject(:upload_progress) { listener.upload_progress(total_uploaded: 10) }

let(:video_blob) { build_stubbed(:video_blob) }
let(:job) { build(:job) }
Expand Down

0 comments on commit f3da076

Please sign in to comment.