Skip to content

Commit 20ae548

Browse files
committed
Return system as down when the lock file is stale (older than 2 hours)
1 parent 6c511f2 commit 20ae548

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

config.ru

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ end
1515
use Rack::CommonLogger, logger
1616

1717
run_file = ENV["RUN_FILE"] || "#{__dir__}/run-rails-master-hook"
18+
lock_file = ENV["LOCK_FILE"]
1819
scheduled = <<EOS
1920
Rails master hook tasks scheduled:
2021
@@ -29,6 +30,24 @@ If a new stable tag is detected it also
2930
This needs typically a few minutes.
3031
EOS
3132

33+
# Helper class for lockfile checking
34+
class LockfileChecker
35+
def self.stale?(lock_file, logger)
36+
return false unless lock_file && File.exist?(lock_file)
37+
38+
file_age = Time.now - File.mtime(lock_file)
39+
stale = file_age > 7200 # 2 hours in seconds
40+
41+
if stale
42+
logger.warn "Lock file #{lock_file} is stale (age: #{(file_age / 60).round(1)} minutes)"
43+
else
44+
logger.debug "Lock file #{lock_file} age: #{(file_age / 60).round(1)} minutes"
45+
end
46+
47+
stale
48+
end
49+
end
50+
3251
map "/rails-master-hook" do
3352
run ->(env) do
3453
request_method = env["REQUEST_METHOD"]
@@ -47,6 +66,13 @@ end
4766

4867
map "/" do
4968
run ->(_env) do
50-
[200, {"Content-Type" => "text/plain", "Content-Length" => "4"}, ["PONG"]]
69+
# Check if lockfile is stale (older than 2 hours)
70+
if LockfileChecker.stale?(lock_file, logger)
71+
error_msg = "System down: Lock file has been present for more than 2 hours"
72+
logger.error error_msg
73+
[503, {"Content-Type" => "text/plain", "Content-Length" => error_msg.length.to_s}, [error_msg]]
74+
else
75+
[200, {"Content-Type" => "text/plain", "Content-Length" => "4"}, ["PONG"]]
76+
end
5177
end
5278
end

config/deploy/production.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
set :puma_service_unit_env_vars, %w[
1010
RUN_FILE=/home/rails/rails-master-hook/run-rails-master-hook
11+
LOCK_FILE=/home/rails/rails-master-hook/lock-rails-master-hook
1112
]
1213
set :puma_access_log, "journal"
1314
set :puma_error_log, "journal"

0 commit comments

Comments
 (0)