forked from DiamondLightSource/python-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample_consumer.py
More file actions
38 lines (28 loc) · 1.16 KB
/
Copy pathsample_consumer.py
File metadata and controls
38 lines (28 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from __future__ import annotations
import json
import time
from workflows.services.common_service import CommonService
class SampleConsumer(CommonService):
"""An example service building on top of the workflow.services architecture,
demonstrating how this architecture can be used.
This service consumes messages off a queue."""
# Human readable service name
_service_name = "Message Consumer"
# Logger name
_logger_name = "workflows.service.sample_consumer"
def initializing(self):
"""Subscribe to a channel."""
self._transport.subscribe("transient.destination", self.consume_message)
def consume_message(self, header, message):
"""Consume a message"""
t = (time.time() % 1000) * 1000
if header:
header_str = json.dumps(header, indent=2) + "\n" + "----------------" + "\n"
else:
header_str = ""
if isinstance(message, dict):
message = json.dumps(message, indent=2) + "\n" + "----------------" + "\n"
self.log.info(
f"=== Consume ====\n{header_str}{message}\nReceived message @{t:10.3f} ms"
)
time.sleep(0.1)