Source code for ska_sdp_batchlet.utils.monitor.handler

from contextlib import ExitStack, nullcontext

from .log import LogMonitor
from .resource import ResourceMonitor


# pylint: disable=R0903
[docs] class MonitorHandler: """ Handles all monitoring related operations. It is responsible for instantiating and starting both :py:class:`LogMonitor` and :py:class:`ResourceMonitor` instances. Parameters ---------- stack: ExitStack The ExitStack to register the monitors with. NOTE: The "stack" is pass by reference, and MonitorHandler will modify it. generate_reports_on_failure: bool, default=True Whether to generate monitoring report, even if there are any failures. resources: dict[dict], optional Optional dictionary of {start: {}, stop: {}} to pass to ResourceMonitor logs: dict, optional Optional dictionary of keyword arguments to pass to LogMonitor """ def __init__( self, stack: ExitStack, generate_reports_on_failure=True, resources=None, logs=None, ): self.log_monitor = None self.resource_monitor = None if resources: self.resource_monitor = ResourceMonitor( generate_reports_on_failure=generate_reports_on_failure, **resources ) stack.enter_context(self.resource_monitor) if logs: self.log_monitor = LogMonitor(**logs) stack.enter_context(self.log_monitor) @property def log_stream(self): """ If log monitor is configured, start a new watcher subprocess, and return a io stream on which the logs of a application to be watched can be passed. If log monitor is not configured, return a nullcontext. """ return ( self.log_monitor.log_write_stream if self.log_monitor else nullcontext() )