Skip to content

Commit c5622a5

Browse files
committed
Add generator for sidekiq
1 parent eb359c5 commit c5622a5

File tree

5 files changed

+180
-0
lines changed

5 files changed

+180
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* Adds Honeybadger generator. ([@mausamp][])
1414
* Adds Rails ERD generator. ([@mausamp][])
1515
* Adds Annotate Generator. ([@TheZero0-ctrl][])
16+
* Adds Sidekiq generator. ([@TheZero0-ctrl][])
1617

1718
## 0.13.0 (March 26th, 2024)
1819
* Adds Letter Opener generator. ([@coolprobn][])

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ The boring generator introduces following generators:
9696
- Install Honeybadger: `rails generate boring:honeybadger:install`
9797
- Install Rails ERD: `rails generate boring:rails_erd:install`
9898
- Install Annotate: `rails generate boring:annotate:install`
99+
- Install Sidekiq: `rails generate boring:sidekiq:install`
99100

100101
## Screencasts
101102

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# frozen_string_literal: true
2+
3+
require 'boring_generators/generator_helper'
4+
5+
module Boring
6+
module Sidekiq
7+
class InstallGenerator < Rails::Generators::Base
8+
include BoringGenerators::GeneratorHelper
9+
10+
desc "Adds Sidekiq to the application"
11+
source_root File.expand_path("templates", __dir__)
12+
13+
class_option :skip_routes,
14+
type: :boolean,
15+
aliases: "-sr",
16+
default: false,
17+
desc: "Tell us if you want to skip sidekiq routes for viewing Web UI. Defaults to false."
18+
class_option :authenticate_routes_with_devise,
19+
type: :boolean,
20+
aliases: "-ar",
21+
default: false,
22+
desc: "Tell us if you want sidekiq routes to only be accessed by authenticated users. Defaults to false."
23+
class_option :skip_procfile_config,
24+
type: :boolean,
25+
aliases: "-sp",
26+
default: false,
27+
desc: "Tell us if you want to skip adding sidekiq worker to Procfile. Defaults to false."
28+
29+
def add_sidekiq_gem
30+
say "Adding sidekiq gem to Gemfile", :green
31+
check_and_install_gem("sidekiq")
32+
bundle_install
33+
end
34+
35+
def set_sidekiq_as_active_job_adapter
36+
say "Setting sidekiq as active_job adapter", :green
37+
38+
inject_into_file "config/application.rb",
39+
optimize_indentation(
40+
"config.active_job.queue_adapter = :sidekiq\n",
41+
4
42+
),
43+
after: /class Application < Rails::Application\n/
44+
45+
end
46+
47+
def add_sidekiq_routes
48+
return if options[:skip_routes]
49+
50+
say "Adding sidekiq routes", :green
51+
52+
if options[:authenticate_routes_with_devise]
53+
route = <<~RUBY
54+
authenticate :user do
55+
mount Sidekiq::Web => '/sidekiq'
56+
end
57+
58+
RUBY
59+
else
60+
route = "mount Sidekiq::Web => '/sidekiq'\n\n"
61+
end
62+
63+
inject_into_file "config/routes.rb",
64+
"require 'sidekiq/web'\n\n",
65+
before: "Rails.application.routes.draw do\n"
66+
67+
inject_into_file "config/routes.rb",
68+
optimize_indentation(route, 2),
69+
after: "Rails.application.routes.draw do\n"
70+
end
71+
72+
def add_sidekiq_worker_to_procfile
73+
return if options[:skip_procfile_config] || !File.exist?("Procfile.dev")
74+
75+
say "Adding sidekiq worker to Procfile.dev", :green
76+
append_to_file "Procfile.dev", "worker: bundle exec sidekiq"
77+
end
78+
79+
def show_message
80+
return if options[:skip_routes]
81+
82+
if options[:authenticate_routes_with_devise]
83+
readme "README"
84+
else
85+
say "\nWe've added Sidekiq routes. Please protect it as necessary to suit your requirements.",
86+
:yellow
87+
end
88+
end
89+
end
90+
end
91+
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
===============================================================================
2+
3+
We've protected Sidekiq routes with Devise to ensure only authenticated users can access them.
4+
This is implemented in the following snippet located in the config/routes.rb file.
5+
```
6+
authenticate :user do
7+
mount Sidekiq::Web => '/sidekiq'
8+
end
9+
```
10+
Please adjust it as necessary to suit your requirements.
11+
12+
===============================================================================
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# frozen_string_literal: true
2+
3+
require "test_helper"
4+
require "generators/boring/sidekiq/install/install_generator"
5+
6+
class SidekiqInstallGeneratorTest < Rails::Generators::TestCase
7+
tests Boring::Sidekiq::InstallGenerator
8+
setup :build_app
9+
teardown :teardown_app
10+
11+
include GeneratorHelper
12+
include ActiveSupport::Testing::Isolation
13+
14+
def destination_root
15+
app_path
16+
end
17+
18+
def test_should_configure_sidekiq
19+
Dir.chdir(app_path) do
20+
add_procfile
21+
quietly { run_generator }
22+
23+
assert_gem "sidekiq"
24+
assert_file "config/application.rb" do |content|
25+
assert_match(/config\.active_job\.queue_adapter = :sidekiq/, content)
26+
end
27+
assert_file "config/routes.rb" do |content|
28+
assert_match(/require 'sidekiq\/web'/, content)
29+
assert_match(/mount Sidekiq::Web/, content)
30+
end
31+
32+
assert_file "Procfile.dev" do |content|
33+
assert_match(/worker: bundle exec sidekiq/, content)
34+
end
35+
end
36+
end
37+
38+
def test_should_skip_sidekiq_routes
39+
Dir.chdir(app_path) do
40+
quietly { run_generator %w[--skip-routes] }
41+
42+
assert_file "config/routes.rb" do |content|
43+
assert_no_match(/mount Sidekiq::Web/, content)
44+
end
45+
end
46+
end
47+
48+
def test_should_authenticate_routes_with_devise
49+
Dir.chdir(app_path) do
50+
quietly { run_generator %w[--authenticate_routes_with_devise] }
51+
52+
assert_file "config/routes.rb" do |content|
53+
assert_match(/authenticate :user do/, content)
54+
assert_match(/mount Sidekiq::Web/, content)
55+
end
56+
end
57+
end
58+
59+
def test_should_skip_adding_sidekiq_worker_to_procfile
60+
Dir.chdir(app_path) do
61+
add_procfile
62+
quietly { run_generator %w[--skip_procfile_config] }
63+
64+
assert_file "Procfile.dev" do |content|
65+
assert_no_match(/worker: bundle exec sidekiq/, content)
66+
end
67+
end
68+
end
69+
70+
private
71+
72+
def add_procfile
73+
File.write("#{app_path}/Procfile.dev", "web: bin/rails server -p 3000 -b 0.0.0.0")
74+
end
75+
end

0 commit comments

Comments
 (0)