Skip to content

Commit

Permalink
Use sidekiq as email delivery queue
Browse files Browse the repository at this point in the history
Also,
* put all indexers in an indexer capistrano role
* Remove perform_enqueued_jobs from the tests, since it is not
  compatible with sidekiq

Closes #4609

Co-authored-by: Max Kadel <[email protected]>
  • Loading branch information
sandbergja and maxkadel committed Jan 23, 2025
1 parent 2d40b34 commit 7145088
Show file tree
Hide file tree
Showing 17 changed files with 74 additions and 66 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,17 @@ bundle exec cap staging deploy # deploys main branch to staging
```

## Staging Mail Catcher
To see mail that has been sent on the staging server you must ssh tunnel into the server
To see mail that has been sent from the staging environment you must ssh tunnel into the staging indexer servers.
```
ssh -L 1082:localhost:1080 pulsys@catalog-staging1
ssh -L 1082:localhost:1080 pulsys@catalog-indexer-staging1
ssh -L 1085:localhost:1080 pulsys@catalog-indexer-staging2
```
To see mail that has been sent on the qa server you must ssh tunnel into the server
```
ssh -L 1082:localhost:1080 pulsys@catalog-qa1
ssh -L 1082:localhost:1080 pulsys@catalog-indexer-qa1
ssh -L 1085:localhost:1080 pulsys@catalog-indexer-qa2
```
Once the tunnel is open [you can see the mail that has been sent on staging here]( http://localhost:1082/)
Once the tunnel is open [you can see the mail that has been sent on indexer1 here]( http://localhost:1082/) and [indexer2 here]( http://localhost:1085/)

## Testing

Expand Down Expand Up @@ -245,6 +247,7 @@ Start the Bibdata server, and then set the ```bidata_base``` value in OrangeLigh
```
run every time
```
bundle exec sidekiq -q default -q mailers
mailcatcher
```
Expand Down
2 changes: 2 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class Application < Rails::Application
# config.time_zone = 'Central Time (US & Canada)'
config.time_zone = 'Eastern Time (US & Canada)'

config.active_job.queue_adapter = :sidekiq

# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
Expand Down
12 changes: 10 additions & 2 deletions config/deploy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

set :branch, ENV.fetch('BRANCH', 'main')

# Default branch is :master
# ask :branch, proc { `git rev-parse --abbrev-ref HEAD`.chomp }.call

# Default deploy_to directory is /var/www/my_app
set :deploy_to, '/opt/orangelight'
Expand Down Expand Up @@ -159,5 +157,15 @@
end
end

namespace :sidekiq do
task :restart do
on roles(:indexer) do
execute :sudo, :service, :sidekiq, :restart
end
end
end

after 'deploy:reverted', 'sneakers:restart'
after 'deploy:published', 'sneakers:restart'
after 'deploy:reverted', 'sidekiq:restart'
after 'deploy:published', 'sidekiq:restart'
6 changes: 3 additions & 3 deletions config/deploy/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
server 'catalog3', user: 'deploy', roles: %i[web app db worker]
server 'catalog4', user: 'deploy', roles: %i[web app db worker]
server 'catalog5', user: 'deploy', roles: %i[web app db worker]
server 'catalog-indexer1', user: 'deploy', roles: %i[cron_prod1 cron_db worker]
server 'catalog-indexer2', user: 'deploy', roles: %i[cron_prod2 worker]
server 'catalog-indexer3', user: 'deploy', roles: %i[cron_prod3 worker]
server 'catalog-indexer1', user: 'deploy', roles: %i[cron_prod1 cron_db worker indexer]
server 'catalog-indexer2', user: 'deploy', roles: %i[cron_prod2 worker indexer]
server 'catalog-indexer3', user: 'deploy', roles: %i[cron_prod3 worker indexer]

set :deploy_to, '/opt/orangelight'
set :log_level, :info
4 changes: 2 additions & 2 deletions config/deploy/qa.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

server 'catalog-qa1.princeton.edu', user: 'deploy', roles: %i[web app db worker mailcatcher]
server 'catalog-qa2.princeton.edu', user: 'deploy', roles: %i[web app db worker mailcatcher cron_db]
server 'catalog-indexer-qa1', user: 'deploy', roles: %i[worker]
server 'catalog-indexer-qa2', user: 'deploy', roles: %i[cron_db worker]
server 'catalog-indexer-qa1', user: 'deploy', roles: %i[worker indexer]
server 'catalog-indexer-qa2', user: 'deploy', roles: %i[cron_db worker indexer]

namespace :env do
desc 'Set an Orangelight environment variable'
Expand Down
4 changes: 2 additions & 2 deletions config/deploy/staging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

server 'catalog-staging1', user: 'deploy', roles: %i[web app db worker mailcatcher cron_db]
server 'catalog-staging2', user: 'deploy', roles: %i[web app worker mailcatcher]
server 'catalog-indexer-staging1', user: 'deploy', roles: %i[cron_db worker]
server 'catalog-indexer-staging2', user: 'deploy', roles: %i[cron_db worker]
server 'catalog-indexer-staging1', user: 'deploy', roles: %i[cron_db worker indexer]
server 'catalog-indexer-staging2', user: 'deploy', roles: %i[cron_db worker indexer]

namespace :env do
desc 'Set an Orangelight environment variable'
Expand Down
4 changes: 3 additions & 1 deletion config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@
# ActionMailer::Base.deliveries array.
config.action_mailer.delivery_method = :test
config.action_mailer.deliver_later_queue_name = 'mailers'
config.active_job.queue_adapter = :test
config.active_job.queue_adapter = :sidekiq
require 'sidekiq/testing'
Sidekiq::Testing.inline!
config.action_mailer.default_options = {
from: '[email protected]'
}
Expand Down
18 changes: 18 additions & 0 deletions config/initializers/sidekiq.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

require "redis-client"

# nosemgrep
redis_config_from_yml = YAML.safe_load(ERB.new(Rails.root.join("config/redis.yml").read).result, aliases: true)[Rails.env].with_indifferent_access
redis_config = RedisClient.config(host: redis_config_from_yml[:host], port: redis_config_from_yml[:port], db: redis_config_from_yml[:db])

# Sidekiq::Client.reliable_push! unless Rails.env.test? # sidekiq pro feature, we would need the key and sidekiq pro gem to enable this
Sidekiq.configure_server do |config|
config.redis = { url: redis_config.server_url }
# config.super_fetch! # sidekiq pro feature
# config.reliable_scheduler! # sidekiq pro feature
end

Sidekiq.configure_client do |config|
config.redis = { url: redis_config.server_url }
end
6 changes: 6 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'sidekiq/web'

Rails.application.routes.draw do
scope module: 'orangelight' do
get 'browse', to: 'browsables#browse'
Expand Down Expand Up @@ -65,6 +67,10 @@
get "sign_out", to: "sessions#destroy"
end

authenticate :user, ->(user) { user.admin? } do
mount Sidekiq::Web => '/sidekiq'
end

get '/catalog/oclc/:id', to: 'catalog#oclc'
get '/catalog/isbn/:id', to: 'catalog#isbn'
get '/catalog/lccn/:id', to: 'catalog#lccn'
Expand Down
2 changes: 0 additions & 2 deletions spec/controllers/catalog_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,12 @@
sign_in user

post :email, params: { id: '9997412163506421', to: '[email protected]' }
perform_enqueued_jobs
expect(email.reply_to).to eq [user.email]
end
it 'supports a user-submitted subject line' do
sign_in user

post :email, params: { id: '9997412163506421', to: '[email protected]', subject: ['Subject'] }
perform_enqueued_jobs
expect(email.subject).to eq 'Subject'
end
it 'does not send an email if not logged in' do
Expand Down
Loading

0 comments on commit 7145088

Please sign in to comment.