-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_scheduler.py
40 lines (36 loc) · 1.4 KB
/
send_scheduler.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
import time
import pika
import logging
import broker
from car import Car
INTERVAL_SEC = 1.0
def send_scheduler(car: Car):
while True:
try:
with broker.get_channel() as channel:
# sleep na sterydach -> https://stackoverflow.com/a/54161792/7598740
cptr = 0
time_start = time.time()
time_init = time.time()
while True:
try:
car.fill_timestamp(time.time_ns() // 1_000_000)
finalMessage = car.to_bytes()
channel.basic_publish(
"amq.direct",
"car",
finalMessage,
pika.BasicProperties(
delivery_mode=pika.DeliveryMode.Persistent
)
)
car.reset()
logging.info("[Send Scheduler] Messsage sent to broker")
except Exception as e:
logging.warning(f"Failed creating final message: " + str(e))
cptr += 1
time_start = time.time()
time.sleep(((time_init + (INTERVAL_SEC * cptr)) - time_start ))
except:
logging.warning("Broker connection can't be established")
time.sleep(0)