From 91ace96acd9255c56fa4b3e26ea02c35274177b3 Mon Sep 17 00:00:00 2001 From: Sojan Jose Date: Sun, 5 Jan 2020 23:26:22 +0530 Subject: [PATCH] Chore: Sidekiq ActionCable fix for Development (#405) - action cable works from sidekiq in development environments - documentation updates for docker --- .env.example | 4 +++ config/cable.yml | 3 ++- config/routes.rb | 27 +++++++++++++++----- docker-compose.production.yaml | 2 +- docker-compose.yaml | 2 +- docs/development/environment-setup/docker.md | 16 ++++++++---- 6 files changed, 40 insertions(+), 14 deletions(-) diff --git a/.env.example b/.env.example index 1ff28dd5b..b83e8dac1 100644 --- a/.env.example +++ b/.env.example @@ -36,6 +36,10 @@ AWS_REGION= #sentry SENTRY_DSN= +# Credentials to access sidekiq dashboard in production +SIDEKIQ_AUTH_USERNAME= +SIDEKIQ_AUTH_PASSWORD= + #### This environment variables are only required in hosted version which has billing ENABLE_BILLING= diff --git a/config/cable.yml b/config/cable.yml index cfe40a8a0..6e89df275 100644 --- a/config/cable.yml +++ b/config/cable.yml @@ -1,5 +1,6 @@ development: - adapter: async + adapter: redis + url: <%= ENV.fetch('REDIS_URL', 'redis://127.0.0.1:6379') %> test: adapter: test diff --git a/config/routes.rb b/config/routes.rb index 42d592fac..4a61500c1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -90,12 +90,6 @@ Rails.application.routes.draw do end end - # Sidekiq Web UI - require 'sidekiq/web' - authenticate :user, ->(u) { u.administrator? } do - mount Sidekiq::Web => '/sidekiq' - end - # Used in mailer templates resource :app, only: [:index] do resources :conversations, only: [:show] @@ -106,4 +100,25 @@ Rails.application.routes.draw do # Routes for testing resources :widget_tests, only: [:index] unless Rails.env.production? + + # ---------------------------------------------------------------------- + # Internal Monitoring Routes + require 'sidekiq/web' + + scope :monitoring do + # Sidekiq should use basic auth in production environment + if Rails.env.production? + Sidekiq::Web.use Rack::Auth::Basic do |username, password| + ENV['SIDEKIQ_AUTH_USERNAME'] && + ENV['SIDEKIQ_AUTH_PASSWORD'] && + ActiveSupport::SecurityUtils.secure_compare(::Digest::SHA256.hexdigest(username), + ::Digest::SHA256.hexdigest(ENV['SIDEKIQ_AUTH_USERNAME'])) && + ActiveSupport::SecurityUtils.secure_compare(::Digest::SHA256.hexdigest(password), + ::Digest::SHA256.hexdigest(ENV['SIDEKIQ_AUTH_PASSWORD'])) + end + end + + mount Sidekiq::Web, at: '/sidekiq' + end + # ---------------------------------------------------------------------- end diff --git a/docker-compose.production.yaml b/docker-compose.production.yaml index af5b91ca5..a45dd77a6 100644 --- a/docker-compose.production.yaml +++ b/docker-compose.production.yaml @@ -20,7 +20,7 @@ services: - redis ports: - 3000:3000 - env_file: .env.example ## Change this file for customised env variables + env_file: .env ## Change this file for customized env variables environment: - NODE_ENV=production - RAILS_ENV=production diff --git a/docker-compose.yaml b/docker-compose.yaml index eedcc1109..d3d971722 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -32,7 +32,7 @@ services: - mailhog ports: - 3000:3000 - env_file: .env.example + env_file: .env environment: - WEBPACKER_DEV_SERVER_HOST=webpack - NODE_ENV=development diff --git a/docs/development/environment-setup/docker.md b/docs/development/environment-setup/docker.md index fef84f37d..5d5eb0f0c 100644 --- a/docs/development/environment-setup/docker.md +++ b/docs/development/environment-setup/docker.md @@ -7,6 +7,12 @@ title: "Docker Setup and Debugging Guide" After cloning the repo and installing docker on your machine, run the following command from the root directory of the project. +```bash +cp .env.example .env +``` + +Make changes to the `.env` file as required. [Optional] + ```bash docker-compose build ``` @@ -14,19 +20,19 @@ docker-compose build After building the image or after destroying the stack you would have to reset the database using following command ```bash -docker-compose run rails bundle exec rails db:reset +docker-compose run --rm rails bundle exec rails db:reset ``` ### Running the app ```bash -docker-compose run --service-port rails +docker-compose run --rm --service-port rails ``` open another terminal and also run below command to run sidekiq in a separate service ``` -docker-compose run rails bundle exec sidekiq +docker-compose run --rm rails bundle exec sidekiq ``` * Access the rails app frontend by visiting `http://0.0.0.0:3000/` @@ -50,13 +56,13 @@ docker-compose down For running the complete rspec tests ```bash -docker-compose run rails bundle exec rspec +docker-compose run --rm rails bundle exec rspec ``` For running specific test: ```bash -docker-compose run rails bundle exec rspec spec/: +docker-compose run --rm rails bundle exec rspec spec/: ``` ## production environment