Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ActionCable #8433

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
/cache/
/config/*.deployed
/config/aliases
/config/cable.yml
/config/config.tmp
/config/crontab
/config/database.yml
Expand Down
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ gem 'matrix', '~> 0.4.2'
gem 'mini_magick', '~> 4.13.1'
gem 'net-protocol', '~> 0.1.3'
gem 'redcarpet', '~> 3.6.0'
gem 'redis', '~> 4.8.1'
gem 'rolify', '~> 6.0.1'
gem 'ruby-msg', '~> 1.5.0', git: 'https://github.com/mysociety/ruby-msg.git', branch: 'ascii-encoding'
gem 'rubyzip', '~> 2.3.2'
Expand Down
1 change: 0 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,6 @@ DEPENDENCIES
rails-i18n (~> 7.0.5)
recaptcha (~> 5.17.0)
redcarpet (~> 3.6.0)
redis (~> 4.8.1)
rolify (~> 6.0.1)
rspec-activemodel-mocks (~> 1.2.1)
rspec-rails (~> 7.0.1)
Expand Down
4 changes: 4 additions & 0 deletions app/channels/application_cable/channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module ApplicationCable
class Channel < ActionCable::Channel::Base
end
end
4 changes: 4 additions & 0 deletions app/channels/application_cable/connection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module ApplicationCable
class Connection < ActionCable::Connection::Base
end
end
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
2 changes: 2 additions & 0 deletions app/javascript/channels/consumer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { createConsumer } from "@rails/actioncable"
export default createConsumer()
2 changes: 2 additions & 0 deletions app/javascript/channels/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +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
}
})
1 change: 1 addition & 0 deletions app/javascript/modern_application.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails
import "@hotwired/turbo-rails";
import "channels";
import "controllers";

// Disable old jQuery UJS event handling, allowing Turbo to handle instead
Expand Down
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>
2 changes: 1 addition & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# require "action_mailbox/engine"
require "action_text/engine"
require "action_view/railtie"
# require "action_cable/engine"
require "action_cable/engine"
require "sprockets/railtie"
# require "rails/test_unit/railtie"

Expand Down
12 changes: 12 additions & 0 deletions config/cable.yml-example
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
development:
adapter: redis
url: <%= ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" } %>
channel_prefix: alaveteli_development

test:
adapter: test

production:
adapter: redis
url: <%= ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" } %>
channel_prefix: alaveteli_production
1 change: 1 addition & 0 deletions config/general.yml-example
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,7 @@ SHARED_FILES_PATH: ''
#
# ---
SHARED_FILES:
- config/cable.yml
- config/database.yml
- config/general.yml
- config/sidekiq.yml
Expand Down
2 changes: 2 additions & 0 deletions config/importmap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
pin "@hotwired/stimulus", to: "stimulus.min.js"
pin "@hotwired/stimulus-loading", to: "stimulus-loading.js"
pin "@hotwired/turbo-rails", to: "turbo.min.js"
pin "@rails/actioncable", to: "actioncable.esm.js"
pin "sortablejs" # @1.15.2

pin "modern_application"
pin_all_from "app/javascript/channels", under: "channels"
pin_all_from "app/javascript/controllers", under: "controllers"
pin_all_from "app/javascript/helpers", under: "helpers"
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
1 change: 1 addition & 0 deletions docker/bootstrap
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ fi
success_msg 'done'

notice_msg "Copying example files..."
[ ! -f config/cable.yml ] && cp config/cable.yml-example config/cable.yml
[ ! -f config/database.yml ] && cp config/database.yml-example config/database.yml
[ ! -f config/sidekiq.yml ] && cp config/sidekiq.yml-example config/sidekiq.yml
[ ! -f config/storage.yml ] && cp config/storage.yml-example config/storage.yml
Expand Down
3 changes: 3 additions & 0 deletions script/install-as-user
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ do
fi
done

# add cable.yml
cp config/cable.yml-example config/cable.yml

# add sidekiq.yml
cp config/sidekiq.yml-example config/sidekiq.yml

Expand Down
Loading