Skip to content

Commit

Permalink
Hot-fix: shutdown du worker si les jobs ne sont plus traités (#4405)
Browse files Browse the repository at this point in the history
* Shut down the worker app if jobs are not processed anymore

* Update doc to better reflect current doc

* mix format

* Make sure to return a conn (avoid RuntimeError)

* mix format
  • Loading branch information
thbar authored Dec 24, 2024
1 parent 3e3bdcb commit ac6a6fd
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions apps/transport/lib/transport_web/plugs/worker_healthcheck.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ defmodule TransportWeb.Plugs.WorkerHealthcheck do
if Oban attempted jobs recently.
"""
import Plug.Conn
require Logger

@app_start_waiting_delay {20, :minute}
@oban_max_delay_since_last_attempt {60, :minute}
Expand All @@ -25,17 +26,28 @@ defmodule TransportWeb.Plugs.WorkerHealthcheck do
store_last_attempted_at_delay_metric()
status_code = if healthy_state?(), do: 200, else: 503

conn =
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()

# NOTE: Clever Cloud monitoring will better pick stuff back up
# if the app is completely down.
if !healthy_state?() do
Logger.info("Hot-fix: shutting down!!!")
# "Asynchronously and carefully stops the Erlang runtime system."
System.stop()
end

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()
else
conn
end
Expand Down

0 comments on commit ac6a6fd

Please sign in to comment.