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)

Types 6

structFuture(T)

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

TThe type of the result value. Example:
auto future = asyncNetConnections();
// Do other work...
NetConnection[] connections = future.get();
Fields
private Tid tid
private T result
private bool received
private Exception error
Methods
T get()Gets the result of the async operation, blocking if necessary.
bool ready() nothrow @nogcChecks if the result is available without blocking.
Constructors
this(Tid tid)Creates a Future from a thread ID.

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.

Fields
private Tid tid
private Process[] result
private bool received
private Exception error
Methods
Process[] get()Gets the result, blocking if necessary.
bool ready() nothrow @nogcChecks if the result is available without blocking.
Constructors
this(Tid tid)Creates a ProcessFuture from a thread ID.

Process event types for lifecycle monitoring.

CREATEDProcess was created
TERMINATEDProcess terminated
STATUS_CHANGEDProcess status changed (e.g., sleeping to running)

Process event structure for monitoring callbacks.

Fields
int pid
SysTime timestamp
string details

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();

Fields
private void delegate(Process) createdCallback
private void delegate(int pid) terminatedCallback
private void delegate(Process proc) statusChangedCallback
private Thread monitorThread
private bool running
private Mutex mutex
private Condition condition
private int[] knownPids
private Duration pollInterval
Methods
ProcessMonitor 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.
private void monitorLoop()
private void monitorLoopPolling()
Constructors
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);
}

Fields
private ProcessMonitor monitor
private ProcessEvent[] buffer
private bool stopped
private Mutex bufferMutex
Methods
bool empty() nothrow @nogcChecks if the range is empty.
ProcessEvent front()Gets the front event.
void popFront()Advances to the next event.
void stop()Stops the event stream and releases resources.
Constructors
this(Duration pollInterval)Creates a new ProcessEventRange.
Destructors
~thisDestructor - ensures monitor is stopped.

Functions 5

fnProcessFuture asyncAllProcesses()Gets all processes asynchronously.
fnFuture!(NetConnection[]) asyncNetConnections(string kind = "inet")Gets network connections asynchronously.
fnProcessEventRange processEventStream(Duration pollInterval = dur!"msecs"(100))Creates a process event stream for reactive monitoring.
fnbool waitForProcess(int pid, Duration timeout = Duration.max)Convenience function to wait for a specific process to terminate.
fnint waitForAnyProcess(int[] pids, Duration timeout = Duration.max)Convenience function to wait for any of the specified processes to terminate.