signals.rs

Overview

This file provides the implementation for handling operating system signals within the application. It listens for specific signals such as SIGHUP, SIGINT, and SIGTERM and executes appropriate actions when these signals are received. The primary purpose is to ensure graceful shutdowns and runtime behavior adjustments, such as database rotation, by sending commands to worker threads. Signal handling is performed asynchronously in a dedicated thread to avoid blocking the main execution flow.

Function

init_signals

pub fn init_signals(tx: mpsc::Sender<WorkerCommand>) -> io::Result<JoinHandle<()>>

Purpose

Initializes the signal handling thread which listens for predefined Unix signals and sends corresponding commands to the worker via a message-passing channel.

Parameters

Returns

Usage Example

use std::sync::mpsc;
use crate::block_subscriber::WorkerCommand;

let (tx, rx) = mpsc::channel();
let signal_thread = signals::init_signals(tx).expect("Failed to initialize signals");

Behavior and Implementation Details

Interaction with Other Components

Important Implementation Details

Mermaid Diagram

flowchart TD
InitSignals["init_signals(tx)"]
SignalsIterator["signal_hook::iterator::Signals"]
SignalHandlerThread["Signal Handler Thread"]
InitSignals --> SignalsIterator
InitSignals --> SignalHandlerThread
SignalHandlerThread -->|Receives SIGTERM| SendShutdown["Send WorkerCommand::Shutdown"]
SignalHandlerThread -->|Receives SIGINT| ExitProcess1["exit(1)"]
SignalHandlerThread -->|Receives SIGHUP| SendRotateDb["Send WorkerCommand::RotateDb"]
SignalHandlerThread -->|Receives Other| LogWarning["Log unhandled signal"]
SendShutdown --> Sleep2s["Sleep 2 seconds"]
Sleep2s --> ExitProcess1

The diagram illustrates the flow in init_signals: initialization of signals iterator, spawning the signal handler thread, and the thread's reactions to various signals, including commands sent to the worker and process termination.


For more on thread management and message passing, see [threading and channels](/threading_channels). For details on worker commands and their processing, consult [block_subscriber](/block_subscriber).