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 sidekiq #291

Merged
merged 4 commits into from
Apr 20, 2022
Merged
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
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ GOOGLE_OAUTH_CLIENT_SECRET='google-oauth-client-secret'

# Application Monitoring
NEW_RELIC_LICENSE_KEY='replace with newrelic licence key'

# Redis url
REDIS_URL='redis://127.0.0.1:6379/12'
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ group :development, :test do

# help to kill N+1 queries and unused eager loading. https://github.com/flyerhzm/bullet
gem "bullet"

# Background job processing adapter
gem "sidekiq"
end

group :development do
Expand Down
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,10 @@ GEM
activesupport (>= 3)
shoulda-matchers (5.1.0)
activesupport (>= 5.2.0)
sidekiq (6.4.1)
connection_pool (>= 2.2.2)
rack (~> 2.0)
redis (>= 4.2.0)
simple_po_parser (1.1.5)
simplecov (0.21.2)
docile (~> 1.1)
Expand Down Expand Up @@ -516,6 +520,7 @@ DEPENDENCIES
selenium-webdriver (>= 4.0.0)
shoulda-callback-matchers (~> 1.1.1)
shoulda-matchers (~> 5.1)
sidekiq
simplecov
spring
stripe
Expand Down
2 changes: 2 additions & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
web: bundle exec puma -t 5:5 -p ${PORT:-3000} -e ${RACK_ENV:-production}
worker: bundle exec sidekiq -e production -C config/sidekiq.yml
1 change: 1 addition & 0 deletions Procfile.dev
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
web: bin/rails s -b 0.0.0.0 -p 3000
webpacker: bin/webpack-dev-server
sidekiq: bundle exec sidekiq -e development -C config/sidekiq.yml
24 changes: 14 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,41 +37,45 @@ nvm install $(cat .nvmrc)
```
brew install postgresql
```
6. Install Redis
```
brew install redis
```

6. Install gem
7. Install gem

```
bundle install
```

7. Install node packages
8. Install node packages

```
yarn install
```

8. Setup ENV's
9. Setup ENV's

```
cp .env.example .env
```

9. Update `DATABASE_URL` in `.env` as per local `psql` creds. For example, if
10. Update `DATABASE_URL` in `.env` as per local `psql` creds. For example, if
the user is `root` and password is `password`, change the variable as
`DATABASE_URL="postgres://root:password@localhost/miru_web?encoding=utf8&pool=5&timeout=5000"`

10. Update `APP_BASE_URL` in `.env` to `localhost:3000`
11. Run `bin/rails db:create RAILS_ENV=development` to create the database
12. Run `bin/rails db:migrate RAILS_ENV=development` for migrations
13. Run `bin/rails db:seed` for populating the database with initial data
11. Update `APP_BASE_URL` in `.env` to `localhost:3000`
12. Run `bin/rails db:create RAILS_ENV=development` to create the database
13. Run `bin/rails db:migrate RAILS_ENV=development` for migrations
14. Run `bin/rails db:seed` for populating the database with initial data

14. Run app in local env
15. Run app in local env

```
foreman start -f Procfile.dev
```

15. Navigate to [http://0.0.0.0:3000](http://0.0.0.0:3000)
16. Navigate to [http://0.0.0.0:3000](http://0.0.0.0:3000)

### To receive the emails in non-production apps.

Expand Down
2 changes: 2 additions & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@
config.action_mailer.delivery_method = ENV.fetch("MAILER_DELIVERY_METHOD", "letter_opener").to_sym
config.action_mailer.perform_deliveries = true

config.active_job.queue_adapter = :sidekiq

Rails.application.config.after_initialize do
Bullet.enable = true
Bullet.bullet_logger = true
Expand Down
1 change: 1 addition & 0 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,5 @@
host = ENV.fetch("APP_BASE_URL", "#{ENV["HEROKU_APP_NAME"] + ".herokuapp.com"}")
config.action_mailer.default_url_options = { host: host }
config.action_mailer.asset_host = host
config.active_job.queue_adapter = :sidekiq
end
9 changes: 9 additions & 0 deletions config/initializers/sidekiq.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

Sidekiq.configure_client do |config|
config.redis = { url: ENV["REDIS_URL"], size: 4, network_timeout: 5 }
end

Sidekiq.configure_server do |config|
config.redis = { url: ENV["REDIS_URL"], size: 4, network_timeout: 5 }
end
4 changes: 4 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"

class ActionDispatch::Routing::Mapper
def draw(routes_name)
instance_eval(File.read(Rails.root.join("config/routes/#{routes_name}.rb")))
Expand Down Expand Up @@ -50,4 +52,6 @@ def draw(routes_name)
get "profile", to: "users/registrations#edit"
delete "profile/purge_avatar", to: "users/registrations#purge_avatar"
end

mount Sidekiq::Web => "/sidekiq"
end
11 changes: 11 additions & 0 deletions config/sidekiq.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
development:
:concurrency: 1
production:
:concurrency: 1
:queues:
- default
Comment on lines +5 to +6
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
:queues:
- default
queues:
- default
- mailers
- action_mailbox_routing
- action_mailbox_incineration
- active_storage_analysis
- active_storage_purge

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found this https://mikerogers.io/2019/06/06/rails-6-sidekiq-queues Are you suggesting adding them for the same reason? Are they used internally by active storage automatically or do we need more configuration? @shivamsinghchahar

Copy link
Contributor

@shivamsinghchahar shivamsinghchahar Apr 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, exactly 💯

That's all we need, no additional configuration.

- mailers
- action_mailbox_routing
- action_mailbox_incineration
- active_storage_analysis
- active_storage_purge
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require "simplecov"
require "pundit/rspec"
require "sidekiq/testing"

if ENV.fetch("COVERAGE", false)
SimpleCov.start "rails" do
Expand Down