-
Notifications
You must be signed in to change notification settings - Fork 2
/
scheduler.py
57 lines (48 loc) · 2.22 KB
/
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from db_details import create_conn
from datetime import datetime, timedelta
import schedule
import time
# Change to minutes later
def update_order_status():
print("Updating order status...")
conn = create_conn()
cursor = conn.cursor(buffered=True)
# Get orders that are still in progress
cursor.execute("SELECT order_id, order_time, preparation_time, transit_time FROM orders WHERE status = 'Being Prepared' OR status = 'In Transit'")
orders = cursor.fetchall()
all_orders = {}
for order_id, order_time, preparation_time, transit_time in orders:
if order_id not in all_orders:
all_orders[order_id] = {
"order_time" : order_time,
"prep_time" : preparation_time,
"transit_time" : transit_time
}
else:
old_prep_time = all_orders[order_id]["prep_time"]
new_prep_time = old_prep_time + preparation_time
all_orders[order_id]["prep_time"] = new_prep_time
for order_id, order_info in all_orders.items():
time_at_transit = order_info["order_time"] + timedelta(seconds=order_info["prep_time"])
time_fully_delivered = time_at_transit + timedelta(seconds=order_info["transit_time"])
current_time = datetime.now()
print(f"Transit: {time_at_transit} || Delivered: {time_fully_delivered} || Current Time: {current_time}")
# Check if the current time has passed the expected completion time
if current_time >= time_at_transit and current_time < time_fully_delivered:
# Update the status to 'In Transit'
cursor.execute("UPDATE orders SET status = 'In Transit' WHERE order_id = %s", (order_id,))
conn.commit()
print("In Transit")
elif current_time >= time_fully_delivered:
# Update the status to 'Delivered'
cursor.execute("UPDATE orders SET status = 'Delivered' WHERE order_id = %s", (order_id,))
conn.commit()
print("Delivered")
cursor.close()
conn.close()
# Schedule the update_order_status function to run every 15 seconds
schedule.every(15).seconds.do(update_order_status)
# Run the scheduler indefinitely
while True:
schedule.run_pending()
time.sleep(1)