-
-
Notifications
You must be signed in to change notification settings - Fork 530
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce suspenders:jobs generator (#1147)
Ports the existing generator to Suspenders 3.0.
- Loading branch information
1 parent
bb3fa56
commit 183e757
Showing
6 changed files
with
183 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters