Skip to content

Commit

Permalink
POC showing ActionCable working
Browse files Browse the repository at this point in the history
  • Loading branch information
gbp committed Oct 23, 2024
1 parent e258059 commit 74fe3d4
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 0 deletions.
5 changes: 5 additions & 0 deletions app/channels/job_status_channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class JobStatusChannel < ApplicationCable::Channel

Check warning on line 1 in app/channels/job_status_channel.rb

View workflow job for this annotation

GitHub Actions / build

[rubocop] reported by reviewdog 🐶 Missing top-level documentation comment for `class JobStatusChannel`. Raw Output: app/channels/job_status_channel.rb:1:1: C: Style/Documentation: Missing top-level documentation comment for `class JobStatusChannel`.
def subscribed
stream_from 'job_status_channel'
end
end
9 changes: 9 additions & 0 deletions app/controllers/jobs_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class JobsController < ApplicationController

Check warning on line 1 in app/controllers/jobs_controller.rb

View workflow job for this annotation

GitHub Actions / build

[rubocop] reported by reviewdog 🐶 Missing top-level documentation comment for `class JobsController`. Raw Output: app/controllers/jobs_controller.rb:1:1: C: Style/Documentation: Missing top-level documentation comment for `class JobsController`.
def index
end

def start
LongRunningJob.perform_later(current_user&.id)
redirect_to jobs_path
end
end
1 change: 1 addition & 0 deletions app/javascript/channels/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
// Import all the channels to be used by Action Cable
import "./job_status_channel"
13 changes: 13 additions & 0 deletions app/javascript/channels/job_status_channel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import consumer from "./consumer"

consumer.subscriptions.create("JobStatusChannel", {
received(data) {
const statusElement = document.getElementById("job-status")
const progressElement = document.getElementById("job-progress")
const resultElement = document.getElementById("job-result")

if (statusElement) statusElement.textContent = data.status
if (progressElement) progressElement.value = data.progress
if (data.result && resultElement) resultElement.textContent = data.result
}
})
30 changes: 30 additions & 0 deletions app/jobs/long_running_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class LongRunningJob < ApplicationJob

Check warning on line 1 in app/jobs/long_running_job.rb

View workflow job for this annotation

GitHub Actions / build

[rubocop] reported by reviewdog 🐶 Missing top-level documentation comment for `class LongRunningJob`. Raw Output: app/jobs/long_running_job.rb:1:1: C: Style/Documentation: Missing top-level documentation comment for `class LongRunningJob`.
queue_as :default

def perform(_user_id)
progress = 0

while progress < 99
sleep(rand(0.5..2.0))
progress += rand(5..15)
progress = [progress, 99].min

ActionCable.server.broadcast(
'job_status_channel',
{
status: 'Processing...',
progress: progress
}
)
end

ActionCable.server.broadcast(
'job_status_channel',
{
status: 'Completed',
progress: 100,
result: "Task completed at #{Time.current}"
}
)
end
end
15 changes: 15 additions & 0 deletions app/views/jobs/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<% content_for :javascript_head do %>
<%= javascript_importmap_tags('modern_application') %>
<% end %>

<div class="job-container">
<h1>Background Job Status</h1>

<div class="status-container">
<p>Status: <span id="job-status">Not started</span></p>
<progress id="job-progress" value="0" max="100"></progress>
<p>Result: <span id="job-result"></span></p>
</div>

<%= button_to "Start Job", start_jobs_path, method: :post, class: 'start-button', data: { turbo: true } %>
</div>
6 changes: 6 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -937,4 +937,10 @@ def matches?(request)
end
end
####

resources :jobs, only: [:index] do
collection do
post :start
end
end
end

0 comments on commit 74fe3d4

Please sign in to comment.