[][src]Crate simple_on_shutdown

This crate consists of a convenient macro to specify on shutdown callbacks called on_shutdown. It takes code that should be executed when your program exits (gracefully).

Internally it creates a closure that gets executed when the context gets dropped, i.e. when main() exits. There is also on_shutdown_move available in case the closure needs to capture vars, like an Arc<>.

In theory this macro can be used everywhere where the context gets dropped. But it has a nice expressive name so that one exactly knows what it should achieve in code. A good example is the main() function in an actix-web-Server. For example you want to log to a file when the server was shut down.

There is no guarantee that this gets executed during "non-regular" shutdown scenarios, like when receiving CTRL+C / SIGINT / SIGTERM. This depends on whether your application properly handles signals and if the operating system gives your application time before it gets totally killed/stopped.

IMPORTANT: Use this on the top level of your main() or whatever your current runtimes main function is! The code gets executed when the context it lives in gets dropped. This macro can be called multiple times (at least with stable Rust 1.48.0) without problems.

This crate uses the log crate on the debug level.

Macros

on_shutdown

This crate consists of a convenient macro to specify on shutdown callbacks called on_shutdown. It takes code that should be executed when your program exits (gracefully).

on_shutdown_move

Like on_shutdown but moves all variables into the created closure.

Structs

OnShutdownCallback

Simple type that holds a closure (callback). The closure gets invoked during drop(). This works also fine with applications that do gracefully shutdown via signals, like SIGTERM.