ddn.util.monaco.async
Async and Event-Driven Monitoring
This module provides non-blocking monitoring with event streams, leveraging D's unique concurrency advantages.
Features:
- Async variants of expensive operations
- Process lifecycle event notifications
- Efficient polling-based monitoring
- Range-based event streaming
- Reactive monitoring patterns
Platform Availability:
- Async operations: Linux, Windows, macOS, BSD
- Process events: All platforms (implementation differs)
- Linux: Netlink process connector (event-driven, preferred) + /proc polling (fallback)
- Windows: Polling-based monitoring
- macOS/BSD: Polling-based monitoring
Performance:
- Linux with netlink: Event-driven, no polling, immediate notifications
- Polling mode: Checks process list at configurable intervals (default: 100ms)
Copyright
Types 6
A simple Future type representing an asynchronous operation.
This struct wraps an operation that executes in a separate thread and allows the caller to retrieve the result when needed.
Parameters
T | The type of the result value. Example: |
T get()Gets the result of the async operation, blocking if necessary.Specialized Future for Process arrays.
This version collects PIDs asynchronously and creates Process objects in the main thread to avoid thread-local data aliasing issues.
Process event types for lifecycle monitoring.
Process event structure for monitoring callbacks.
Process monitor for tracking process lifecycle events.
This class provides callbacks for process creation, termination, and status changes. It uses platform-specific mechanisms when available, falling back to polling when necessary.
Example:
auto monitor = new ProcessMonitor();
monitor.onProcessCreated((Process proc) {
writeln("New process: ", proc.name());
});
monitor.onProcessTerminated((int pid) {
writeln("Process terminated: ", pid);
});
monitor.start();
// Monitor is now running...
monitor.stop();private void delegate(Process) createdCallbackprivate void delegate(int pid) terminatedCallbackprivate void delegate(Process proc) statusChangedCallbackprivate Thread monitorThreadprivate bool runningprivate Mutex mutexprivate Condition conditionprivate int[] knownPidsprivate Duration pollIntervalProcessMonitor useNetlink(bool enabled = true)Enables or disables netlink support (Linux only).ProcessMonitor onProcessCreated(void delegate(Process) callback)Sets the callback for process creation events.ProcessMonitor onProcessTerminated(void delegate(int pid) callback)Sets the callback for process termination events.ProcessMonitor onProcessStatusChanged(void delegate(Process proc) callback)Sets the callback for process status change events.ProcessMonitor setPollInterval(Duration interval)Sets the polling interval for checking process changes.void start()Starts monitoring process events.void stop()Stops monitoring process events.bool isRunning()Checks if the monitor is currently running.void monitorLoop()void monitorLoopPolling()this()Creates a new ProcessMonitor instance.Process event stream range for reactive monitoring.
This range-based interface allows for functional-style processing of process events.
Example:
auto events = processEventStream()
.filter!(e => e.type == ProcessEventType.CREATED)
.take(10);
foreach (event; events) {
writeln("New process: ", event.pid);
}private ProcessMonitor monitorprivate ProcessEvent[] bufferprivate bool stoppedprivate Mutex bufferMutexProcessEvent front()Gets the front event.void popFront()Advances to the next event.void stop()Stops the event stream and releases resources.~thisDestructor - ensures monitor is stopped.Functions 5
Future!(NetConnection[]) asyncNetConnections(string kind = "inet")Gets network connections asynchronously.ProcessEventRange processEventStream(Duration pollInterval = dur!"msecs"(100))Creates a process event stream for reactive monitoring.bool waitForProcess(int pid, Duration timeout = Duration.max)Convenience function to wait for a specific process to terminate.int waitForAnyProcess(int[] pids, Duration timeout = Duration.max)Convenience function to wait for any of the specified processes to terminate.