From 198f9f675b28a1c8ce9f730b910a61f2b13f3157 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Geonizeli?= Date: Wed, 18 Aug 2021 00:20:58 -0300 Subject: [PATCH] use jobs to devise emails delivery --- .foreman.yml | 3 ++- Gemfile | 1 + Gemfile.lock | 7 +++++++ app/models/admin_user.rb | 6 ++++++ app/models/user.rb | 4 ++++ config/application.rb | 10 ++-------- config/routes.rb | 12 ++++++++++-- docker-compose.yaml | 5 +++++ 8 files changed, 37 insertions(+), 11 deletions(-) diff --git a/.foreman.yml b/.foreman.yml index c63a8ca..fb02a25 100644 --- a/.foreman.yml +++ b/.foreman.yml @@ -1,3 +1,4 @@ rails: bundle exec rails server +sidekiq: bundle exec sidekiq webpack: yarn dev -relay: yarn relay:watch +relay: yarn relay:watch \ No newline at end of file diff --git a/Gemfile b/Gemfile index 9ec06ca..fed558f 100644 --- a/Gemfile +++ b/Gemfile @@ -19,6 +19,7 @@ gem "devise" gem "devise-i18n" gem "paper_trail" gem "slack-notifier" +gem "sidekiq", "~> 6.2" gem "administrate-field-active_storage" gem "administrate-field-enumerize" gem "tailwindcss-rails" diff --git a/Gemfile.lock b/Gemfile.lock index b0edbd3..7a0614e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -90,6 +90,7 @@ GEM choice (0.2.0) coderay (1.1.3) concurrent-ruby (1.1.9) + connection_pool (2.2.5) crass (1.0.6) datetime_picker_rails (0.0.7) momentjs-rails (>= 2.8.1) @@ -237,6 +238,7 @@ GEM rb-fsevent (0.11.0) rb-inotify (0.10.1) ffi (~> 1.0) + redis (4.4.0) regexp_parser (2.1.1) request_store (1.5.0) rack (>= 1.4) @@ -302,6 +304,10 @@ GEM semantic_range (3.0.0) shoulda-matchers (5.0.0) activesupport (>= 5.2.0) + sidekiq (6.2.1) + connection_pool (>= 2.2.2) + rack (~> 2.0) + redis (>= 4.2.0) slack-notifier (2.4.0) spring (2.1.1) sprockets (4.0.2) @@ -374,6 +380,7 @@ DEPENDENCIES rubocop-shopify sass-rails (>= 6) shoulda-matchers (~> 5.0) + sidekiq (~> 6.2) slack-notifier spring tailwindcss-rails diff --git a/app/models/admin_user.rb b/app/models/admin_user.rb index 42aafae..ee86d37 100644 --- a/app/models/admin_user.rb +++ b/app/models/admin_user.rb @@ -21,4 +21,10 @@ class AdminUser < ApplicationRecord devise :database_authenticatable, :recoverable, :rememberable, :validatable + + private + + def send_devise_notification(notification, *args) + devise_mailer.send(notification, self, *args).deliver_later + end end diff --git a/app/models/user.rb b/app/models/user.rb index 5b82b24..47c0853 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -51,4 +51,8 @@ class User < ApplicationRecord def notification_message "🎉 New user: #{email} 🎉" end + + def send_devise_notification(notification, *args) + devise_mailer.send(notification, self, *args).deliver_later + end end diff --git a/config/application.rb b/config/application.rb index 3af4639..1109ec1 100644 --- a/config/application.rb +++ b/config/application.rb @@ -27,15 +27,9 @@ module XStake # Initialize configuration defaults for originally generated Rails version. config.load_defaults(6.1) - # Configuration for the application, engines, and railties goes here. - # - # These settings can be overridden in specific environments using the files - # in config/environments, which are processed later. - # - # config.time_zone = "Central Time (US & Canada)" - # config.eager_load_paths << Rails.root.join("extras") - # Don't generate system test files. config.generators.system_tests = nil + + config.active_job.queue_adapter = :sidekiq end end diff --git a/config/routes.rb b/config/routes.rb index f7802d0..1459322 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,6 @@ # frozen_string_literal: true +require "sidekiq/web" + Rails.application.routes.draw do devise_for :users devise_for :admin_users @@ -16,9 +18,15 @@ Rails.application.routes.draw do root to: "home#index" get "*all" => "home#index", constraints: lambda { |req| - req.path.exclude?("playground") && req.path.exclude?("rails") + ["playground", "rails", "sidekiq"].filter do |path| + req.path != path + end.blank? } post "/graphql", to: "graphql#execute" - mount GraphqlPlayground::Rails::Engine, at: "/playground", graphql_path: "/graphql" if Rails.env.development? + + if Rails.env.development? + mount GraphqlPlayground::Rails::Engine, at: "/playground", graphql_path: "/graphql" + mount Sidekiq::Web => "/sidekiq" + end end diff --git a/docker-compose.yaml b/docker-compose.yaml index f3eefeb..35cbcfc 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -5,9 +5,11 @@ services: - "3000:3000" environment: - DATABASE_URL=postgres://postgres:postgres@db:5432/postgres + - REDIS_URL=redis://redis image: xstake depends_on: - db + - redis volumes: - ./:/usr/src/app/ db: @@ -19,5 +21,8 @@ services: - "5432:5432" volumes: - "data:/var/lib/postgresql/data" + db: + image: redis:6.2-alpine + volumes: data: