Skip to content

Commit b4bf02c

Browse files
committed
[mock_uss] Regular cleanup of outdated flights
1 parent dade5df commit b4bf02c

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

monitoring/mock_uss/f3548v21/routes_scd.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ def scdsc_notify_operational_intent_details_changed():
139139
conflicts=Conflict.Single, # TODO: detect multiple conflicts
140140
)
141141
)
142-
tx.value.cleanup_notifications()
143142

144143
# Do nothing else because this USS is unsophisticated and polls the DSS for
145144
# every change in its operational intents

monitoring/mock_uss/flights/database.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from implicitdict import ImplicitDict, Optional
66
from uas_standards.astm.f3548.v21.api import OperationalIntent
77

8+
from monitoring.mock_uss.app import webapp
89
from monitoring.mock_uss.user_interactions.notifications import UserNotification
910
from monitoring.monitorlib.clients.flight_planning.flight_info import FlightInfo
1011
from monitoring.monitorlib.clients.mock_uss.mock_uss_scd_injection_api import (
@@ -14,6 +15,9 @@
1415

1516
DEADLOCK_TIMEOUT = timedelta(seconds=5)
1617
NOTIFICATIONS_LIMIT = timedelta(hours=1)
18+
DB_CLEANUP_INTERVAL = timedelta(hours=1)
19+
FLIGHTS_LIMIT = timedelta(hours=1)
20+
OPERATIONAL_INTENTS_LIMIT = timedelta(hours=1)
1721

1822

1923
class FlightRecord(ImplicitDict):
@@ -42,8 +46,49 @@ def cleanup_notifications(self):
4246
> arrow.utcnow().datetime
4347
]
4448

49+
def cleanup_flights(self):
50+
to_cleanup = []
51+
52+
for flight_id, flight in self.flights.items():
53+
if (
54+
flight
55+
and not flight.locked
56+
and flight.op_intent.reference.time_end.value.datetime + FLIGHTS_LIMIT
57+
< arrow.utcnow().datetime
58+
):
59+
to_cleanup.append(flight_id)
60+
61+
for flight_id in to_cleanup:
62+
del self.flights[flight_id]
63+
64+
def cleanup_operational_intents(self):
65+
to_cleanup = []
66+
67+
for op_id, op_intent in self.cached_operations.items():
68+
if (
69+
op_intent.reference.time_end.value.datetime + FLIGHTS_LIMIT
70+
< arrow.utcnow().datetime
71+
):
72+
to_cleanup.append(op_id)
73+
74+
for op_id in to_cleanup:
75+
del self.cached_operations[op_id]
76+
4577

4678
db = SynchronizedValue[Database](
4779
Database(),
4880
decoder=lambda b: ImplicitDict.parse(json.loads(b.decode("utf-8")), Database),
4981
)
82+
83+
TASK_DATABASE_CLEANUP = "flights database cleanup"
84+
85+
86+
@webapp.periodic_task(TASK_DATABASE_CLEANUP)
87+
def database_cleanup() -> None:
88+
with db.transact() as tx:
89+
tx.value.cleanup_notifications()
90+
tx.value.cleanup_flights()
91+
tx.value.cleanup_operational_intents()
92+
93+
94+
webapp.set_task_period(TASK_DATABASE_CLEANUP, DB_CLEANUP_INTERVAL)

monitoring/mock_uss/scd_injection/routes_injection.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,6 @@ def unsuccessful(
245245
)
246246
)
247247

248-
tx.value.cleanup_notifications()
249-
250248
step_name = "returning final successful result"
251249
log("Complete.")
252250

0 commit comments

Comments
 (0)