proc-daemon 1.0.0

A foundational framework for building high-performance, resilient daemon services in Rust. Handles all the boilerplate for signal handling, graceful shutdown, logging, and configuration.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
//! Core daemon implementation with builder pattern.
//!
//! This module provides the main `Daemon` struct and `DaemonBuilder` for creating
//! high-performance, resilient daemon services. The builder pattern allows for
//! flexible configuration while maintaining zero-copy performance characteristics.

use std::future::Future;
use std::io::{self, Write};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::Instant;
use tracing::{debug, error, info, instrument, warn};

use crate::config::Config;
use crate::error::{Error, Result};
use crate::shutdown::{ShutdownCoordinator, ShutdownReason};
use crate::signal::{ConfigurableSignalHandler, SignalConfig, SignalHandler};
use crate::subsystem::{Subsystem, SubsystemId, SubsystemManager};

#[cfg(feature = "config-watch")]
use arc_swap::ArcSwap;
#[cfg(feature = "config-watch")]
use notify::RecommendedWatcher;

/// Type alias for subsystem registration function
type SubsystemRegistrationFn = Box<dyn FnOnce(&SubsystemManager) -> SubsystemId + Send + 'static>;

/// Main daemon instance that coordinates all subsystems and handles lifecycle.
pub struct Daemon {
    /// Configuration
    config: Arc<Config>,
    /// Live-updating configuration snapshot (when config-watch is enabled)
    #[cfg(feature = "config-watch")]
    config_shared: Arc<ArcSwap<Config>>,
    /// Shutdown coordination
    shutdown_coordinator: ShutdownCoordinator,
    /// Subsystem management
    subsystem_manager: SubsystemManager,
    /// Signal handling
    signal_handler: Option<SignalHandlerKind>,
    /// Keep the config watcher alive (when enabled)
    #[cfg(feature = "config-watch")]
    _config_watcher: Option<RecommendedWatcher>,
    /// Start time
    started_at: Option<Instant>,
}

struct PidFileGuard {
    path: PathBuf,
}

impl PidFileGuard {
    fn create(path: &Path) -> Result<Self> {
        let pid = std::process::id();
        let mut file = std::fs::File::create(path).map_err(|e| {
            Error::io_with_source(
                format!("Failed to create PID file at {}", path.display()),
                e,
            )
        })?;
        writeln!(file, "{pid}").map_err(|e| {
            Error::io_with_source(format!("Failed to write PID file at {}", path.display()), e)
        })?;
        Ok(Self {
            path: path.to_path_buf(),
        })
    }
}

impl Drop for PidFileGuard {
    fn drop(&mut self) {
        let _ = std::fs::remove_file(&self.path);
    }
}

struct RotatingFileWriterInner {
    file: std::fs::File,
    path: PathBuf,
    max_size: u64,
    max_files: u32,
    size: u64,
}

#[derive(Clone)]
struct RotatingFileWriter {
    inner: Arc<std::sync::Mutex<RotatingFileWriterInner>>,
}

impl RotatingFileWriter {
    fn new(path: PathBuf, max_size: Option<u64>, max_files: Option<u32>) -> io::Result<Self> {
        let file = std::fs::OpenOptions::new()
            .create(true)
            .append(true)
            .open(&path)?;
        let size = file.metadata().map(|m| m.len()).unwrap_or(0);

        let max_size = max_size.unwrap_or(u64::MAX);
        let max_files = max_files.unwrap_or(0);

        Ok(Self {
            inner: Arc::new(std::sync::Mutex::new(RotatingFileWriterInner {
                file,
                path,
                max_size,
                max_files,
                size,
            })),
        })
    }

    fn rotate_locked(inner: &mut RotatingFileWriterInner) -> io::Result<()> {
        if inner.max_files == 0 {
            return Ok(());
        }

        for idx in (1..=inner.max_files).rev() {
            let from = Self::rotated_path(&inner.path, idx - 1);
            let to = Self::rotated_path(&inner.path, idx);
            if from.exists() {
                let _ = std::fs::remove_file(&to);
                std::fs::rename(&from, &to)?;
            }
        }
        inner.file = std::fs::OpenOptions::new()
            .create(true)
            .append(true)
            .open(&inner.path)?;
        inner.size = 0;
        Ok(())
    }

    fn rotated_path(path: &Path, idx: u32) -> PathBuf {
        if idx == 0 {
            return path.to_path_buf();
        }
        PathBuf::from(format!("{}.{}", path.display(), idx))
    }
}

struct RotatingFileWriterGuard {
    inner: Arc<std::sync::Mutex<RotatingFileWriterInner>>,
}

impl io::Write for RotatingFileWriterGuard {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        let mut inner = self
            .inner
            .lock()
            .map_err(|_| io::Error::other("rotating log writer mutex poisoned"))?;

        if inner.size.saturating_add(buf.len() as u64) > inner.max_size {
            RotatingFileWriter::rotate_locked(&mut inner)?;
        }

        let written = inner.file.write(buf)?;
        inner.size = inner.size.saturating_add(written as u64);
        drop(inner);
        Ok(written)
    }

    fn flush(&mut self) -> io::Result<()> {
        let mut inner = self
            .inner
            .lock()
            .map_err(|_| io::Error::other("rotating log writer mutex poisoned"))?;
        let result = inner.file.flush();
        drop(inner);
        result
    }
}

impl<'a> tracing_subscriber::fmt::writer::MakeWriter<'a> for RotatingFileWriter {
    type Writer = RotatingFileWriterGuard;

    fn make_writer(&'a self) -> Self::Writer {
        RotatingFileWriterGuard {
            inner: Arc::clone(&self.inner),
        }
    }
}

#[derive(Clone)]
enum SignalHandlerKind {
    Default(Arc<SignalHandler>),
    Configurable(Arc<ConfigurableSignalHandler>),
}

impl SignalHandlerKind {
    #[allow(dead_code)]
    async fn handle_signals(&self) -> Result<()> {
        match self {
            Self::Default(handler) => handler.handle_signals().await,
            Self::Configurable(handler) => handler.handle_signals().await,
        }
    }

    fn stop(&self) {
        match self {
            Self::Default(handler) => handler.stop(),
            Self::Configurable(handler) => handler.stop(),
        }
    }
}

impl Daemon {
    /// Create a new daemon builder with the provided configuration.
    #[must_use]
    pub fn builder(config: Config) -> DaemonBuilder {
        DaemonBuilder::new(config)
    }

    /// Create a new daemon with default configuration.
    ///
    /// # Errors
    ///
    /// Will return an error if the default configuration is invalid.
    pub fn with_defaults() -> Result<DaemonBuilder> {
        let config = Config::new()?;
        Ok(Self::builder(config))
    }

    /// Run the daemon until shutdown is requested.
    /// This is the main entry point that starts all subsystems and waits for shutdown.
    ///
    /// # Errors
    ///
    /// Returns an error if logging initialization fails, configuration validation fails,
    /// subsystem startup fails, or if there is an error during the shutdown sequence.
    #[instrument(skip(self), fields(daemon_name = %self.config.name))]
    pub async fn run(mut self) -> Result<()> {
        info!(daemon_name = %self.config.name, "Starting daemon");
        self.started_at = Some(Instant::now());

        // Apply working directory before initializing logging or PID files
        if let Some(work_dir) = &self.config.work_dir {
            std::env::set_current_dir(work_dir).map_err(|e| {
                Error::io_with_source(
                    format!("Failed to set working directory to {}", work_dir.display()),
                    e,
                )
            })?;
        }

        // Write PID file if configured
        let _pid_guard = if let Some(pid_file) = &self.config.pid_file {
            Some(PidFileGuard::create(pid_file)?)
        } else {
            None
        };

        // Initialize logging
        self.init_logging()?;

        // Apply optional scheduler hints (no-op placeholders for future tuning)
        #[cfg(feature = "scheduler-hints")]
        {
            crate::scheduler::apply_process_hints(&self.config);
            crate::scheduler::apply_runtime_hints();
        }

        // Start all subsystems
        if let Err(e) = self.subsystem_manager.start_all().await {
            error!(error = %e, "Failed to start all subsystems");
            return Err(e);
        }

        // Start signal handling in the background
        // Only spawn when a supported async runtime is enabled.
        #[cfg(any(feature = "tokio", feature = "async-std"))]
        let signal_task = self.signal_handler.as_ref().map(|signal_handler| {
            let handler = signal_handler.clone();
            Self::spawn_signal_handler(handler)
        });

        #[cfg(not(any(feature = "tokio", feature = "async-std")))]
        let _signal_task: Option<()> = None;

        // Wait for shutdown to be initiated
        info!("Daemon started successfully, waiting for shutdown signal");

        // Main daemon loop - wait for shutdown
        loop {
            if self.shutdown_coordinator.is_shutdown() {
                break;
            }

            // Check subsystem health periodically
            if self.config.monitoring.health_checks {
                let health_results = self.subsystem_manager.run_health_checks();
                // Early exit on first unhealthy subsystem to avoid allocations
                let mut found_unhealthy = false;
                for (id, name, healthy) in &health_results {
                    if !healthy {
                        if !found_unhealthy {
                            warn!("Unhealthy subsystems detected:");
                            found_unhealthy = true;
                        }
                        warn!(subsystem_id = id, subsystem_name = %name, "Unhealthy subsystem");
                        // Could implement auto-restart logic here
                    }
                }
            }

            // Sleep for a short interval
            #[cfg(feature = "tokio")]
            tokio::time::sleep(self.config.health_check_interval()).await;

            #[cfg(all(feature = "async-std", not(feature = "tokio")))]
            async_std::task::sleep(self.config.health_check_interval()).await;

            #[cfg(not(any(feature = "tokio", feature = "async-std")))]
            std::thread::sleep(self.config.health_check_interval());
        }

        // Graceful shutdown sequence
        info!("Shutdown initiated, beginning graceful shutdown");

        // Stop signal handling
        if let Some(signal_handler) = &self.signal_handler {
            signal_handler.stop();
        }

        // Wait for signal handler task to complete
        #[cfg(any(feature = "tokio", feature = "async-std"))]
        if let Some(task) = signal_task {
            #[cfg(feature = "tokio")]
            {
                if let Err(e) = task.await {
                    warn!(error = %e, "Signal handler task failed");
                }
            }

            #[cfg(all(feature = "async-std", not(feature = "tokio")))]
            {
                if let Err(e) = task.await {
                    warn!(error = %e, "Signal handler task failed");
                }
            }
        }

        // Stop all subsystems
        if let Err(e) = self.subsystem_manager.stop_all().await {
            error!(error = %e, "Failed to stop all subsystems gracefully");
        }

        // Wait for graceful shutdown with timeout
        if let Err(e) = self.shutdown_coordinator.wait_for_shutdown().await {
            warn!(error = %e, "Graceful shutdown timeout exceeded");

            // Wait for force shutdown timeout
            if let Err(e) = self.shutdown_coordinator.wait_for_force_shutdown().await {
                error!(error = %e, "Force shutdown timeout exceeded");
            }

            // Wait for kill shutdown timeout
            if let Err(e) = self.shutdown_coordinator.wait_for_kill_shutdown().await {
                error!(error = %e, "Kill shutdown timeout exceeded, exiting immediately");
            }
        }

        let elapsed = self.started_at.map(|t| t.elapsed());
        info!(uptime = ?elapsed, "Daemon shutdown complete");

        Ok(())
    }

    /// Initialize the logging system based on configuration.
    fn init_logging(&self) -> Result<()> {
        use tracing_subscriber::fmt::format::FmtSpan;
        use tracing_subscriber::fmt::writer::BoxMakeWriter;
        use tracing_subscriber::{EnvFilter, FmtSubscriber};

        let level: tracing::Level = self.config.logging.level.into();
        let filter = EnvFilter::from_default_env().add_directive(level.into());

        // Configure output writer
        let writer = if let Some(path) = &self.config.logging.file {
            Some(
                RotatingFileWriter::new(
                    path.clone(),
                    self.config.logging.max_file_size,
                    self.config.logging.max_files,
                )
                .map_err(|e| {
                    Error::io_with_source(
                        format!("Failed to initialize log file at {}", path.display()),
                        e,
                    )
                })?,
            )
        } else {
            None
        };

        let make_writer = |writer: &Option<RotatingFileWriter>| {
            writer.as_ref().map_or_else(
                || BoxMakeWriter::new(std::io::stdout),
                |writer| BoxMakeWriter::new(writer.clone()),
            )
        };

        // Configure output format
        if self.config.is_json_logging() {
            #[cfg(feature = "json-logs")]
            {
                let base_subscriber = FmtSubscriber::builder()
                    .with_env_filter(filter)
                    .with_span_events(FmtSpan::CLOSE)
                    .with_target(true)
                    .with_thread_ids(true)
                    .with_thread_names(true)
                    .with_writer(make_writer(&writer));

                let json_subscriber = base_subscriber
                    .json()
                    .flatten_event(true)
                    .with_current_span(false);

                tracing::subscriber::set_global_default(json_subscriber.finish()).map_err(|e| {
                    Error::config(format!("Failed to initialize JSON logging: {e}"))
                })?;

                return Ok(()); // Return early as logging is initialized
            }

            #[cfg(not(feature = "json-logs"))]
            {
                return Err(Error::config(
                    "JSON logging requested but feature not enabled",
                ));
            }
        }

        // Regular non-JSON logging
        let base_subscriber = FmtSubscriber::builder()
            .with_env_filter(filter)
            .with_span_events(FmtSpan::CLOSE)
            .with_target(true)
            .with_thread_ids(true)
            .with_thread_names(true)
            .with_writer(make_writer(&writer));

        let regular_subscriber = base_subscriber
            .with_ansi(self.config.is_colored_logging())
            .compact();

        tracing::subscriber::set_global_default(regular_subscriber.finish())
            .map_err(|e| Error::config(format!("Failed to initialize logging: {e}")))?;

        debug!(
            "Logging initialized with level: {:?}",
            self.config.logging.level
        );
        Ok(())
    }

    /// Spawn the signal handler task.
    #[cfg(feature = "tokio")]
    fn spawn_signal_handler(handler: SignalHandlerKind) -> tokio::task::JoinHandle<Result<()>> {
        tokio::spawn(async move { handler.handle_signals().await })
    }

    #[cfg(all(feature = "async-std", not(feature = "tokio")))]
    fn spawn_signal_handler(handler: SignalHandlerKind) -> async_std::task::JoinHandle<Result<()>> {
        async_std::task::spawn(async move { handler.handle_signals().await })
    }

    /// Get daemon statistics.
    pub fn get_stats(&self) -> DaemonStats {
        let subsystem_stats = self.subsystem_manager.get_stats();
        let shutdown_stats = self.shutdown_coordinator.get_stats();
        let total_restarts = subsystem_stats.total_restarts;

        DaemonStats {
            name: self.config.name.clone(),
            uptime: self.started_at.map(|t| t.elapsed()),
            is_shutdown: shutdown_stats.is_shutdown,
            shutdown_reason: shutdown_stats.reason,
            subsystem_stats,
            total_restarts,
        }
    }

    /// Request graceful shutdown programmatically.
    pub fn shutdown(&self) -> bool {
        self.shutdown_coordinator
            .initiate_shutdown(ShutdownReason::Requested)
    }

    /// Check if the daemon is running.
    pub fn is_running(&self) -> bool {
        !self.shutdown_coordinator.is_shutdown()
    }

    /// Get the daemon configuration.
    pub fn config(&self) -> &Config {
        &self.config
    }

    /// Get a snapshot of the current configuration. When the `config-watch` feature
    /// is enabled and hot-reload is active, this reflects the most recent loaded
    /// configuration; otherwise it returns the initial configuration.
    #[cfg(feature = "config-watch")]
    pub fn config_snapshot(&self) -> Arc<Config> {
        self.config_shared.load_full()
    }
}

impl Clone for Daemon {
    fn clone(&self) -> Self {
        Self {
            config: Arc::clone(&self.config),
            #[cfg(feature = "config-watch")]
            config_shared: Arc::clone(&self.config_shared),
            shutdown_coordinator: self.shutdown_coordinator.clone(),
            subsystem_manager: self.subsystem_manager.clone(),
            signal_handler: self.signal_handler.clone(),
            #[cfg(feature = "config-watch")]
            _config_watcher: None,
            started_at: self.started_at,
        }
    }
}

/// Statistics about the daemon's current state.
#[derive(Debug, Clone)]
pub struct DaemonStats {
    /// Daemon name
    pub name: String,
    /// Time since daemon started
    pub uptime: Option<std::time::Duration>,
    /// Whether shutdown has been initiated
    pub is_shutdown: bool,
    /// Reason for shutdown (if any)
    pub shutdown_reason: Option<crate::shutdown::ShutdownReason>,
    /// Subsystem statistics
    pub subsystem_stats: crate::subsystem::SubsystemStats,
    /// Total number of subsystem restarts
    pub total_restarts: u64,
}

/// Builder for creating daemon instances with fluent API.
pub struct DaemonBuilder {
    config: Config,
    // Pre-allocate the vector with a reasonable capacity
    subsystems: Vec<SubsystemRegistrationFn>,
    signal_config: Option<SignalConfig>,
    enable_signals: bool,
    config_path: Option<PathBuf>,
}

impl DaemonBuilder {
    /// Create a new daemon builder with the provided configuration.
    #[must_use]
    pub fn new(config: Config) -> Self {
        Self {
            config,
            // Pre-allocate the subsystems vector with a reasonable capacity
            subsystems: Vec::with_capacity(16),
            signal_config: None,
            enable_signals: true,
            config_path: None,
        }
    }

    /// Configure signal handling.
    #[must_use]
    pub fn with_signal_config(mut self, config: SignalConfig) -> Self {
        self.signal_config = Some(config);
        self
    }

    /// Override the configuration file path used for hot-reload.
    #[must_use]
    pub fn with_config_path<P: Into<PathBuf>>(mut self, path: P) -> Self {
        self.config_path = Some(path.into());
        self
    }

    /// Disable signal handling.
    #[must_use]
    pub const fn without_signals(mut self) -> Self {
        self.enable_signals = false;
        self
    }

    /// Enable only specific signals.
    #[must_use]
    pub fn with_signals(mut self, sigterm: bool, sigint: bool) -> Self {
        let mut config = SignalConfig::new();
        if !sigterm {
            config = config.without_sigterm();
        }
        if !sigint {
            config = config.without_sigint();
        }
        self.signal_config = Some(config);
        self
    }

    /// Add a task that will be run as part of the daemon.
    ///
    /// A task is a function that will be executed repeatedly until shutdown is requested.
    ///
    /// # Arguments
    ///
    /// * `name` - Name of the task for identification
    /// * `task_fn` - Function that implements the task logic
    ///
    /// # Returns
    ///
    /// Updated builder instance
    #[must_use]
    pub fn with_task<F, Fut>(mut self, name: &str, task_fn: F) -> Self
    where
        F: Fn(crate::shutdown::ShutdownHandle) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<()>> + Send + 'static,
    {
        // Clone the name to avoid lifetime issues
        let name = name.to_string();
        let subsystem_fn = Box::new(move |subsystem_manager: &SubsystemManager| {
            subsystem_manager.register_fn(&name, task_fn)
        });

        self.subsystems.push(subsystem_fn);
        self
    }

    /// Add a subsystem that will be managed by the daemon.
    ///
    /// A subsystem is a component that implements the `Subsystem` trait.
    ///
    /// # Arguments
    ///
    /// * `subsystem` - The subsystem to add
    ///
    /// # Returns
    ///
    /// Updated builder instance
    #[must_use]
    pub fn with_subsystem<S>(mut self, subsystem: S) -> Self
    where
        S: Subsystem + Send + Sync + 'static,
    {
        let subsystem_fn = Box::new(move |subsystem_manager: &SubsystemManager| {
            subsystem_manager.register(subsystem)
        });

        self.subsystems.push(subsystem_fn);
        self
    }

    /// Add a subsystem using a registration function.
    ///
    /// This is a lower-level method that gives direct access to the `SubsystemManager`
    /// for registration. It's useful when you need more control over the registration process.
    ///
    /// # Arguments
    ///
    /// * `name` - Name for identification in logs
    /// * `register_fn` - Function that handles the subsystem registration
    ///
    /// # Returns
    ///
    /// Updated builder instance
    #[must_use]
    pub fn with_subsystem_fn<F>(mut self, name: &str, register_fn: F) -> Self
    where
        F: FnOnce(&SubsystemManager) -> SubsystemId + Send + 'static,
    {
        debug!("Adding subsystem registration function for {}", name);
        self.subsystems.push(Box::new(register_fn));
        self
    }

    /// Build the daemon instance.
    /// Builds a daemon from the configured builder
    ///
    /// # Errors
    ///
    /// Returns an error if the daemon configuration is invalid or if required components cannot be initialized
    pub fn build(self) -> Result<Daemon> {
        // Validate configuration
        self.config.validate()?;

        // Create shutdown coordinator
        let shutdown_coordinator = ShutdownCoordinator::new(
            self.config.shutdown.graceful,
            self.config.shutdown.force,
            self.config.shutdown.kill,
        );

        // Create subsystem manager
        let subsystem_manager = SubsystemManager::new(shutdown_coordinator.clone());

        // Register all subsystems
        for subsystem_fn in self.subsystems {
            let id = subsystem_fn(&subsystem_manager);
            debug!(subsystem_id = id, "Registered subsystem");
        }

        // Create signal handler if enabled
        let signal_handler = if self.enable_signals {
            self.signal_config.clone().map_or_else(
                || {
                    Some(SignalHandlerKind::Default(Arc::new(SignalHandler::new(
                        shutdown_coordinator.clone(),
                    ))))
                },
                |config| {
                    Some(SignalHandlerKind::Configurable(Arc::new(
                        ConfigurableSignalHandler::new(shutdown_coordinator.clone(), config),
                    )))
                },
            )
        } else {
            None
        };

        // Prepare configuration arcs
        let config_arc = Arc::new(self.config);

        #[cfg(feature = "config-watch")]
        let config_shared: Arc<ArcSwap<Config>> = Arc::new(ArcSwap::from(config_arc.clone()));

        // Optionally start config watcher when hot_reload is enabled
        #[cfg(feature = "config-watch")]
        let mut config_watcher: Option<RecommendedWatcher> = None;

        #[cfg(feature = "config-watch")]
        {
            if config_arc.hot_reload {
                let swap = Arc::clone(&config_shared);
                let watch_path = self
                    .config_path
                    .or_else(|| {
                        config_arc
                            .work_dir
                            .as_ref()
                            .map(|d| d.join(crate::DEFAULT_CONFIG_FILE))
                    })
                    .unwrap_or_else(|| PathBuf::from(crate::DEFAULT_CONFIG_FILE));

                let watch_path_for_cb = watch_path.clone();

                match Config::watch_file(&watch_path, move |res| match res {
                    Ok(new_cfg) => {
                        swap.store(Arc::new(new_cfg));
                        info!(
                            "Configuration hot-reloaded from {}",
                            watch_path_for_cb.display()
                        );
                    }
                    Err(e) => {
                        warn!(error = %e, "Configuration reload failed");
                    }
                }) {
                    Ok(w) => {
                        config_watcher = Some(w);
                        info!("Config watcher started for {}", watch_path.display());
                    }
                    Err(e) => {
                        warn!(error = %e, "Failed to start config watcher; continuing without hot-reload");
                    }
                }
            }
        }

        Ok(Daemon {
            config: config_arc,
            #[cfg(feature = "config-watch")]
            config_shared,
            shutdown_coordinator,
            subsystem_manager,
            signal_handler,
            #[cfg(feature = "config-watch")]
            _config_watcher: config_watcher,
            started_at: None,
        })
    }

    /// Build and run the daemon in one step.
    /// Runs the daemon until completion or error
    ///
    /// # Errors
    ///
    /// Returns an error if the daemon encounters an unrecoverable error during execution
    pub async fn run(self) -> Result<()> {
        let daemon = self.build()?;
        daemon.run().await
    }
}

/// Convenience macro for creating subsystems from closures.
#[macro_export]
macro_rules! subsystem {
    ($name:expr, $closure:expr) => {
        Box::new(move |shutdown: $crate::shutdown::ShutdownHandle| {
            Box::pin($closure(shutdown)) as Pin<Box<dyn Future<Output = $crate::Result<()>> + Send>>
        })
    };
}

/// Convenience macro for creating simple task-based subsystems.
#[macro_export]
macro_rules! task {
    ($name:expr, $body:expr) => {
        |shutdown: $crate::shutdown::ShutdownHandle| async move {
            #[cfg(feature = "tokio")]
            let mut shutdown = shutdown;
            loop {
                #[cfg(feature = "tokio")]
                {
                    tokio::select! {
                        _ = shutdown.cancelled() => {
                            tracing::info!("Task '{}' shutting down", $name);
                            break;
                        }
                        _ = async { $body } => {}
                    }
                }

                #[cfg(all(feature = "async-std", not(feature = "tokio")))]
                {
                    if shutdown.is_shutdown() {
                        tracing::info!("Task '{}' shutting down", $name);
                        break;
                    }
                    // Execute the body directly without awaiting
                    $body;
                    // Add a small delay to prevent tight loop
                    async_std::task::sleep(std::time::Duration::from_millis(10)).await;
                }
            }
            Ok(())
        }
    };
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::pin::Pin;
    use std::time::Duration;

    async fn test_subsystem(shutdown: crate::shutdown::ShutdownHandle) -> Result<()> {
        #[cfg(feature = "tokio")]
        let mut shutdown = shutdown;
        loop {
            #[cfg(feature = "tokio")]
            {
                tokio::select! {
                    () = shutdown.cancelled() => break,
                    () = tokio::time::sleep(Duration::from_millis(10)) => {}
                }
            }

            #[cfg(all(feature = "async-std", not(feature = "tokio")))]
            {
                if shutdown.is_shutdown() {
                    break;
                }
                async_std::task::sleep(Duration::from_millis(10)).await;
            }
        }
        Ok(())
    }

    #[cfg(feature = "tokio")]
    #[cfg_attr(miri, ignore)]
    #[tokio::test]
    async fn test_daemon_builder() {
        // Add a test timeout to prevent freezing
        let test_result = tokio::time::timeout(Duration::from_secs(5), async {
            let config = Config::new().unwrap();
            let daemon = Daemon::builder(config)
                .with_subsystem_fn("test", |subsystem_manager| {
                    subsystem_manager.register_fn("test_subsystem", test_subsystem)
                })
                .build()
                .unwrap();

            assert!(daemon.is_running());
            assert_eq!(daemon.config().name, "proc-daemon");

            // Ensure proper cleanup
            daemon.shutdown();
        })
        .await;

        assert!(test_result.is_ok(), "Test timed out after 5 seconds");
    }

    #[cfg(all(feature = "async-std", not(feature = "tokio")))]
    #[async_std::test]
    async fn test_daemon_builder() {
        // Add a test timeout to prevent freezing
        let test_result = async_std::future::timeout(Duration::from_secs(5), async {
            let config = Config::new().unwrap();
            let daemon = Daemon::builder(config)
                .with_subsystem_fn("test", |subsystem_manager| {
                    subsystem_manager.register_fn("test_subsystem", test_subsystem)
                })
                .build()
                .unwrap();

            assert!(daemon.is_running());
            assert_eq!(daemon.config().name, "proc-daemon");

            // Ensure proper cleanup
            daemon.shutdown();
        })
        .await;

        assert!(test_result.is_ok(), "Test timed out after 5 seconds");
    }

    #[cfg(feature = "tokio")]
    #[cfg_attr(miri, ignore)]
    #[tokio::test]
    async fn test_daemon_with_defaults() {
        // Add a test timeout to prevent freezing
        let test_result = tokio::time::timeout(Duration::from_secs(5), async {
            let builder = Daemon::with_defaults().unwrap();
            let daemon = builder
                .with_task("simple_task", |_shutdown| async {
                    tokio::time::sleep(Duration::from_millis(10)).await;
                    Ok(())
                })
                .build()
                .unwrap();

            assert!(daemon.is_running());

            // Ensure proper cleanup
            daemon.shutdown();
        })
        .await;

        assert!(test_result.is_ok(), "Test timed out after 5 seconds");
    }

    #[cfg(all(feature = "async-std", not(feature = "tokio")))]
    #[async_std::test]
    async fn test_daemon_with_defaults() {
        // Add a test timeout to prevent freezing
        let test_result = async_std::future::timeout(Duration::from_secs(5), async {
            let builder = Daemon::with_defaults().unwrap();
            let daemon = builder
                .with_task("simple_task", |_shutdown| async {
                    async_std::task::sleep(Duration::from_millis(10)).await;
                    Ok(())
                })
                .build()
                .unwrap();

            assert!(daemon.is_running());

            // Ensure proper cleanup
            daemon.shutdown();
        })
        .await;

        assert!(test_result.is_ok(), "Test timed out after 5 seconds");
    }

    #[cfg(feature = "tokio")]
    #[cfg_attr(miri, ignore)]
    #[tokio::test]
    async fn test_daemon_shutdown() {
        // Add a test timeout to prevent freezing
        let test_result = tokio::time::timeout(Duration::from_secs(5), async {
            let config = Config::builder()
                .name("test-daemon")
                .shutdown_timeout(Duration::from_millis(100))
                .unwrap()
                .build()
                .unwrap();

            let daemon = Daemon::builder(config)
                .with_subsystem_fn("test", |subsystem_manager| {
                    subsystem_manager.register_fn("test_subsystem", test_subsystem)
                })
                .without_signals()
                .build()
                .unwrap();

            // Request shutdown
            daemon.shutdown();
            assert!(!daemon.is_running());
        })
        .await;

        assert!(test_result.is_ok(), "Test timed out after 5 seconds");
    }

    #[cfg(all(feature = "async-std", not(feature = "tokio")))]
    #[async_std::test]
    async fn test_daemon_shutdown() {
        // Add a test timeout to prevent freezing
        let test_result = async_std::future::timeout(Duration::from_secs(5), async {
            let config = Config::builder()
                .name("test-daemon")
                .shutdown_timeout(Duration::from_millis(100))
                .unwrap()
                .build()
                .unwrap();

            let daemon = Daemon::builder(config)
                .with_subsystem_fn("test", |subsystem_manager| {
                    subsystem_manager.register_fn("test_subsystem", test_subsystem)
                })
                .without_signals()
                .build()
                .unwrap();

            // Request shutdown
            daemon.shutdown();
            assert!(!daemon.is_running());
        })
        .await;

        assert!(test_result.is_ok(), "Test timed out after 5 seconds");
    }

    #[test]
    fn test_daemon_stats() {
        let config = Config::new().unwrap();
        let daemon = Daemon::builder(config).build().unwrap();

        let stats = daemon.get_stats();
        assert_eq!(stats.name, "proc-daemon");
        assert!(stats.uptime.is_none()); // Not started yet
        assert!(!stats.is_shutdown);
    }

    struct TestSubsystemStruct {
        name: String,
    }

    impl TestSubsystemStruct {
        fn new(name: &str) -> Self {
            Self {
                name: name.to_string(),
            }
        }
    }

    impl Subsystem for TestSubsystemStruct {
        fn run(
            &self,
            shutdown: crate::shutdown::ShutdownHandle,
        ) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
            Box::pin(async move {
                #[cfg(feature = "tokio")]
                let mut shutdown = shutdown;
                loop {
                    #[cfg(feature = "tokio")]
                    {
                        tokio::select! {
                            () = shutdown.cancelled() => break,
                            () = tokio::time::sleep(Duration::from_millis(10)) => {}
                        }
                    }

                    #[cfg(all(feature = "async-std", not(feature = "tokio")))]
                    {
                        if shutdown.is_shutdown() {
                            break;
                        }
                        async_std::task::sleep(Duration::from_millis(10)).await;
                    }
                }
                Ok(())
            })
        }

        fn name(&self) -> &str {
            &self.name
        }
    }

    #[cfg(feature = "tokio")]
    #[cfg_attr(miri, ignore)]
    #[tokio::test]
    async fn test_daemon_with_struct_subsystem() {
        // Add a test timeout to prevent freezing
        let test_result = tokio::time::timeout(Duration::from_secs(5), async {
            let config = Config::new().unwrap();
            let subsystem = TestSubsystemStruct::new("struct_test");

            let daemon = Daemon::builder(config)
                .with_subsystem(subsystem)
                .without_signals()
                .build()
                .unwrap();

            let stats = daemon.get_stats();
            assert_eq!(stats.subsystem_stats.total_subsystems, 1);

            // Ensure proper cleanup
            daemon.shutdown();
        })
        .await;

        assert!(test_result.is_ok(), "Test timed out after 5 seconds");
    }

    #[cfg(all(feature = "async-std", not(feature = "tokio")))]
    #[async_std::test]
    async fn test_daemon_with_struct_subsystem() {
        // Add a test timeout to prevent freezing
        let test_result = async_std::future::timeout(Duration::from_secs(5), async {
            let config = Config::new().unwrap();
            let subsystem = TestSubsystemStruct::new("struct_test");

            let daemon = Daemon::builder(config)
                .with_subsystem(subsystem)
                .without_signals()
                .build()
                .unwrap();

            let stats = daemon.get_stats();
            assert_eq!(stats.subsystem_stats.total_subsystems, 1);

            // Ensure proper cleanup
            daemon.shutdown();
        })
        .await;

        assert!(test_result.is_ok(), "Test timed out after 5 seconds");
    }

    #[cfg(feature = "tokio")]
    #[cfg_attr(miri, ignore)]
    #[tokio::test]
    async fn test_daemon_signal_configuration() {
        // Add a test timeout to prevent freezing
        let test_result = tokio::time::timeout(Duration::from_secs(5), async {
            let config = Config::new().unwrap();
            let signal_config = SignalConfig::new().with_sighup().without_sigint();

            let daemon = Daemon::builder(config)
                .with_signal_config(signal_config)
                .build()
                .unwrap();

            assert!(daemon.signal_handler.is_some());

            // Ensure proper cleanup
            daemon.shutdown();
        })
        .await;

        assert!(test_result.is_ok(), "Test timed out after 5 seconds");
    }

    #[cfg(all(feature = "async-std", not(feature = "tokio")))]
    #[async_std::test]
    async fn test_daemon_signal_configuration() {
        // Add a test timeout to prevent freezing
        let test_result = async_std::future::timeout(Duration::from_secs(5), async {
            let config = Config::new().unwrap();
            let signal_config = SignalConfig::new().with_sighup().without_sigint();

            let daemon = Daemon::builder(config)
                .with_signal_config(signal_config)
                .build()
                .unwrap();

            assert!(daemon.signal_handler.is_some());

            // Ensure proper cleanup
            daemon.shutdown();
        })
        .await;

        assert!(test_result.is_ok(), "Test timed out after 5 seconds");
    }

    #[cfg(feature = "tokio")]
    #[cfg_attr(miri, ignore)]
    #[tokio::test]
    async fn test_macro_usage() {
        // Add a test timeout to prevent freezing
        let test_result = tokio::time::timeout(Duration::from_secs(5), async {
            let config = Config::new().unwrap();

            let daemon = Daemon::builder(config)
                .with_task(
                    "macro_test",
                    task!("macro_test", {
                        tokio::time::sleep(Duration::from_millis(1)).await;
                    }),
                )
                .without_signals()
                .build()
                .unwrap();

            let stats = daemon.get_stats();
            assert_eq!(stats.subsystem_stats.total_subsystems, 1);

            // Ensure proper cleanup
            daemon.shutdown();
        })
        .await;

        assert!(test_result.is_ok(), "Test timed out after 5 seconds");
    }

    #[cfg(all(feature = "async-std", not(feature = "tokio")))]
    #[async_std::test]
    async fn test_macro_usage() {
        // Add a test timeout to prevent freezing
        let test_result = async_std::future::timeout(Duration::from_secs(5), async {
            let config = Config::new().unwrap();

            let daemon = Daemon::builder(config)
                .with_task(
                    "macro_test",
                    task!("macro_test", {
                        async_std::task::sleep(Duration::from_millis(1)).await;
                    }),
                )
                .without_signals()
                .build()
                .unwrap();

            let stats = daemon.get_stats();
            assert_eq!(stats.subsystem_stats.total_subsystems, 1);

            // Ensure proper cleanup
            daemon.shutdown();
        })
        .await;

        assert!(test_result.is_ok(), "Test timed out after 5 seconds");
    }
}