Skip to content

Commit

Permalink
Introduce suspenders:jobs generator (#1147)
Browse files Browse the repository at this point in the history
Ports the existing generator to Suspenders 3.0.
  • Loading branch information
laicuRoot authored and stevepolitodesign committed May 10, 2024
1 parent bb3fa56 commit 183e757
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 2 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Unreleased
* Introduce `suspenders:factories` generator
* Introduce `suspenders:advisories` generator
* Introduce `suspenders:styles` generator
* Introduce `suspenders:jobs` generator

20230113.0 (January, 13, 2023)

Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ Also creates additional stylesheets if using PostCSS.
[cssbundling-rails]: https://github.com/rails/cssbundling-rails
[modern-normalize]: https://github.com/sindresorhus/modern-normalize


### Jobs

Installs [Sidekiq][] for background job processing and configures ActiveJob for job queueing.

`bin/rails g suspenders:jobs`

[Sidekiq]: https://github.com/sidekiq/sidekiq

## Contributing

See the [CONTRIBUTING] document.
Expand Down
36 changes: 36 additions & 0 deletions lib/generators/suspenders/jobs_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module Suspenders
module Generators
class JobsGenerator < Rails::Generators::Base
source_root File.expand_path("../../templates/active_job", __FILE__)
desc "Installs Sidekiq for background job processing."

def add_sidekiq_gem
gem "sidekiq"
Bundler.with_unbundled_env { run "bundle install" }
end

def initialize_active_job
copy_file "active_job.rb", "config/initializers/active_job.rb"
end

def configure_active_job
environment "config.active_job.queue_adapter = :sidekiq"
environment "config.action_mailer.deliver_later_queue_name = nil"
environment "config.action_mailbox.queues.routing = nil"
environment "config.active_storage.queues.analysis = nil"
environment "config.active_storage.queues.purge = nil"
environment "config.active_storage.queues.mirror = nil"
environment "config.active_job.queue_adapter = :inline", env: "test"
end

def configure_procfile
if Rails.root.join("Procfile.dev").exist?
append_to_file "Procfile.dev", "worker: bundle exec sidekiq"
else
say "Add default Procfile.dev"
create_file "Procfile.dev", "worker: bundle exec sidekiq"
end
end
end
end
end
14 changes: 14 additions & 0 deletions lib/generators/templates/active_job/active_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require "active_job/logging"
require "active_job/log_subscriber"

ActiveSupport::Notifications.unsubscribe("enqueue.active_job")

module ActiveJob
module Logging
class EnqueueLogSubscriber < LogSubscriber
define_method :enqueue, instance_method(:enqueue)
end
end
end

ActiveJob::Logging::EnqueueLogSubscriber.attach_to(:active_job)
122 changes: 122 additions & 0 deletions test/generators/suspenders/jobs_generator_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
require "test_helper"
require "generators/suspenders/jobs_generator"

module Suspenders
module Generators
class JobsGeneratorTest < Rails::Generators::TestCase
include Suspenders::TestHelpers

tests Suspenders::Generators::JobsGenerator
destination Rails.root
setup :prepare_destination
teardown :restore_destination

test "adds gems to Gemfile" do
expected_output = <<~RUBY
gem "sidekiq"
RUBY

run_generator

assert_file app_root("Gemfile") do |file|
assert_match(expected_output, file)
end
end

test "installs gems with Bundler" do
output = run_generator

assert_match(/bundle install/, output)
end

test "generator has a description" do
description = "Installs Sidekiq for background job processing."

assert_equal description, Suspenders::Generators::JobsGenerator.desc
end

test "configures ActiveJob logging" do
expected_configuration = <<~RUBY
require "active_job/logging"
require "active_job/log_subscriber"
ActiveSupport::Notifications.unsubscribe("enqueue.active_job")
module ActiveJob
module Logging
class EnqueueLogSubscriber < LogSubscriber
define_method :enqueue, instance_method(:enqueue)
end
end
end
ActiveJob::Logging::EnqueueLogSubscriber.attach_to(:active_job)
RUBY

run_generator

assert_file app_root("config/initializers/active_job.rb") do |file|
assert_equal(expected_configuration, file)
end
end

test "adds ActiveJob configuration to the application file" do
run_generator

assert_file app_root("config/application.rb") do |file|
assert_match(/config.active_job.queue_adapter = :sidekiq/, file)
assert_match(/config.action_mailer.deliver_later_queue_name = nil/, file)
assert_match(/config.action_mailbox.queues.routing = nil/, file)
assert_match(/config.active_storage.queues.analysis = nil/, file)
assert_match(/config.active_storage.queues.purge = nil/, file)
assert_match(/config.active_storage.queues.mirror = nil/, file)
end
end

test "adds ActiveJob configuration to the test environment file" do
run_generator

assert_file app_root("config/environments/test.rb") do |file|
assert_match(/config.active_job.queue_adapter = :inline/, file)
end
end

test "creates a Procfile.dev with Sidekiq configuration" do
run_generator

assert_file app_root("Procfile.dev") do |file|
assert_match(/worker: bundle exec sidekiq/, file)
end
end

test "adds Sidekiq configuration if procfile exists" do
proc_file = <<~TEXT
TEXT

File.write(app_root("Procfile.dev"), proc_file)

run_generator

assert_file app_root("Procfile.dev") do |file|
assert_match(/worker: bundle exec sidekiq/, file)
end
end

private

def prepare_destination
touch "Gemfile"
backup_file "config/application.rb"
backup_file "config/environments/test.rb"
end

def restore_destination
remove_file_if_exists "Gemfile"
remove_file_if_exists "config/initializers/active_job.rb"
remove_file_if_exists "Procfile.dev"
restore_file "config/application.rb"
restore_file "config/environments/test.rb"
end
end
end
end
3 changes: 1 addition & 2 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ class Application < Rails::Application
end

def backup_file(file)
FileUtils.mv app_root(file), app_root("#{file}.bak")
touch file
FileUtils.copy app_root(file), app_root("#{file}.bak")
end

def restore_file(file)
Expand Down

0 comments on commit 183e757

Please sign in to comment.