Skip to content

Commit 5c4a7a7

Browse files
authored
Merge pull request rails#50223 from bensheldon/runner_conditional_executor
Add runner script option to disable Executor wrap
2 parents c80fa6a + a9c4699 commit 5c4a7a7

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

railties/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
* Use `bin/rails runner --skip-executor` option to not wrap the runner script
2+
with an Executor.
3+
4+
*Ben Sheldon*
5+
16
* Fix isolated engines to take `ActiveRecord::Base.table_name_prefix` into consideration.
27
This will allow for engine defined models, such as inside Active Storage, to respect
38
Active Record table name prefix configuration.

railties/lib/rails/commands/runner/runner_command.rb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ module Command
77
class RunnerCommand < Base # :nodoc:
88
include EnvironmentArgument
99

10+
class_option :skip_executor, type: :boolean, aliases: "-w", desc: "Don't wrap with Rails Executor", default: false
11+
1012
no_commands do
1113
def help(command_name = nil, *)
1214
super
@@ -30,19 +32,20 @@ def perform(code_or_file = nil, *command_argv)
3032

3133
ARGV.replace(command_argv)
3234

35+
wrap_with_executor = !options[:skip_executor]
3336
if code_or_file == "-"
34-
Rails.application.executor.wrap(source: "application.runner.railties") do
37+
conditional_executor(wrap_with_executor, source: "application.runner.railties") do
3538
eval($stdin.read, TOPLEVEL_BINDING, "stdin")
3639
end
3740
elsif File.exist?(code_or_file)
3841
expanded_file_path = File.expand_path code_or_file
3942
$0 = expanded_file_path
40-
Rails.application.executor.wrap(source: "application.runner.railties") do
43+
conditional_executor(wrap_with_executor, source: "application.runner.railties") do
4144
Kernel.load expanded_file_path
4245
end
4346
else
4447
begin
45-
Rails.application.executor.wrap(source: "application.runner.railties") do
48+
conditional_executor(wrap_with_executor, source: "application.runner.railties") do
4649
eval(code_or_file, TOPLEVEL_BINDING, __FILE__, __LINE__)
4750
end
4851
rescue SyntaxError, NameError => e
@@ -62,6 +65,14 @@ def perform(code_or_file = nil, *command_argv)
6265
end
6366

6467
private
68+
def conditional_executor(enabled, **args, &block)
69+
if enabled
70+
Rails.application.executor.wrap(**args, &block)
71+
else
72+
yield
73+
end
74+
end
75+
6576
def looks_like_a_file_path?(code_or_file)
6677
code_or_file.ends_with?(".rb")
6778
end

railties/test/commands/runner_test.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ def test_rails_runner_with_ruby_code
4444
OUTPUT
4545
end
4646

47+
def test_rails_runner_with_conditional_executor
48+
assert_equal <<~OUTPUT, run_runner_command("puts Rails.application.executor.active?", allow_failure: true)
49+
true
50+
OUTPUT
51+
52+
assert_equal <<~OUTPUT, run_runner_command("--skip-executor", "puts Rails.application.executor.active?", allow_failure: true)
53+
false
54+
OUTPUT
55+
end
56+
4757
def test_rails_runner_with_syntax_error_in_ruby_code
4858
command_output = run_runner_command("This is not ruby code", allow_failure: true)
4959

@@ -61,7 +71,7 @@ def test_rails_runner_with_name_error_in_ruby_code
6171
end
6272

6373
private
64-
def run_runner_command(argument, allow_failure: false)
65-
rails "runner", argument, allow_failure: allow_failure
74+
def run_runner_command(*arguments, allow_failure: false)
75+
rails "runner", *arguments, allow_failure: allow_failure
6676
end
6777
end

0 commit comments

Comments
 (0)