Last active
May 6, 2023 19:59
-
-
Save rayrayzayzay/007cf95da6543bd38ad5d35674aa81a6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MyApp.GracefulShutdownHandler do | |
use GenServer | |
require Logger | |
def start_link(opts \\\\ []) do | |
GenServer.start_link(__MODULE__, opts, name: __MODULE__) | |
end | |
@impl GenServer | |
def init(_init_arg) do | |
# We need to trap exits so that we receive the `terminate/2` callback during | |
# a graceful shutdown | |
Process.flag(:trap_exit, true) | |
{:ok, []} | |
end | |
# NOTE: We cannot guarantee that this will run on every shutdown, it will only | |
# get run on graceful shutdowns such as via a SIGTERM | |
@impl GenServer | |
def terminate(_reason, _state) do | |
Logger.info("Graceful Shutdown occurring") | |
:ok | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment