Skip to content

Commit

Permalink
Prototype ActionCable and ActiveJob with Redis for admin notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
remomueller committed Aug 13, 2018
1 parent d7e4e00 commit 00b23d1
Show file tree
Hide file tree
Showing 19 changed files with 114 additions and 14 deletions.
10 changes: 10 additions & 0 deletions .ebextensions/https-redirect-ruby-puma.config
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ files:
add_header Cache-Control public;
}

# Enables WebSockets for ActionCable.
location /cable {
proxy_pass http://my_app;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

location /public {
alias /var/app/current/public;
gzip_static on;
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ gem "bootsnap", ">= 1.1.0", require: false
gem "coffee-rails", "~> 4.2"
gem "jbuilder", "~> 2.5"
gem "puma", "~> 3.11"
gem "redis", "~> 4.0"
gem "sass-rails", "~> 5.0"
gem "turbolinks", "~> 5"
gem "uglifier", ">= 1.3.0"
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ GEM
rb-fsevent (0.10.3)
rb-inotify (0.9.10)
ffi (>= 0.5.0, < 2)
redis (4.0.2)
responders (2.4.0)
actionpack (>= 4.2.0, < 5.3)
railties (>= 4.2.0, < 5.3)
Expand Down Expand Up @@ -274,6 +275,7 @@ DEPENDENCIES
pg_search (~> 2.1.2)
puma (~> 3.11)
rails (= 5.2.1)
redis (~> 4.0)
sass-rails (~> 5.0)
selenium-webdriver
simplecov (~> 0.16.1)
Expand Down
18 changes: 18 additions & 0 deletions app/assets/javascripts/channels/admin.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
App.admin = App.cable.subscriptions.create "AdminChannel",
connected: ->
console.log "connected: AdminChannel"
# Called when the subscription is ready for use on the server

disconnected: ->
console.log "disconnected: AdminChannel"
# Called when the subscription has been terminated by the server

received: (data) ->
console.log "received: AdminChannel"
console.log data
# $("[data-channel=files-count]").append(data.files_count)
$("[data-channel=files-count]").html(data.files_count)

refresh_count: ->
console "refresh_count: AdminChannel"
@perform "refresh_count"
14 changes: 14 additions & 0 deletions app/assets/javascripts/channels/files.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
App.files = App.cable.subscriptions.create "FilesChannel",
connected: ->
console.log "connected: FilesChannel"
# Called when the subscription is ready for use on the server

disconnected: ->
console.log "disconnected: FilesChannel"
# Called when the subscription has been terminated by the server

received: (data) ->
# Called when there's incoming data on the websocket for this channel
$("#files-count").html(data.files_count)
console.log "received: FilesChannel"
console.log data
9 changes: 9 additions & 0 deletions app/channels/admin_channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class AdminChannel < ApplicationCable::Channel
def subscribed
stream_from "admin_channel"
end

def unsubscribed
# Any cleanup needed when channel is unsubscribed
end
end
13 changes: 13 additions & 0 deletions app/channels/files_channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class FilesChannel < ApplicationCable::Channel
def subscribed
stream_from "files_channel"
end

def unsubscribed
# Any cleanup needed when channel is unsubscribed
end

def receive(data)
ActionCable.server.broadcast("files_channel", data)
end
end
8 changes: 5 additions & 3 deletions app/controllers/admin_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ class AdminController < ApplicationController
# def dashboard
# end

# # GET /admin/debug
# def debug
# end
# GET /admin/debug
def debug
AdminJob.perform_later
ActionCable.server.broadcast("files_channel", files_count: ActiveStorage::Blob.count)
end
end
12 changes: 12 additions & 0 deletions app/jobs/admin_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

class AdminJob < ApplicationJob
queue_as :default

def perform
ActionCable.server.broadcast(
"admin_channel",
files_count: AdminController.render(partial: "admin/files_count", locals: { number: rand(10_000) })
)
end
end
1 change: 1 addition & 0 deletions app/views/admin/_files_count.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
= render "forms/horizontal/show/generic", title: "Files Count (ActionCable)", content: number
2 changes: 2 additions & 0 deletions app/views/admin/dashboard.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@
.dashboard-container
= render "forms/horizontal/show/generic", title: "Users", content: User.count
=# render "forms/horizontal/show/generic", title: "Documents", content: -1

#files-count
1 change: 1 addition & 0 deletions app/views/admin/debug.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
= render "forms/horizontal/show/generic", title: "Website name", content: ENV["website_name"]
= render "forms/horizontal/show/generic", title: "Website URL", content: ENV["website_url"]
= render "forms/horizontal/show/generic", title: "Emails enabled", content: simple_check(EMAILS_ENABLED)
%div{ data: { channel: "files-count" } }=# render "admin/files_count", number: rand(100)
1 change: 1 addition & 0 deletions app/views/layouts/application.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
= favicon_link_tag
= csrf_meta_tags
= csp_meta_tag
= action_cable_meta_tag
= stylesheet_link_tag "application", media: "all", data: { turbolinks_track: "reload" }
= javascript_include_tag "application", data: { turbolinks_track: "reload" }
- render("themes/theme") unless @theme
Expand Down
1 change: 1 addition & 0 deletions app/views/layouts/full_page.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
= favicon_link_tag
= csrf_meta_tags
= csp_meta_tag
= action_cable_meta_tag
= stylesheet_link_tag "application", media: "all", data: { turbolinks_track: "reload" }
= javascript_include_tag "application", data: { turbolinks_track: "reload" }
- render("themes/theme") unless @theme
Expand Down
1 change: 1 addition & 0 deletions app/views/layouts/full_page_sidebar.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
= favicon_link_tag
= csrf_meta_tags
= csp_meta_tag
= action_cable_meta_tag
= stylesheet_link_tag "application", media: "all", data: { turbolinks_track: "reload" }
= javascript_include_tag "application", data: { turbolinks_track: "reload" }
- render("themes/theme") unless @theme
Expand Down
4 changes: 3 additions & 1 deletion config/cable.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
development:
adapter: async
adapter: redis
url: redis://localhost:6379/1
channel_prefix: loft_hf_development

test:
adapter: async
Expand Down
7 changes: 5 additions & 2 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@

# Enable/disable caching. By default caching is disabled.
# Run rails dev:cache to toggle caching.
if Rails.root.join('tmp', 'caching-dev.txt').exist?
if Rails.root.join("tmp", "caching-dev.txt").exist?
config.action_controller.perform_caching = true

config.cache_store = :memory_store
config.public_file_server.headers = {
'Cache-Control' => "public, max-age=#{2.days.to_i}"
"Cache-Control" => "public, max-age=#{2.days.to_i}"
}
else
config.action_controller.perform_caching = false
Expand Down Expand Up @@ -58,4 +58,7 @@
# Use an evented file watcher to asynchronously detect changes in source code,
# routes, locales, etc. This feature depends on the listen gem.
config.file_watcher = ActiveSupport::EventedFileUpdateChecker

# Set Action Cable server url for consumer connection
config.action_cable.url = "ws://localhost/edge/lofthf.study/cable"
end
16 changes: 8 additions & 8 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

# Disable serving static files from the `/public` folder by default since
# Apache or NGINX already handles this.
config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
config.public_file_server.enabled = ENV["RAILS_SERVE_STATIC_FILES"].present?

# Compress JavaScripts and CSS.
config.assets.js_compressor = :uglifier
Expand All @@ -32,19 +32,19 @@
# `config.assets.precompile` and `config.assets.version` have moved to config/initializers/assets.rb

# Enable serving of images, stylesheets, and JavaScripts from an asset server.
# config.action_controller.asset_host = 'http://assets.example.com'
# config.action_controller.asset_host = "http://assets.example.com"

# Specifies the header that your server uses for sending files.
# config.action_dispatch.x_sendfile_header = 'X-Sendfile' # for Apache
# config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX
# config.action_dispatch.x_sendfile_header = "X-Sendfile" # for Apache
# config.action_dispatch.x_sendfile_header = "X-Accel-Redirect" # for NGINX

# Store files on Amazon S3.
config.active_storage.service = :amazon

# Mount Action Cable outside main process or domain
# config.action_cable.mount_path = nil
# config.action_cable.url = 'wss://example.com/cable'
# config.action_cable.allowed_request_origins = [ 'http://example.com', /http:\/\/example.*/ ]
# config.action_cable.url = "wss://example.com/cable"
# config.action_cable.allowed_request_origins = [ "http://example.com", /http:\/\/example.*/ ]

# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
# config.force_ssl = true
Expand Down Expand Up @@ -80,8 +80,8 @@
config.log_formatter = ::Logger::Formatter.new

# Use a different logger for distributed setups.
# require 'syslog/logger'
# config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name')
# require "syslog/logger"
# config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new "app-name")

if ENV["RAILS_LOG_TO_STDOUT"].present?
logger = ActiveSupport::Logger.new(STDOUT)
Expand Down
7 changes: 7 additions & 0 deletions test/jobs/admin_job_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class AdminJobTest < ActiveJob::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit 00b23d1

Please sign in to comment.