File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -1040,14 +1040,18 @@ async def get_metrics():
10401040
10411041
10421042@app .get ('/maintenance/purge-old-nodes' )
1043- async def purge_handler (current_user : User = Depends (get_current_superuser )):
1043+ async def purge_handler (current_user : User = Depends (get_current_superuser ),
1044+ days : int = 180 ,
1045+ batch_size : int = 1000 ):
10441046 """Purge old nodes from the database
10451047 This is a maintenance operation and should be performed
10461048 only by superusers.
1049+ Accepts GET parameters:
1050+ - days: Number of days to keep nodes, default is 180.
1051+ - batch_size: Number of nodes to delete in one batch, default is 1000.
10471052 """
10481053 metrics .add ('http_requests_total' , 1 )
1049- await purge_old_nodes ()
1050- return "OK"
1054+ return await purge_old_nodes (age_days = days , batch_size = batch_size )
10511055
10521056
10531057versioned_app = VersionedFastAPI (
Original file line number Diff line number Diff line change @@ -48,7 +48,7 @@ def connect_to_db():
4848 return db
4949
5050
51- async def purge_old_nodes (age_days = 180 ):
51+ async def purge_old_nodes (age_days = 180 , batch_size = 1000 ):
5252 """
5353 Purge nodes from the 'nodes' collection that are older than the
5454 specified number of days.
@@ -63,13 +63,22 @@ async def purge_old_nodes(age_days=180):
6363 nodes = db ["nodes" ].find ({
6464 "created" : {"$lt" : date_end }
6565 })
66- # We need to delete node in chunks of 1000,
66+ # We need to delete node in chunks of {batch_size}
6767 # to not block the main thread for too long
68+ deleted = 0
6869 del_batch = []
6970 for node in nodes :
7071 del_batch .append (node ["_id" ])
71- if len (del_batch ) == 1000 :
72+ if len (del_batch ) == batch_size :
73+ deleted += len (del_batch )
7274 purge_ids (db , "nodes" , del_batch )
7375 del_batch = []
7476 if del_batch :
77+ deleted += len (del_batch )
7578 purge_ids (db , "nodes" , del_batch )
79+ db = {
80+ 'response' : 'ok' ,
81+ 'deleted' : deleted ,
82+ 'age_days' : age_days
83+ }
84+ return db
You can’t perform that action at this time.
0 commit comments