1515use Rack ::CommonLogger , logger
1616
1717run_file = ENV [ "RUN_FILE" ] || "#{ __dir__ } /run-rails-master-hook"
18+ lock_file = ENV [ "LOCK_FILE" ]
1819scheduled = <<EOS
1920Rails master hook tasks scheduled:
2021
@@ -29,6 +30,24 @@ If a new stable tag is detected it also
2930This needs typically a few minutes.
3031EOS
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+
3251map "/rails-master-hook" do
3352 run -> ( env ) do
3453 request_method = env [ "REQUEST_METHOD" ]
4766
4867map "/" 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
5278end
0 commit comments