Sidecar.GracefulShutdown (spawn v2.0.0-RC14)
View SourceThis module was copied and adapted from this project https://github.com/straw-hat-team/beam-monorepo
Catches SIGTERM
signal to gracefully stop the system.
By default, a SIGTERM
signal triggers a System.stop/0
. When running in a Kubernetes-managed cluster or an
infrastructure alike, the nodes will be joining and leaving the cluster when auto-scaling is enabled or when deploying
new version of the application.
You should let the system to gracefully stop in combination with readiness probe you can make sure that the system is
ready to stop without a relying on a SIGKILL
signal.
You can read more about this at Termination of Pods.
When a SIGTERM
is received, the handler enters "connection draining mode", during which get_status/0
starts
returning :draining
status, and then, after a delay, the handler calls System.stop/1
, carefully halting the Erlang VM
and starts returning :stopping
status.
Summary
Functions
Returns a specification to start Sidecar.GracefulShutdown
under a supervisor.
See the "Child specification" section in the Supervisor module for more detailed information.
Returns the status of the system.
Returns true if the system is in the :running
status.
Types
@type opts() :: [ shutdown_delay_ms: non_neg_integer(), init_stop?: boolean(), notify_pid: pid() ]
Configuration options for the server.
shutdown_delay_ms
: milliseconds before draining the VM afterSIGTERM
. milliseconds between start of connection draining and ordered shut-down usingSystem.stop/0
.notify_pid
: process ID to notify when the server started draining.init_stop?
: whether to call:System.stop/1
when aSIGTERM
is received. Useful for testing since we don't want to stop the VM when testing. Be careful when using this option since it should always betrue
in production.
@type status() :: :running | :draining | :stopping
The state of system.
:draining
- the node is draining and will be shutdown after the delay.:stopping
- the node is shutting down.:running
- the node is accepting work.
Functions
@spec child_spec(opts :: opts()) :: Supervisor.child_spec()
Returns a specification to start Sidecar.GracefulShutdown
under a supervisor.
See the "Child specification" section in the Supervisor module for more detailed information.
@spec get_status() :: status()
Returns the status of the system.
@spec running?() :: boolean()
Returns true if the system is in the :running
status.