userver: Handling OS signals
⚠️ This is the documentation for an old userver version. Click here to switch to the latest version.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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

  1. Add in private field class
    os_signals::Subscriber signal_subscribe_
  2. In in the component's constructor, register a signal listener. For example, components::Logging uses the following code:
    Logging::Logging(const ComponentConfig& config, const ComponentContext& context)
    : signal_subscriber_(context.FindComponent<os_signals::ProcessorComponent>()
    .Get()
    .AddListener(this, kName, os_signals::kSigUsr1,
    &Logging::OnLogRotate))
  3. In the component's destructor, unsubscribe from the signal
    signal_subscriber_.Unsubscribe();