-
Notifications
You must be signed in to change notification settings - Fork 1
/
filesize_request_generator.py
73 lines (59 loc) · 2.14 KB
/
filesize_request_generator.py
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import pika
import uuid
import json
from dane import Document, Task
from dane.config import cfg
class filesize_server:
def __init__(self, config):
self.config = config.RABBITMQ
credentials = pika.PlainCredentials(self.config.USER, self.config.PASSWORD)
self.connection = pika.BlockingConnection(
pika.ConnectionParameters(
credentials=credentials, host=self.config.HOST, port=self.config.PORT
)
)
self.channel = self.connection.channel()
self.channel.queue_declare(queue="response_queue", exclusive=True)
self.channel.basic_consume(
queue="response_queue", on_message_callback=self.on_response, auto_ack=True
)
def on_response(self, ch, method, props, body):
print("# Response:", json.loads(body))
self.stop()
print("## Handled response. Exiting..")
def run(self):
self.channel.start_consuming()
def stop(self):
self.channel.stop_consuming()
def simulate_request(self):
task = Task("FILESIZE")
document = Document(
{"id": "THIS", "url": "filesize_request_generator.py", "type": "Text"},
{"id": "FileSizeExample", "type": "Software"},
)
self.corr_id = str(uuid.uuid4())
self.channel.basic_publish(
exchange=self.config.EXCHANGE,
routing_key="#.FILESIZE",
properties=pika.BasicProperties(
reply_to="response_queue",
correlation_id=self.corr_id,
),
body=json.dumps(
{
# flipflop between json and object is intentional
# but maybe not most elegant way..
"task": json.loads(task.to_json())["task"],
"document": json.loads(document.to_json()),
}
),
)
if __name__ == "__main__":
fss = filesize_server(cfg)
print("## Simulating request for size of this file")
fss.simulate_request()
print("## Waiting for response. Ctrl+C to exit.")
try:
fss.run()
except KeyboardInterrupt:
fss.stop()