Skip to content

Commit cb5dff8

Browse files
authored
Introduce suspenders:jobs generator (#1147)
Ports the existing generator to Suspenders 3.0.
1 parent 6bed72e commit cb5dff8

File tree

6 files changed

+183
-2
lines changed

6 files changed

+183
-2
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Unreleased
66
* Introduce `suspenders:factories` generator
77
* Introduce `suspenders:advisories` generator
88
* Introduce `suspenders:styles` generator
9+
* Introduce `suspenders:jobs` generator
910

1011
20230113.0 (January, 13, 2023)
1112

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@ Also creates additional stylesheets if using PostCSS.
8787
[cssbundling-rails]: https://github.com/rails/cssbundling-rails
8888
[modern-normalize]: https://github.com/sindresorhus/modern-normalize
8989

90+
91+
### Jobs
92+
93+
Installs [Sidekiq][] for background job processing and configures ActiveJob for job queueing.
94+
95+
`bin/rails g suspenders:jobs`
96+
97+
[Sidekiq]: https://github.com/sidekiq/sidekiq
98+
9099
## Contributing
91100

92101
See the [CONTRIBUTING] document.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module Suspenders
2+
module Generators
3+
class JobsGenerator < Rails::Generators::Base
4+
source_root File.expand_path("../../templates/active_job", __FILE__)
5+
desc "Installs Sidekiq for background job processing."
6+
7+
def add_sidekiq_gem
8+
gem "sidekiq"
9+
Bundler.with_unbundled_env { run "bundle install" }
10+
end
11+
12+
def initialize_active_job
13+
copy_file "active_job.rb", "config/initializers/active_job.rb"
14+
end
15+
16+
def configure_active_job
17+
environment "config.active_job.queue_adapter = :sidekiq"
18+
environment "config.action_mailer.deliver_later_queue_name = nil"
19+
environment "config.action_mailbox.queues.routing = nil"
20+
environment "config.active_storage.queues.analysis = nil"
21+
environment "config.active_storage.queues.purge = nil"
22+
environment "config.active_storage.queues.mirror = nil"
23+
environment "config.active_job.queue_adapter = :inline", env: "test"
24+
end
25+
26+
def configure_procfile
27+
if Rails.root.join("Procfile.dev").exist?
28+
append_to_file "Procfile.dev", "worker: bundle exec sidekiq"
29+
else
30+
say "Add default Procfile.dev"
31+
create_file "Procfile.dev", "worker: bundle exec sidekiq"
32+
end
33+
end
34+
end
35+
end
36+
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require "active_job/logging"
2+
require "active_job/log_subscriber"
3+
4+
ActiveSupport::Notifications.unsubscribe("enqueue.active_job")
5+
6+
module ActiveJob
7+
module Logging
8+
class EnqueueLogSubscriber < LogSubscriber
9+
define_method :enqueue, instance_method(:enqueue)
10+
end
11+
end
12+
end
13+
14+
ActiveJob::Logging::EnqueueLogSubscriber.attach_to(:active_job)
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
require "test_helper"
2+
require "generators/suspenders/jobs_generator"
3+
4+
module Suspenders
5+
module Generators
6+
class JobsGeneratorTest < Rails::Generators::TestCase
7+
include Suspenders::TestHelpers
8+
9+
tests Suspenders::Generators::JobsGenerator
10+
destination Rails.root
11+
setup :prepare_destination
12+
teardown :restore_destination
13+
14+
test "adds gems to Gemfile" do
15+
expected_output = <<~RUBY
16+
gem "sidekiq"
17+
RUBY
18+
19+
run_generator
20+
21+
assert_file app_root("Gemfile") do |file|
22+
assert_match(expected_output, file)
23+
end
24+
end
25+
26+
test "installs gems with Bundler" do
27+
output = run_generator
28+
29+
assert_match(/bundle install/, output)
30+
end
31+
32+
test "generator has a description" do
33+
description = "Installs Sidekiq for background job processing."
34+
35+
assert_equal description, Suspenders::Generators::JobsGenerator.desc
36+
end
37+
38+
test "configures ActiveJob logging" do
39+
expected_configuration = <<~RUBY
40+
require "active_job/logging"
41+
require "active_job/log_subscriber"
42+
43+
ActiveSupport::Notifications.unsubscribe("enqueue.active_job")
44+
45+
module ActiveJob
46+
module Logging
47+
class EnqueueLogSubscriber < LogSubscriber
48+
define_method :enqueue, instance_method(:enqueue)
49+
end
50+
end
51+
end
52+
53+
ActiveJob::Logging::EnqueueLogSubscriber.attach_to(:active_job)
54+
RUBY
55+
56+
run_generator
57+
58+
assert_file app_root("config/initializers/active_job.rb") do |file|
59+
assert_equal(expected_configuration, file)
60+
end
61+
end
62+
63+
test "adds ActiveJob configuration to the application file" do
64+
run_generator
65+
66+
assert_file app_root("config/application.rb") do |file|
67+
assert_match(/config.active_job.queue_adapter = :sidekiq/, file)
68+
assert_match(/config.action_mailer.deliver_later_queue_name = nil/, file)
69+
assert_match(/config.action_mailbox.queues.routing = nil/, file)
70+
assert_match(/config.active_storage.queues.analysis = nil/, file)
71+
assert_match(/config.active_storage.queues.purge = nil/, file)
72+
assert_match(/config.active_storage.queues.mirror = nil/, file)
73+
end
74+
end
75+
76+
test "adds ActiveJob configuration to the test environment file" do
77+
run_generator
78+
79+
assert_file app_root("config/environments/test.rb") do |file|
80+
assert_match(/config.active_job.queue_adapter = :inline/, file)
81+
end
82+
end
83+
84+
test "creates a Procfile.dev with Sidekiq configuration" do
85+
run_generator
86+
87+
assert_file app_root("Procfile.dev") do |file|
88+
assert_match(/worker: bundle exec sidekiq/, file)
89+
end
90+
end
91+
92+
test "adds Sidekiq configuration if procfile exists" do
93+
proc_file = <<~TEXT
94+
TEXT
95+
96+
File.write(app_root("Procfile.dev"), proc_file)
97+
98+
run_generator
99+
100+
assert_file app_root("Procfile.dev") do |file|
101+
assert_match(/worker: bundle exec sidekiq/, file)
102+
end
103+
end
104+
105+
private
106+
107+
def prepare_destination
108+
touch "Gemfile"
109+
backup_file "config/application.rb"
110+
backup_file "config/environments/test.rb"
111+
end
112+
113+
def restore_destination
114+
remove_file_if_exists "Gemfile"
115+
remove_file_if_exists "config/initializers/active_job.rb"
116+
remove_file_if_exists "Procfile.dev"
117+
restore_file "config/application.rb"
118+
restore_file "config/environments/test.rb"
119+
end
120+
end
121+
end
122+
end

test/test_helper.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ class Application < Rails::Application
7777
end
7878

7979
def backup_file(file)
80-
FileUtils.mv app_root(file), app_root("#{file}.bak")
81-
touch file
80+
FileUtils.copy app_root(file), app_root("#{file}.bak")
8281
end
8382

8483
def restore_file(file)

0 commit comments

Comments
 (0)