Skip to content

Commit

Permalink
Progress bar is now present bottom right of the page, this will be wh…
Browse files Browse the repository at this point in the history
…at is used for all background tasks
  • Loading branch information
brand-it committed Apr 12, 2024
1 parent 950e8b7 commit f9bbea2
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 29 deletions.
6 changes: 5 additions & 1 deletion app/components/progress_bar_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
</div>
</div>
<div>
<%= message.presence || model.model_name.singular %> ... <%= number_to_percentage completed, precision: 2 %>
<%= message.presence || model.model_name.singular %>
<% if show_percentage %>
...
<%= number_to_percentage completed, precision: 2 %>
<% end %>
</div>
</div>
1 change: 1 addition & 0 deletions app/components/progress_bar_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class ProgressBarComponent < ViewComponent::Base
option :completed, Types::Coercible::Float, default: -> { 0.0 }, null: nil
option :status, default: -> { 'info' }, null: nil
option :message, optional: true
option :show_percentage, default: -> { true }

def dom_id
"#{model.model_name.name}-progress-bar"
Expand Down
5 changes: 4 additions & 1 deletion app/controllers/the_movie_dbs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ def next_page
private

def synced_recently?
Video.maximum(:synced_on) < 5.minutes.ago
last_sync = Video.maximum(:synced_on)
return false if last_sync.nil?

last_sync + 5.minutes > Time.zone.now
end

def search_service
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import 'regenerator-runtime/runtime'

require("@rails/ujs").start();
require("@rails/activestorage").start();
require("./channels");
require("@popperjs/core");
require("bootstrap");
import '@fortawesome/fontawesome-free/js/all.js';
Expand All @@ -23,3 +22,4 @@ import '@fortawesome/fontawesome-free/js/all.js';
import '@hotwired/turbo-rails';

import "./controllers"
import './channels'
6 changes: 6 additions & 0 deletions app/javascript/channels/disk_channel.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import CableReady from 'cable_ready'
import consumer from './consumer'

console.log("disk_channel.js")

consumer.subscriptions.create('DiskTitleChannel', {
// print out a connection established
connected(data) {
console.log("DiskTitleChannel" + data)
},
received(data) {
if (data.cableReady) CableReady.perform(data.operations)
}
Expand Down
1 change: 1 addition & 0 deletions app/javascript/channels/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
// Channel files must be named *_channel.js.

const channels = require.context('.', true, /_channel\.js$/)
console.log("channels", channels.keys())
channels.keys().forEach(channels)
13 changes: 11 additions & 2 deletions app/views/layouts/application.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,17 @@
<%= render ProcessComponent.new worker: ScanPlexWorker do |c| %>
<%= c.with_body do %>
<% if ScanPlexWorker.job.pending? %>
<div class="spinner-border text-primary" role="status"></div>
<span>Scanning...</span>
<%=
render(
ProgressBarComponent.new(
model: Movie,
completed: (ScanPlexWorker.job.worker.completed.zero? ? 20 : ScanPlexWorker.job.worker.completed),
status: :success,
message: 'scanning plex for movies',
show_percentage: false
)
)
%>
<% else %>
<span>Done! you have a total of <%= pluralize(Video.count, 'video') %> on plex.</span>
<% end %>
Expand Down
57 changes: 33 additions & 24 deletions app/workers/scan_plex_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@

class ScanPlexWorker < ApplicationWorker
def perform
broadcast_progress(in_progress_component('Scan Plex...', 50, show_percentage: false))
plex_movies.map do |blob|
blob.update!(video: create_movie!(blob))
job.log("Updated #{blob.filename}")
self.completed += 1
broadcast_progress(in_progress_component(blob&.video&.plex_name))
percent_completed = (completed / plex_movies.size.to_f * 100)
broadcast_progress(
in_progress_component('Updating Database...', percent_completed)
)
end
broadcast_progress(completed_component)
end

def completed
@completed ||= 0
end

private

def broadcast_progress(component)
Expand All @@ -21,27 +29,32 @@ def broadcast_progress(component)
end

def completed_component
ProcessComponent.new worker: ScanPlexWorker do |c|
c.with_body do
ProgressBarComponent.new \
model: Movie,
completed: 100,
status: :success,
message: 'Plex scan complete!'
end
end
progress_bar = render(
ProgressBarComponent.new(
model: Movie,
completed: 100,
status: :success,
message: 'Plex scan complete!'
), layout: false
)
component = ProcessComponent.new(worker: ScanPlexWorker)
component.with_body { progress_bar }
component
end

def in_progress_component(message)
ProcessComponent.new worker: ScanPlexWorker do |c|
c.with_body do
ProgressBarComponent.new \
model: Movie,
completed: (completed / plex_movies.size.to_f * 100),
status: :info,
message: message || 'Scanning Plex...'
end
end
def in_progress_component(message, completed, show_percentage: true)
progress_bar = render(
ProgressBarComponent.new(
model: Movie,
completed:,
status: :info,
message: message || 'Scanning Plex...',
show_percentage:
), layout: false
)
component = ProcessComponent.new(worker: ScanPlexWorker)
component.with_body { progress_bar }
component
end

def search_for_movie(blob) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
Expand All @@ -68,9 +81,5 @@ def plex_movies
@plex_movies ||= Ftp::VideoScannerService.call.movies
end

def completed
@completed ||= 0
end

attr_writer :completed
end

0 comments on commit f9bbea2

Please sign in to comment.