diff --git a/apps/transport/lib/transport_web/plugs/worker_healthcheck.ex b/apps/transport/lib/transport_web/plugs/worker_healthcheck.ex index 269386d259..a29a3ed422 100644 --- a/apps/transport/lib/transport_web/plugs/worker_healthcheck.ex +++ b/apps/transport/lib/transport_web/plugs/worker_healthcheck.ex @@ -8,8 +8,10 @@ defmodule TransportWeb.Plugs.WorkerHealthcheck do - the last attempt for Oban jobs - if the system is healthy - The system is considered healthy if the app was started recently or - if Oban attempted jobs recently. + The system is considered: + - healthy if the app was started recently or if Oban attempted jobs recently. + - not healthy: we don't respond to incoming HTTP requests by halting + the connection and expect our hosting provider to reboot the app. """ import Plug.Conn @@ -23,19 +25,22 @@ defmodule TransportWeb.Plugs.WorkerHealthcheck do if apply(mod, fun, []) do store_last_attempted_at_delay_metric() - status_code = if healthy_state?(), do: 200, else: 503 - conn - |> put_resp_content_type("text/plain") - |> send_resp(status_code, """ - UP (WORKER-ONLY) - App start time: #{app_start_datetime()} - App started recently?: #{app_started_recently?()} - Oban last attempt: #{oban_last_attempted_at()} - Oban attempted jobs recently?: #{oban_attempted_jobs_recently?()} - Healthy state?: #{healthy_state?()} - """) - |> halt() + if healthy_state?() do + conn + |> put_resp_content_type("text/plain") + |> send_resp(200, """ + UP (WORKER-ONLY) + App start time: #{app_start_datetime()} + App started recently?: #{app_started_recently?()} + Oban last attempt: #{oban_last_attempted_at()} + Oban attempted jobs recently?: #{oban_attempted_jobs_recently?()} + Healthy state?: #{healthy_state?()} + """) + |> halt() + else + conn |> halt() + end else conn end diff --git a/apps/transport/test/transport_web/plugs/worker_healthcheck_test.exs b/apps/transport/test/transport_web/plugs/worker_healthcheck_test.exs index ce6da59f9d..3f3547ba2e 100644 --- a/apps/transport/test/transport_web/plugs/worker_healthcheck_test.exs +++ b/apps/transport/test/transport_web/plugs/worker_healthcheck_test.exs @@ -121,7 +121,7 @@ defmodule TransportWeb.Plugs.WorkerHealthcheckTest do refute WorkerHealthcheck.oban_attempted_jobs_recently?() refute WorkerHealthcheck.healthy_state?() - assert conn |> WorkerHealthcheck.call(if: {__MODULE__, :plug_enabled?}) |> text_response(503) + assert %Plug.Conn{halted: true} = conn |> WorkerHealthcheck.call(if: {__MODULE__, :plug_enabled?}) end end