munggoggo.messages¶
I decided not to go for pickle, although it would simplify the serialization process significantly due to its capability to handle any python objects.
However, in order to allow for communication with non-python world I went for JSON encoding and wrapping objects into a common data structure based on python dataclasses.
Message Definition and Serialization:¶
Self describing data serialization format based on json and python dataclasses.
Two stage serialization
serialize payload by using dataclasses and marshmallow (dataclass with dataclass_json decorator)
wrap result into RPC dataclass with two fields: {c_type: str, c_data: json_payload}
Deserialization:
extract the class_type of payload from the SerializedObject (obj.c_type)
deserizalize the json_payload with the class_type as schema into the respective python object
Caveat when serializing dataclasses:
As specified in the datetime docs, if your datetime object is naive, it will assume your system local timezone when calling .timestamp(). JSON nunbers corresponding to a datetime field in your dataclass are decoded into a datetime-aware object, with tzinfo set to your system local timezone. Thus, if you encode a datetime-naive object, you will decode into a datetime-aware object. This is important, because encoding and decoding won’t strictly be inverses.
When using tz-aware objects everything should be fine
Rabbit MQ Message attributes and their semantic here:
body: payload
headers: message headers
headers_raw: message raw headers
content_type: application/json
content_encoding:
delivery_mode: delivery mode
priority: priority (not used)
correlation_id: correlation id
reply_to: reply to
routing_key: routing_key (e.g. topic)
expiration: expiration in seconds (or datetime or timedelta)
message_id: message id
timestamp: timestamp
type: type -> (RmqMessageTypes: not rmq relevant, for application to communicate semantics)
user_id: user id -> (rmq user, e.g. guest)
app_id: app id -> (sender identity)
-
class
messages.
ListTraceStore
(limit: Union[int, NoneType] = None, app_id: Union[str, NoneType] = None, category: Union[str, NoneType] = None, traces: List[str] = <factory>)[source]¶
-
class
messages.
RmqMessageTypes
[source]¶ Defines the RMQ messages which are handled by system
User can define arbitrary message types by using the msg_type parameter of fanout_send/direct_send methods.
-
class
messages.
RpcMessage
(c_type: str, c_data: str, request_type: str = <RpcMessageTypes.RPC_REQUEST: 1>)[source]¶
-
class
messages.
SerializableObject
[source]¶
-
class
messages.
TraceStoreMessage
(body: str = '', body_size: int = 0, headers: dict = None, content_type: str = 'application/json', content_encoding: str = '', delivery_mode: int = 0, priority: int = 0, correlation_id: str = '', reply_to: str = '', expiration: datetime.datetime = None, message_id: str = '', timestamp: <module 'time' (built-in)> = None, type: str = '', user_id: str = '', app_id: str = '', target: str = '', cluster_id: str = '', consumer_tag: str = '', delivery_tag: int = 0, exchange: str = '', redelivered: bool = False, routing_key: str = '')[source]¶