Skip to content

Commit

Permalink
WorkerHealthcheck : pas de réponse au lieu de 503
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoineAugusti committed Dec 23, 2024
1 parent 3e3bdcb commit 927719a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
33 changes: 19 additions & 14 deletions apps/transport/lib/transport_web/plugs/worker_healthcheck.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 927719a

Please sign in to comment.