-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
137 lines (111 loc) · 3.95 KB
/
main.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
from pathlib import Path
from retry import retry
from zipfile import ZipFile, BadZipFile
import pulsar
import os
import time
from viaa.configuration import ConfigParser
from viaa.observability import logging
from cloudevents.events import (
CEMessageMode,
Event,
EventAttributes,
EventOutcome,
PulsarBinding,
)
configParser = ConfigParser()
log = logging.get_logger(__name__, config=configParser)
APP_NAME = "unzip-service"
client = pulsar.Client(
f"pulsar://{configParser.app_cfg['pulsar']['host']}:{configParser.app_cfg['pulsar']['port']}"
)
@retry(pulsar.ConnectError, tries=10, delay=1, backoff=2)
def create_producer():
return client.create_producer(
configParser.app_cfg["unzip-service"]["producer_topic"]
)
@retry(pulsar.ConnectError, tries=10, delay=1, backoff=2)
def subscribe():
return client.subscribe(
configParser.app_cfg["unzip-service"]["consumer_topic"],
subscription_name=APP_NAME,
)
producer = create_producer()
consumer = subscribe()
def handle_event(event: Event) -> bool:
"""
Handles an incoming pulsar event.
If the event has a succesful outcome, the incoming zip will be extracted and an event will be produced.
"""
if not event.has_successful_outcome():
log.info(f"Dropping non succesful event: {event.get_data()}")
return False
log.debug(f"Incoming event: {event.get_data()}")
try:
filename = event.get_data()["destination"]
except KeyError as e:
error_msg = (
f"The data payload of the incoming event is not conform. Missing key: {e}."
)
log.warning(error_msg)
outcome = EventOutcome.FAIL
data = {
"source": "N/A",
"message": error_msg,
}
send_event(data, outcome, event.correlation_id)
return False
# Use the folder of the incoming zip file to derive a part of the target folder.
# Example: /path/to/sipin/incoming/file.zip -> /path/to/sipin/
directory_base_path = Path(filename).parents[1]
basename = os.path.basename(filename)
extract_path = os.path.join(
directory_base_path,
configParser.app_cfg["unzip-service"]["target_folder"],
basename,
)
data = {"destination": extract_path, "source": filename}
try:
with ZipFile(filename, "r") as zipObj:
# Extract all the contents of zip file in the target directory
zipObj.extractall(extract_path)
outcome = EventOutcome.SUCCESS
data["message"] = f"The bag '{basename}' unzipped in '{extract_path}'"
log.info(data["message"])
except BadZipFile:
outcome = EventOutcome.FAIL
data["message"] = f"{filename} is not a a valid zipfile."
log.warning(data["message"])
except OSError as e:
outcome = EventOutcome.FAIL
data["message"] = f"Error when unzipping: {str(e)}"
log.warning(data["message"])
send_event(data, outcome, event.correlation_id)
return outcome == EventOutcome.SUCCESS
def send_event(data: dict, outcome: EventOutcome, correlation_id: str):
attributes = EventAttributes(
type=configParser.app_cfg["unzip-service"]["producer_topic"],
source=APP_NAME,
subject=data["source"],
correlation_id=correlation_id,
outcome=outcome,
)
event = Event(attributes, data)
create_msg = PulsarBinding.to_protocol(event, CEMessageMode.STRUCTURED.value)
producer.send(
create_msg.data,
properties=create_msg.attributes,
event_timestamp=event.get_event_time_as_int(),
)
if __name__ == "__main__":
try:
while True:
msg = consumer.receive()
event = PulsarBinding.from_protocol(msg)
result = handle_event(event)
consumer.acknowledge(msg)
if result:
time.sleep(int(configParser.app_cfg["unzip-service"]["sleep_time"]))
except KeyboardInterrupt:
client.close()
exit()