High-level handlers MR4: Message handlers
This MR is split out from the original High-level handlers MR !25 (closed), towards uis/devops/epics#321
These MRs are chained, with each MR targeting the previous MR's branch. They either need to be merged in order, or all at once from the final one (after changing it to target main).
This MR adds support for event handler functions that process message events, such as GCP PubSub messages.
This module provides support for:
- Validating incoming message metadata envelope
- Parsing/validating the message payload
- Recording the message envelope metadata in the function's
ExecutionInforesult
Handler functions can focus on processing decoded message data payloads, without needing to do their own parsing and validation or event metadata logging/reporting.
To use this module, handler functions are wrapped with this module's
@message_handler(...) decorator. The decorator takes a message_type argument
that describes the message payload that the handler will be called with to
handle an incoming message. The message_type can be any of:
- A Pydantic model, to handle a JSON payload; the handler receives an instance of the model.
- A Protobuf Message class, to handle a protobuf payload; the handler receives an instance of the Message.
-
Noneto perform no validation; the handler receives the payload bytes. - A parser function that receives bytes and returns a value; the handler receives the parser's return value.
Handler functions receive the message payload decoded by the message_type, and
also have access to the the full GCP PubSub message and CloudEvent values that
the message was delivered with. Metadata from these metadata envelopes is
automatically included in PubsubMessageExecutionInfo values created within the
handler function's scope.
Issues
This MR implements #25 (closed), #27 (closed), #28 (closed), #29 (closed) and #30 (closed).