flux.brokermod module

class flux.brokermod.BrokerLogger(h)

Bases: object

Logger bridge from a broker module to the Flux ring buffer.

Provides convenience methods (debug, info, notice, warning, error, critical, alert, emerg). Also callable as log(level, msg) so it can serve as a drop-in wrapper for handle.log().

The level attribute is a threshold: messages with a numerically higher syslog priority value (i.e. lower urgency, such as LOG_DEBUG) are silently dropped when they exceed the threshold. The default syslog.LOG_DEBUG passes all messages through, matching C module behaviour where the broker's global log level is the only filter.

level may be assigned a syslog integer constant or a level-name string (e.g. "info", "debug"); reading level always returns the integer.

LEVEL_NAMES = {'alert': 1, 'crit': 2, 'debug': 7, 'emerg': 0, 'err': 3, 'info': 6, 'notice': 5, 'warning': 4}
alert(msg: str) None
critical(msg: str) None
debug(msg: str) None
emerg(msg: str) None
error(msg: str) None
info(msg: str) None
property level

Current log-level threshold as a syslog integer constant.

property level_name

Current log level as a name string (e.g. 'info').

notice(msg: str) None
warning(msg: str) None
class flux.brokermod.BrokerModule(h, *args)

Bases: object

Base class for Python broker modules.

Subclass this and use the request_handler() and event_handler() decorators to register message handlers, then call run() to start the reactor. Handler topics are automatically prefixed with the module name unless prefix=False is passed to the decorator.

Example:

from flux.brokermod import BrokerModule, event_handler, request_handler

class MyModule(BrokerModule):

    @request_handler("info")
    def info(self, msg):
        self.handle.respond(msg, self.name)

    @event_handler("panic")
    def panic(self, msg):
        self.stop_error()

When this is the only BrokerModule subclass in the file and no mod_main() is defined, the loader synthesizes the entry point automatically. An explicit mod_main() is only needed to resolve ambiguity when multiple subclasses are present.

property args

Tuple of module arguments passed at load time.

debug_test(flag, clear=False)

Test a module debug bit, optionally clearing it.

Returns True if the bit identified by flag is set. If clear is True the bit is atomically cleared after being read. Bits are set externally via flux module debug --setbit N <module-name>.

property handle

The underlying flux.Flux handle.

property name

The module name as assigned by the broker.

run()

Run the reactor until stopped.

Raises OSError if the reactor was stopped with an error (e.g. via stop_error()) or if a Python exception was set on the handle during a callback.

set_running()

Signal to the broker that the module is now running.

Call this before performing synchronous initialization in run() to allow flux module load to return instead of waiting for the reactor to start. See flux_module_set_running(3).

stop()

Stop the reactor and exit run() normally.

stop_error()

Stop the reactor and cause run() to raise OSError.

flux.brokermod.event_handler(topic, prefix=True)

Decorator to register a method as an event handler.

The handler is invoked with (self, msg) when an event matching <module_name>.<topic> is received. If prefix=False, the topic is used as-is without prepending the module name.

flux.brokermod.request_handler(topic, prefix=True, allow_guest=False)

Decorator to register a method as a request handler.

The handler is invoked with (self, msg) when a request matching <module_name>.<topic> is received. If prefix=False, the topic is used as-is without prepending the module name. If allow_guest=True, requests from non-owner users (FLUX_ROLE_USER) are also accepted.

flux.brokermod.resolve_entry_point(mod)

Resolve the entry point callable from a Python broker module object.

If the module defines mod_main() it is returned as-is. Otherwise, a single BrokerModule subclass is searched for in the module's namespace and its entry point synthesized as cls(h, *args).run().

Raises

ValueError -- if no entry point can be determined, or if multiple BrokerModule subclasses are found without an explicit mod_main().