Skip to content

Commit 946f6fe

Browse files
committed
Add bootchecking for rails applications
Adds "RailsBootcheck" to the Rails buildpack. RailsBootcheck attempts to load your rails application and will fail the the build of your application if it is unable to load. This can help prevent scenarios where you can accidentally ship a version of your application that cannot boot properly. Which will lead to errors as the application is released and traffic is routed to it. You can opt-in to this feature via the `HEROKU_RAILS_BOOTCHECK_ENABLE` variable, new rails applications will have this variable set by default. You can opt-out of this feature via the `HEROKU_RAILS_BOOTCHECK_DISABLE` variable. Closes heroku#1120
1 parent bcae21a commit 946f6fe

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# frozen_string_literal: true
2+
3+
class LanguagePack::Helpers::RailsBootcheck
4+
include LanguagePack::ShellHelpers
5+
6+
def initialize(timeout = 65)
7+
@timeout = timeout
8+
end
9+
10+
def call
11+
return unless opted_in? && !opted_out?
12+
13+
topic("Bootchecking rails application")
14+
15+
process = ProcessSpawn.new(
16+
"rails runner 'puts Rails.env'",
17+
user_env: true,
18+
timeout: @timeout,
19+
file: "./.heroku/ruby/compile/rails_bootcheck.txt"
20+
)
21+
22+
if process.timeout?
23+
failure("timeout", process.output)
24+
elsif !process.success?
25+
failure("failure", process.output)
26+
end
27+
end
28+
29+
private
30+
31+
def failure(type, output)
32+
message = String.new("Bootchecking rails application #{type}\n")
33+
message << "set HEROKU_RAILS_BOOTCHECK_DISABLE=1 to disable this feature\n"
34+
35+
if !output.empty?
36+
message << "\n"
37+
message << output
38+
end
39+
40+
error(message)
41+
end
42+
43+
def opted_in?
44+
env("HEROKU_RAILS_BOOTCHECK_ENABLE")
45+
end
46+
47+
def opted_out?
48+
env("HEROKU_RAILS_BOOTCHECK_DISABLE")
49+
end
50+
end

lib/language_pack/rails2.rb

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require "fileutils"
22
require "language_pack"
33
require "language_pack/rack"
4+
require "language_pack/helpers/rails_bootcheck"
45

56
# Rails 2 Language Pack. This is for any Rails 2.x apps.
67
class LanguagePack::Rails2 < LanguagePack::Ruby
@@ -19,6 +20,7 @@ def self.use?
1920
def initialize(*args)
2021
super(*args)
2122
@rails_runner = LanguagePack::Helpers::RailsRunner.new
23+
@bootcheck = LanguagePack::Helpers::RailsBootcheck.new
2224
end
2325

2426
def name
@@ -29,7 +31,7 @@ def default_env_vars
2931
{
3032
"RAILS_ENV" => "production",
3133
"RACK_ENV" => "production"
32-
}
34+
}.merge(bootcheck_env_vars)
3335
end
3436

3537
def default_config_vars
@@ -60,6 +62,7 @@ def compile
6062
instrument "rails2.compile" do
6163
install_plugins
6264
super
65+
@bootcheck.call
6366
end
6467
end
6568

@@ -106,4 +109,11 @@ def setup_profiled(*args)
106109
end
107110
end
108111

112+
def bootcheck_env_vars
113+
value = env("HEROKU_RAILS_BOOTCHECK_ENABLE")
114+
115+
return {} unless value || new_app?
116+
117+
{ "HEROKU_RAILS_BOOTCHECK_ENABLE" => value || "1" }
118+
end
109119
end

0 commit comments

Comments
 (0)