From fecad19b913e77833a52a6a8a8f258e80c74df55 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Sun, 21 Jun 2020 18:34:59 +0200 Subject: [PATCH] add maintenance mode --- app/controllers/ping_controller.rb | 11 ++++++++--- spec/controllers/ping_controller_spec.rb | 10 ++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/app/controllers/ping_controller.rb b/app/controllers/ping_controller.rb index 2ac9e20ff..08f439aec 100644 --- a/app/controllers/ping_controller.rb +++ b/app/controllers/ping_controller.rb @@ -1,11 +1,16 @@ class PingController < ApplicationController def index Rails.logger.silence do - if (ActiveRecord::Base.connected?) - head :ok, content_type: "application/json" + status_code = if File.file?(Rails.root.join("maintenance")) + # See https://cbonte.github.io/haproxy-dconv/2.0/configuration.html#4.2-http-check%20disable-on-404 + :not_found + elsif (ActiveRecord::Base.connected?) + :ok else - head :internal_server_error, content_type: "application/json" + :internal_server_error end + + head status_code, content_type: "application/json" end end end diff --git a/spec/controllers/ping_controller_spec.rb b/spec/controllers/ping_controller_spec.rb index 0649048f7..a288b69c1 100644 --- a/spec/controllers/ping_controller_spec.rb +++ b/spec/controllers/ping_controller_spec.rb @@ -11,5 +11,15 @@ describe PingController, type: :controller do it { expect(subject.status).to eq 500 } end + + context 'when a maintenance file is present' do + before do + allow(File).to receive(:file?).and_return(true) + end + + it 'tells HAProxy that the app is in maintenance, but will be available again soon' do + expect(subject.status).to eq 404 + end + end end end