Skip to content

Comments

Add generator for sidekiq#17

Open
TheZero0-ctrl wants to merge 3 commits intomainfrom
generator-for-sidekiq
Open

Add generator for sidekiq#17
TheZero0-ctrl wants to merge 3 commits intomainfrom
generator-for-sidekiq

Conversation

@TheZero0-ctrl
Copy link
Collaborator

@TheZero0-ctrl TheZero0-ctrl commented Apr 25, 2024

Summary by CodeRabbit

  • New Features
    • Added a generator to install and configure Sidekiq.
    • Automatically sets Active Job to use Sidekiq.
    • Optionally mounts the Sidekiq Web UI (authenticated by default) or skips it.
    • Optionally adds a Sidekiq worker entry to Procfile.dev.
  • Documentation
    • Updated README with usage instructions and guidance on protecting Sidekiq routes.
    • Updated changelog with the new generator entry.
  • Tests
    • Added tests covering default setup and all configuration options.

if options[:authenticate_routes_with_devise]
readme "README"
else
say "\nWe've added Sidekiq routes. Please protect it as necessary to suit your requirements.",
Copy link
Member

Choose a reason for hiding this comment

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

Let's update to " ... ... Please protect it with authentication as necessary ... ... ..."

@coolprobn
Copy link
Member

coolprobn commented May 9, 2024

I just remembered, we also need to add the command to run the sidekiq to Procfile.dev (or Procfile in general, research) if it exists so it runs when we do "bin/dev"

@TheZero0-ctrl
Copy link
Collaborator Author

I just remembered, we also need to add the command to run the sidekiq to Procfile.dev (or Procfile in general, research) if it exists so it runs when we do "bin/dev"

rails auto generate Procfile.dev and it seems Procfile is used mostly for the heroku, should I check for both file?

@coolprobn
Copy link
Member

@TheZero0-ctrl We should only support Procfile.dev generated by rails in the beginning, user should have capability to skip this configuration as well.

aliases: "-ar",
default: false,
desc: "Tell us if you want sidekiq routes to only be accessed by authenticated users. Defaults to false."
class_option :skip_adding_sidekiq_to_procfile,
Copy link
Member

Choose a reason for hiding this comment

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

Let's update to skip_procfile_config / _configuration whichever you want.

say "Adding sidekiq worker to Procfile.dev", :green
append_to_file "Procfile.dev", "worker: bundle exec sidekiq"
else
say "Procfile.dev not found. Skipping adding sidekiq worker to Procfile.dev", :yellow
Copy link
Member

Choose a reason for hiding this comment

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

It's fine to return early if File is not found, we don't need to show file not found message

@TheZero0-ctrl TheZero0-ctrl force-pushed the generator-for-sidekiq branch 4 times, most recently from cd73ee5 to c9abf62 Compare May 24, 2024 09:59
@TheZero0-ctrl TheZero0-ctrl force-pushed the generator-for-sidekiq branch from c9abf62 to 9f91d3f Compare May 31, 2024 15:32
@coolprobn coolprobn added the PR CREATED PR has been created in main Boring Generators Github label Jun 17, 2024
@TheZero0-ctrl TheZero0-ctrl force-pushed the generator-for-sidekiq branch 2 times, most recently from beaea6b to c5622a5 Compare June 30, 2024 11:47
@TheZero0-ctrl TheZero0-ctrl force-pushed the generator-for-sidekiq branch from c5622a5 to 0a4fe67 Compare August 12, 2024 14:30
@coderabbitai
Copy link

coderabbitai bot commented Sep 23, 2025

Walkthrough

Adds a new Rails generator to install and configure Sidekiq, updates documentation (CHANGELOG, README), provides a template README note for authenticated routes, and introduces tests covering generator behaviors and options.

Changes

Cohort / File(s) Summary
Documentation
CHANGELOG.md, README.md
Added unreleased changelog entry for Sidekiq generator and a README usage bullet for running the install command.
Generator Implementation
lib/generators/boring/sidekiq/install/install_generator.rb, lib/generators/boring/sidekiq/install/templates/README
Introduced Sidekiq install generator with options to skip web UI, authenticated web UI, and Procfile entry; configures gem, Active Job adapter, routes, and Procfile; added template README note about Devise-protected routes.
Tests
test/generators/.../install_generator_test.rb
Added tests verifying full setup and option handling: skipping web UI, skipping authentication wrapper, and skipping Procfile entry; includes helper to seed Procfile.dev.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor Dev as Developer
  participant Rails as Rails Generator CLI
  participant Gen as Boring::Sidekiq::InstallGenerator
  participant Bundler as Bundler
  participant App as config/application.rb
  participant Routes as config/routes.rb
  participant Proc as Procfile.dev

  Dev->>Rails: rails generate boring:sidekiq:install [options]
  Rails->>Gen: invoke
  Gen->>Bundler: add "sidekiq" gem + bundle install
  Gen->>App: set config.active_job.queue_adapter = :sidekiq
  alt Web UI not skipped
    alt Authenticated not skipped
      Gen->>Routes: mount Sidekiq::Web within authenticate :user
    else Authenticated skipped
      Gen->>Routes: mount Sidekiq::Web without auth
    end
  else Web UI skipped
    Gen->>Routes: no changes to Sidekiq::Web
  end
  alt Procfile config not skipped and Procfile.dev exists
    Gen->>Proc: append worker: bundle exec sidekiq
  else Skipped or missing
    Gen->>Proc: no change
  end
  Gen-->>Dev: output next steps / README note
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

I nibbled some queues with a whisker’s delight,
Spun Sidekiq threads in the soft moonlight.
Routes tucked safely, behind a snug door,
A Procfile hums—one worker, then more.
With tiny paws, I commit and hop—
Jobs bound for burrows, tip-tap, nonstop! 🐇✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title "Add generator for sidekiq" is concise and directly describes the primary change—the addition of a Sidekiq generator—making it relevant and clear for repository history and reviewers.
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch generator-for-sidekiq

Tip

👮 Agentic pre-merge checks are now available in preview!

Pro plan users can now enable pre-merge checks in their settings to enforce checklists before merging PRs.

  • Built-in checks – Quickly apply ready-made checks to enforce title conventions, require pull request descriptions that follow templates, validate linked issues for compliance, and more.
  • Custom agentic checks – Define your own rules using CodeRabbit’s advanced agentic capabilities to enforce organization-specific policies and workflows. For example, you can instruct CodeRabbit’s agent to verify that API documentation is updated whenever API schema files are modified in a PR. Note: Upto 5 custom checks are currently allowed during the preview period. Pricing for this feature will be announced in a few weeks.

Please see the documentation for more information.

Example:

reviews:
  pre_merge_checks:
    custom_checks:
      - name: "Undocumented Breaking Changes"
        mode: "warning"
        instructions: |
          Pass/fail criteria: All breaking changes to public APIs, CLI flags, environment variables, configuration keys, database schemas, or HTTP/GraphQL endpoints must be documented in the "Breaking Change" section of the PR description and in CHANGELOG.md. Exclude purely internal or private changes (e.g., code not exported from package entry points or explicitly marked as internal).

Please share your feedback with us on this Discord post.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 5

🧹 Nitpick comments (5)
README.md (1)

101-101: Document flags for the new generator.

Add a short note showing available options (--skip_web_ui, --skip_authenticated_web_ui, --skip_procfile_config) so users don’t have to discover them via --help.

Apply this diff to extend the bullet:

-- Install Sidekiq: `rails generate boring:sidekiq:install`
+- Install Sidekiq: `rails generate boring:sidekiq:install` (flags: `--skip_web_ui`, `--skip_authenticated_web_ui`, `--skip_procfile_config`)
lib/generators/boring/sidekiq/install/templates/README (1)

1-12: Clarify Devise dependency and alternatives.

Explicitly state that the snippet requires Devise and suggest alternatives (basic auth or custom constraints) when Devise isn’t present.

Apply this diff to add a one‑liner:

 We've protected Sidekiq routes with Devise to ensure only authenticated users can access them.
 This is implemented in the following snippet located in the config/routes.rb file.
+Note: This requires Devise. If you don't use Devise, protect the route via another mechanism (e.g., HTTP basic auth or a custom constraint).
test/generators/sidekiq_install_generator_test.rb (1)

18-36: Add idempotency checks and newline handling for Procfile.dev.

  • Run the generator twice and assert no duplicate route or adapter lines.
  • Ensure worker: is appended on a new line even if Procfile.dev lacks a trailing newline.

I can send a follow-up test patch if you want it bundled here.

lib/generators/boring/sidekiq/install/install_generator.rb (2)

38-47: Make adapter injection idempotent.

Re-running the generator will duplicate config.active_job.queue_adapter in config/application.rb.

Apply this diff:

   def set_sidekiq_as_active_job_adapter
     say "Setting sidekiq as active_job adapter", :green
-
-    inject_into_file "config/application.rb",
-                     optimize_indentation(
-                       "config.active_job.queue_adapter = :sidekiq\n",
-                       4
-                     ),
-                     after: /class Application < Rails::Application\n/
+    path = "config/application.rb"
+    content = File.read(path) if File.exist?(path)
+    return if content&.include?("config.active_job.queue_adapter = :sidekiq")
+
+    inject_into_file path,
+                     optimize_indentation("config.active_job.queue_adapter = :sidekiq\n", 4),
+                     after: /class Application < Rails::Application\n/
   end

65-72: Guard against duplicate route injections.

Re-running the generator will duplicate both require and mount entries.

Apply this diff:

-    inject_into_file "config/routes.rb",
-                     "require 'sidekiq/web'\\n\\n",
-                     before: "Rails.application.routes.draw do\\n"
+    routes_path = "config/routes.rb"
+    routes_content = File.read(routes_path)
+    unless routes_content.include?("require 'sidekiq/web'")
+      inject_into_file routes_path, "require 'sidekiq/web'\\n\\n",
+                       before: "Rails.application.routes.draw do\\n"
+    end
 
-    inject_into_file "config/routes.rb",
-                     optimize_indentation(route, 2),
-                     after: "Rails.application.routes.draw do\\n"
+    mount_signature = "mount Sidekiq::Web => '/sidekiq'"
+    unless routes_content.include?(mount_signature)
+      inject_into_file routes_path, optimize_indentation(route, 2),
+                       after: "Rails.application.routes.draw do\\n"
+    end
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

Disabled knowledge base sources:

  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between f882e5e and 67ae8b6.

📒 Files selected for processing (5)
  • CHANGELOG.md (1 hunks)
  • README.md (1 hunks)
  • lib/generators/boring/sidekiq/install/install_generator.rb (1 hunks)
  • lib/generators/boring/sidekiq/install/templates/README (1 hunks)
  • test/generators/sidekiq_install_generator_test.rb (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
lib/generators/boring/sidekiq/install/install_generator.rb (1)
lib/boring_generators/generator_helper.rb (2)
  • check_and_install_gem (22-32)
  • bundle_install (18-20)
test/generators/sidekiq_install_generator_test.rb (3)
lib/generators/boring/sidekiq/install/install_generator.rb (1)
  • include (7-91)
test/generator_helper.rb (1)
  • app_path (15-22)
test/test_helper.rb (1)
  • assert_gem (13-19)
🔇 Additional comments (1)
CHANGELOG.md (1)

5-5: Changelog entry reads well.

Entry is clear and placed under master (unreleased). No action needed.

Comment on lines +32 to +36
def add_sidekiq_gem
say "Adding sidekiq gem to Gemfile", :green
check_and_install_gem("sidekiq")
bundle_install
end
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Avoid double bundling; check_and_install_gem already runs bundle install.

This currently triggers bundle install twice, slowing the generator.

Apply this diff:

   def add_sidekiq_gem
     say "Adding sidekiq gem to Gemfile", :green
     check_and_install_gem("sidekiq")
-    bundle_install
   end
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
def add_sidekiq_gem
say "Adding sidekiq gem to Gemfile", :green
check_and_install_gem("sidekiq")
bundle_install
end
def add_sidekiq_gem
say "Adding sidekiq gem to Gemfile", :green
check_and_install_gem("sidekiq")
end
🤖 Prompt for AI Agents
In lib/generators/boring/sidekiq/install/install_generator.rb around lines 32 to
36, the generator calls bundle_install after check_and_install_gem even though
check_and_install_gem already performs bundling; remove the redundant
bundle_install call so the Gemfile is only bundled once, leaving the say message
and check_and_install_gem invocation intact.

Comment on lines +49 to +64
def add_sidekiq_web_ui
return if options[:skip_web_ui]

say "Adding sidekiq web UI", :green

if options[:skip_authenticated_web_ui]
route = "mount Sidekiq::Web => '/sidekiq'\n\n"
else
route = <<~RUBY
authenticate :user do
mount Sidekiq::Web => '/sidekiq'
end

RUBY
end

Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Don’t assume Devise; generate a safe route that works without it.

Using authenticate :user will break apps without Devise. Provide a conditional fallback so routes compile even when Devise isn’t installed.

Apply this diff:

   def add_sidekiq_web_ui
     return if options[:skip_web_ui]
 
     say "Adding sidekiq web UI", :green
 
-    if options[:skip_authenticated_web_ui]
-      route = "mount Sidekiq::Web => '/sidekiq'\n\n"
-    else
-      route = <<~RUBY
-        authenticate :user do
-          mount Sidekiq::Web => '/sidekiq'
-        end
-
-      RUBY
-    end
+    mount_line = "mount Sidekiq::Web => '/sidekiq'"
+    if options[:skip_authenticated_web_ui]
+      route = "#{mount_line}\n\n"
+    else
+      route = <<~RUBY
+        if defined?(Devise)
+          authenticate :user do
+            #{mount_line}
+          end
+        else
+          #{mount_line}
+        end
+
+      RUBY
+    end
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
def add_sidekiq_web_ui
return if options[:skip_web_ui]
say "Adding sidekiq web UI", :green
if options[:skip_authenticated_web_ui]
route = "mount Sidekiq::Web => '/sidekiq'\n\n"
else
route = <<~RUBY
authenticate :user do
mount Sidekiq::Web => '/sidekiq'
end
RUBY
end
def add_sidekiq_web_ui
return if options[:skip_web_ui]
say "Adding sidekiq web UI", :green
mount_line = "mount Sidekiq::Web => '/sidekiq'"
if options[:skip_authenticated_web_ui]
route = "#{mount_line}\n\n"
else
route = <<~RUBY
if defined?(Devise)
authenticate :user do
#{mount_line}
end
else
#{mount_line}
end
RUBY
end
🤖 Prompt for AI Agents
In lib/generators/boring/sidekiq/install/install_generator.rb around lines 49 to
64, the generator unconditionally uses authenticate :user (Devise), which will
break apps without Devise; change the generated route to wrap the authenticate
block in a runtime check and provide a fallback mount so routes compile without
Devise. Replace the current route assignment with a HEREDOC that emits: if
defined?(Devise) then authenticate :user do mount Sidekiq::Web => '/sidekiq' end
else mount Sidekiq::Web => '/sidekiq' end (preserving newlines/indentation) so
the generated routes work whether or not Devise is installed.

Comment on lines +74 to +79
def add_sidekiq_worker_to_procfile
return if options[:skip_procfile_config] || !File.exist?("Procfile.dev")

say "Adding sidekiq worker to Procfile.dev", :green
append_to_file "Procfile.dev", "worker: bundle exec sidekiq"
end
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Append worker: on a new line and avoid duplicates in Procfile.dev.

Without a newline, the worker entry can be glued to the last line. Also, prevent multiple worker lines across repeated runs.

Apply this diff:

   def add_sidekiq_worker_to_procfile
     return if options[:skip_procfile_config] || !File.exist?("Procfile.dev")
 
     say "Adding sidekiq worker to Procfile.dev", :green
-    append_to_file "Procfile.dev", "worker: bundle exec sidekiq"
+    path = "Procfile.dev"
+    content = File.read(path)
+    return if content.include?("worker: bundle exec sidekiq")
+
+    needs_newline = !content.end_with?("\n")
+    append_to_file path, "#{needs_newline ? "\n" : ""}worker: bundle exec sidekiq\n"
   end
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
def add_sidekiq_worker_to_procfile
return if options[:skip_procfile_config] || !File.exist?("Procfile.dev")
say "Adding sidekiq worker to Procfile.dev", :green
append_to_file "Procfile.dev", "worker: bundle exec sidekiq"
end
def add_sidekiq_worker_to_procfile
return if options[:skip_procfile_config] || !File.exist?("Procfile.dev")
say "Adding sidekiq worker to Procfile.dev", :green
path = "Procfile.dev"
content = File.read(path)
return if content.include?("worker: bundle exec sidekiq")
needs_newline = !content.end_with?("\n")
append_to_file path, "#{needs_newline ? "\n" : ""}worker: bundle exec sidekiq\n"
end
🤖 Prompt for AI Agents
In lib/generators/boring/sidekiq/install/install_generator.rb around lines 74 to
79, the generator appends "worker: bundle exec sidekiq" without ensuring a
preceding newline and without checking for duplicates; update the method to
first detect an existing Procfile.dev entry for a sidekiq worker and do nothing
if found, otherwise append the worker line preceded by a newline (or ensure the
file ends with a newline before appending) so the new entry stays on its own
line and repeated runs do not create duplicate worker lines.

Comment on lines +81 to +90
def show_message
return if options[:skip_routes]

if options[:authenticate_routes_with_devise]
readme "README"
else
say "\nWe've added Sidekiq routes. Please protect it as necessary to suit your requirements.",
:yellow
end
end
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix options mismatch in show_message.

skip_routes and authenticate_routes_with_devise aren’t declared; the message may be misleading when --skip_web_ui is used. Use existing options to decide what to print/read.

Apply this diff:

   def show_message
-    return if options[:skip_routes]
-
-    if options[:authenticate_routes_with_devise]
-      readme "README"
-    else
-      say "\nWe've added Sidekiq routes. Please protect it as necessary to suit your requirements.",
-          :yellow
-    end
+    return if options[:skip_web_ui]
+
+    if options[:skip_authenticated_web_ui]
+      say "\nWe've added Sidekiq routes. Please protect it with authentication as necessary to suit your requirements.",
+          :yellow
+    else
+      readme "README"
+    end
   end
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
def show_message
return if options[:skip_routes]
if options[:authenticate_routes_with_devise]
readme "README"
else
say "\nWe've added Sidekiq routes. Please protect it as necessary to suit your requirements.",
:yellow
end
end
def show_message
return if options[:skip_web_ui]
if options[:skip_authenticated_web_ui]
say "\nWe've added Sidekiq routes. Please protect it with authentication as necessary to suit your requirements.",
:yellow
else
readme "README"
end
end
🤖 Prompt for AI Agents
In lib/generators/boring/sidekiq/install/install_generator.rb around lines 81 to
90, the show_message method checks non-existent options (:skip_routes and
:authenticate_routes_with_devise) which can produce incorrect output; change it
to use the actual options—return early if options[:skip_web_ui], and branch on
options[:authenticate_with_devise] so that when authenticate_with_devise is true
you call readme "README" otherwise you print the yellow notice about protecting
the Sidekiq web UI.

Comment on lines +39 to +47
def test_should_skip_sidekiq_web_ui
Dir.chdir(app_path) do
quietly { run_generator %w[--skip_web_ui] }

assert_file "config/routes.rb" do |content|
assert_no_match(/mount Sidekiq::Web/, content)
end
end
end
Copy link

Choose a reason for hiding this comment

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

🧹 Nitpick

Also assert require 'sidekiq/web' is not added when Web UI is skipped.

Strengthens the contract when --skip_web_ui is used.

Apply this diff:

   assert_file "config/routes.rb" do |content|
     assert_no_match(/mount Sidekiq::Web/, content)
+    assert_no_match(/require 'sidekiq\/web'/, content)
   end
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
def test_should_skip_sidekiq_web_ui
Dir.chdir(app_path) do
quietly { run_generator %w[--skip_web_ui] }
assert_file "config/routes.rb" do |content|
assert_no_match(/mount Sidekiq::Web/, content)
end
end
end
def test_should_skip_sidekiq_web_ui
Dir.chdir(app_path) do
quietly { run_generator %w[--skip_web_ui] }
assert_file "config/routes.rb" do |content|
assert_no_match(/mount Sidekiq::Web/, content)
assert_no_match(/require 'sidekiq\/web'/, content)
end
end
end
🤖 Prompt for AI Agents
In test/generators/sidekiq_install_generator_test.rb around lines 39 to 47, the
test for --skip_web_ui checks that routes do not mount Sidekiq::Web but doesn't
assert that the require 'sidekiq/web' line is also omitted; update the assertion
block to also assert_no_match for a require statement (e.g.
assert_no_match(/require ['"]sidekiq\/web['"]/, content)) so the test fails if
the generator inadvertently adds the require when Web UI is skipped.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PR CREATED PR has been created in main Boring Generators Github

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants