Skip to content

Instantly share code, notes, and snippets.

@rayrayzayzay
Last active May 6, 2023 19:59
Show Gist options
  • Save rayrayzayzay/007cf95da6543bd38ad5d35674aa81a6 to your computer and use it in GitHub Desktop.
Save rayrayzayzay/007cf95da6543bd38ad5d35674aa81a6 to your computer and use it in GitHub Desktop.
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