userver: Handling OS signals
Loading...
Searching...
No Matches
Handling OS signals

Default behavior

  • SIGINT - return trace
  • SIGTERM, SIGQUIT - shutdown service
  • SIGUSR1 - reopen file descriptors for log (logrotate)

User behavior

The component os_signals::ProcessorComponent stores os_signals::Processor and listens to SIGUSR1 and SIGUSR2 signals. You can use AddListener function to subscribe to these signals.

For example, we use signal SIGUSR1 in components::Logging for reopening files after executing logrotate.

Example

In the component constructor, pass context.Scopes() to os_signals::Processor::AddListener. The component's scopes register the listener after construction and unsubscribe it before destruction. For example, components::Logging registers its log rotation handler here:

auto& signals_processor = context.FindComponent<os_signals::ProcessorComponent>().Get();
signals_processor.AddListener(context.Scopes(), this, kName, os_signals::kSigUsr1, &Logging::OnLogRotate);
context.Scopes().Register([this] {
// Force logrotate just after signal subscription to be sure we haven't lost signals during the loading.
// If there were no signals, it's OK to re-open log files one more time.
OnLogRotate();
});