A signal watcher that can be used to shutdown an application.
Yama provides a signal watcher that can be used to shutdown an application1.
A signal watcher can be constructed to watch any number of signals and will
call any number of registered io.Closer
instances, when such signals occur; the
results of calling Close()
on the registered instances are ignored.
watcher, err := yama.NewWatcher(
yama.WatchingSignals(syscall.SIGINT, syscall.SIGTERM),
yama.WithTimeout(2*time.Second),
yama.WithClosers(server))
An application can wait fir the completion of the Closer
notifications by
calling the blocking method, Wait()
.
watcher.Wait()
Here, the caller will be blocked until one of the signals occur and all the
Closer
notifications have either completed or two seconds have elapsed since
the start of Closer
notifications; the timeout is set above by passing
yama.WithTimeout()
. Subsequent signals will not trigger Closer
notifications.
The application can programmatically trigger Closer
notifications by calling
watcher.Close()
If this is done, subsequent signals will not trigger Closer
notifications.
There are a few helper methods, FnAsCloser()
and ErrValFnAsCloser()
, that can
be used to wrap simple functions and functions that can return an error,
respectively, into instances that implement io.Closer
.