-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_update.py
More file actions
45 lines (36 loc) · 1.54 KB
/
db_update.py
File metadata and controls
45 lines (36 loc) · 1.54 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
39
40
41
42
43
44
45
from datetime import datetime
from sqlalchemy import create_engine, MetaData, Table, Column, Integer, String, Boolean, DateTime
from sqlalchemy.sql import select, update, text
import pytz, time
import os
from sqlalchemy.sql import func
from src import db
from sqlalchemy.orm import sessionmaker
#This is run in its own thread, initiated in src/__init__.py, and runs every 15 minutes to update the db. It checks if a box is in use, and if the booked_until_interval15 is in the past, and if so, sets in_use to false, booked_until_interval15 to None and user_id to None.
class update_db():
def update_box_script():
engine = create_engine(os.getenv("DATABASE_URL"))
metadata = MetaData()
Session = sessionmaker(bind=engine)
session = Session()
box_tbl = Table('boxes', metadata, autoload_with=engine)
# Get the current time in server time --> UTC
server_time_all = session.execute(text("SELECT CURRENT_TIMESTAMP"))
server_time = server_time_all.fetchone()[0]
query = select(box_tbl).where(
(box_tbl.c.in_use == True) &
(box_tbl.c.booked_until_interval15 < server_time)
)
results = session.execute(query).fetchall()
# Update in_use and booked..
for row in results:
update_query = update(box_tbl).where(box_tbl.c.id == row.id).values(in_use=False, booked_until_interval15=None, user_id=None)
session.execute(update_query)
session.commit()
session.close()
# Run the update logic every 15 minutes
def update_db_infinite():
while (1):
print("\nUpdating DB\n")
update_db.update_box_script()
time.sleep(900)