diff --git a/api/main.py b/api/main.py index 306b3595..b8735ec5 100644 --- a/api/main.py +++ b/api/main.py @@ -1040,14 +1040,18 @@ async def get_metrics(): @app.get('/maintenance/purge-old-nodes') -async def purge_handler(current_user: User = Depends(get_current_superuser)): +async def purge_handler(current_user: User = Depends(get_current_superuser), + days: int = 180, + batch_size: int = 1000): """Purge old nodes from the database This is a maintenance operation and should be performed only by superusers. + Accepts GET parameters: + - days: Number of days to keep nodes, default is 180. + - batch_size: Number of nodes to delete in one batch, default is 1000. """ metrics.add('http_requests_total', 1) - await purge_old_nodes() - return "OK" + return await purge_old_nodes(age_days=days, batch_size=batch_size) versioned_app = VersionedFastAPI( diff --git a/api/maintenance.py b/api/maintenance.py index 1b3b0af2..715e411a 100644 --- a/api/maintenance.py +++ b/api/maintenance.py @@ -48,7 +48,7 @@ def connect_to_db(): return db -async def purge_old_nodes(age_days=180): +async def purge_old_nodes(age_days=180, batch_size=1000): """ Purge nodes from the 'nodes' collection that are older than the specified number of days. @@ -63,13 +63,22 @@ async def purge_old_nodes(age_days=180): nodes = db["nodes"].find({ "created": {"$lt": date_end} }) - # We need to delete node in chunks of 1000, + # We need to delete node in chunks of {batch_size} # to not block the main thread for too long + deleted = 0 del_batch = [] for node in nodes: del_batch.append(node["_id"]) - if len(del_batch) == 1000: + if len(del_batch) == batch_size: + deleted += len(del_batch) purge_ids(db, "nodes", del_batch) del_batch = [] if del_batch: + deleted += len(del_batch) purge_ids(db, "nodes", del_batch) + db = { + 'response': 'ok', + 'deleted': deleted, + 'age_days': age_days + } + return db