|
| 1 | +############################################################### |
| 2 | +# Copyright 2025 Lawrence Livermore National Security, LLC |
| 3 | +# (c.f. AUTHORS, NOTICE.LLNS, COPYING) |
| 4 | +# |
| 5 | +# This file is part of the Flux resource manager framework. |
| 6 | +# For details, see https://github.com/flux-framework. |
| 7 | +# |
| 8 | +# SPDX-License-Identifier: LGPL-3.0 |
| 9 | +############################################################### |
| 10 | + |
| 11 | +from flux.abc import JournalConsumerBase, JournalEventBase |
| 12 | + |
| 13 | + |
| 14 | +class ResourceJournalEvent(JournalEventBase): |
| 15 | + """A container for an event from the resource journal |
| 16 | +
|
| 17 | + Attributes: |
| 18 | + name (str): event name (See :rfc:`21` for possible event names) |
| 19 | + timestamp (float): event timestamp in seconds since the epoch |
| 20 | + with sub-millisecond precision. |
| 21 | + context (dict): context dictionary |
| 22 | + (See `RFC 21: Event Descriptions <https://flux-framework.readthedocs.io/projects/flux-rfc/en/latest/spec_21.html#event-descriptions>`_.) |
| 23 | + context_string (str): context dict converted to comma separated |
| 24 | + key=value string. |
| 25 | + R (dict): For resource-define events, the instance R (See :rfc:`20`) |
| 26 | + """ |
| 27 | + |
| 28 | + def __init__(self, event, R=None): |
| 29 | + super().__init__(event) |
| 30 | + self.R = R |
| 31 | + |
| 32 | + def __str__(self): |
| 33 | + if self.is_empty(): |
| 34 | + return f"{self.timestamp:<0.5f} End of historical data stream" |
| 35 | + return " ".join( |
| 36 | + [ |
| 37 | + f"{self.timestamp:<0.5f}", |
| 38 | + self.name, |
| 39 | + self.context_string, |
| 40 | + ] |
| 41 | + ) |
| 42 | + |
| 43 | + |
| 44 | +class ResourceJournalConsumer(JournalConsumerBase): |
| 45 | + """Class for consuming the resource journal |
| 46 | +
|
| 47 | + See :obj:`journal.JournalConsumerBase` for documentation of the |
| 48 | + journal consumer interface. |
| 49 | + """ |
| 50 | + |
| 51 | + def __init__(self, flux_handle, since=0.0, include_sentinel=False): |
| 52 | + super().__init__( |
| 53 | + flux_handle, |
| 54 | + "resource.journal", |
| 55 | + full=True, |
| 56 | + since=since, |
| 57 | + include_sentinel=include_sentinel, |
| 58 | + ) |
| 59 | + |
| 60 | + def process_response(self, resp): |
| 61 | + """Process a single response from the resource journal""" |
| 62 | + return {"R": resp.get("R")} |
| 63 | + |
| 64 | + def create_event(self, entry, R=None): |
| 65 | + """Create a ResourceJournalEvent from one event entry in a response""" |
| 66 | + return ResourceJournalEvent(entry, R) |
| 67 | + |
| 68 | + |
| 69 | +# vi: ts=4 sw=4 expandtab |
0 commit comments