699
///
700
/// # Note
701
///
702
- /// This method runs indefinitely until shutdown is requested (via Ctrl-C or stdin closing).
702
+ /// This method runs indefinitely until shutdown is requested (via signals or stdin closing).
703
pub async fn run(mut self) -> Result<()> {
704
info!("starting plugin runtime: {}", self.plugin_name);
705
706
- self.handle_ctr_c();
706
+ self.handle_shutdown_signals();
707
708
// Start chart registry if charts were registered
709
if let Some(registry) = self.chart_registry.take() {
736
Ok(())
737
}
738
739
- /// Setup Ctrl-C signal handler for graceful shutdown.
740
- fn handle_ctr_c(&self) {
739
+ /// Setup signal handlers for graceful shutdown
740
+ fn handle_shutdown_signals(&self) {
741
let shutdown_token = self.shutdown_token.clone();
742
743
tokio::spawn(async move {
744
- match tokio::signal::ctrl_c().await {
745
- Ok(()) => {
746
- info!("received ctrl-c signal, initiating graceful shutdown");
747
- shutdown_token.cancel();
748
- }
749
- Err(e) => {
750
- error!("failed to listen for Ctrl-C signal: {}", e);
751
- }
744
+ match wait_for_shutdown_signal().await {
745
+ Ok(()) => info!("received shutdown signal, initiating graceful shutdown"),
746
+ Err(e) => error!(
747
+ "failed to wait for shutdown signal: {}, initiating shutdown",
748
+ e
749
+ ),
750
}
751
+ shutdown_token.cancel();
752
});
753
}
754
+}
755
+
756
+/// Waits for a shutdown signal (SIGINT or SIGTERM on Unix, SIGINT on other platforms).
757
+async fn wait_for_shutdown_signal() -> std::io::Result<()> {
758
+ #[cfg(unix)]
759
+ {
760
+ use tokio::signal::unix::{SignalKind, signal};
761
+
762
+ let mut sigterm = signal(SignalKind::terminate())?;
763
+
764
+ tokio::select! {
765
+ result = tokio::signal::ctrl_c() => result,
766
+ _ = sigterm.recv() => Ok(()),
767
+ }
768
+ }
769
+
770
+ #[cfg(not(unix))]
771
+ {
772
+ tokio::signal::ctrl_c().await
773
+ }
774
+}
775
776
+impl<R: AsyncRead + Unpin + Send, W: AsyncWrite + Unpin + Send> PluginRuntime<R, W> {
777
/// Declare all registered functions to Netdata.
778
///
779
/// Sends a [`FunctionDeclaration`] message for each registered handler,