Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(api) Add maintenance function to purge old data #599

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
from fastapi_users import FastAPIUsers
from beanie import PydanticObjectId
from pydantic import BaseModel
from kernelci.api.models import (

Check failure on line 39 in api/main.py

View workflow job for this annotation

GitHub Actions / Lint

Unable to import 'kernelci.api.models'
Node,
Hierarchy,
PublishEvent,
Expand All @@ -61,6 +61,7 @@
UserGroup,
)
from .metrics import Metrics
from .maintenance import purge_old_nodes


@asynccontextmanager
Expand Down Expand Up @@ -1088,6 +1089,17 @@
return PlainTextResponse(response)


@app.get('/maintenance/purge-old-nodes')
async def purge_old_nodes(current_user: User = Depends(get_current_superuser)):

Check failure on line 1093 in api/main.py

View workflow job for this annotation

GitHub Actions / Lint

function already defined line 64
"""Purge old nodes from the database
This is a maintenance operation and should be performed
only by superusers.
"""
metrics.add('http_requests_total', 1)
await purge_old_nodes()
return "OK"


versioned_app = VersionedFastAPI(
app,
version_format='{major}',
Expand Down
33 changes: 33 additions & 0 deletions api/maintenance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from pymongo import MongoClient

Check warning on line 1 in api/maintenance.py

View workflow job for this annotation

GitHub Actions / Lint

Missing module docstring
import datetime

Check warning on line 2 in api/maintenance.py

View workflow job for this annotation

GitHub Actions / Lint

standard import "import datetime" should be placed before "from pymongo import MongoClient"
import os

Check warning on line 3 in api/maintenance.py

View workflow job for this annotation

GitHub Actions / Lint

standard import "import os" should be placed before "from pymongo import MongoClient"


def purge_ids(db, collection, ids):

Check warning on line 6 in api/maintenance.py

View workflow job for this annotation

GitHub Actions / Lint

Argument name "db" doesn't conform to snake_case naming style

Check warning on line 6 in api/maintenance.py

View workflow job for this annotation

GitHub Actions / Lint

Missing function or method docstring
print("Purging", len(ids), "from", collection)
db[collection].delete_many({"_id": {"$in": ids}})


def connect_to_db():

Check warning on line 11 in api/maintenance.py

View workflow job for this annotation

GitHub Actions / Lint

Missing function or method docstring
mongo_service = os.environ["MONGO_SERVICE"]
if not mongo_service:
raise ValueError("MONGO_SERVICE environment variable is not set")
client = MongoClient(mongo_service)
db = client["kernelci"]

Check warning on line 16 in api/maintenance.py

View workflow job for this annotation

GitHub Actions / Lint

Variable name "db" doesn't conform to snake_case naming style
return db


async def purge_old_nodes(age_days=180):

Check warning on line 20 in api/maintenance.py

View workflow job for this annotation

GitHub Actions / Lint

Missing function or method docstring
date_end = datetime.datetime.today() - datetime.timedelta(days=age_days)
db = connect_to_db()

Check warning on line 22 in api/maintenance.py

View workflow job for this annotation

GitHub Actions / Lint

Variable name "db" doesn't conform to snake_case naming style
nodes = db["nodes"].find({"created": {"$lt": date_end}})
# We need to delete node in chunks of 1000,
# to not block the main thread for too long
del_batch = []
for node in nodes:
del_batch.append(node["_id"])
if len(del_batch) == 1000:
purge_ids(db, "nodes", del_batch)
del_batch = []
if del_batch:
purge_ids(db, "nodes", del_batch)
6 changes: 5 additions & 1 deletion kube/aks/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ spec:
labels:
app: api
spec:
initContainers:
- name: wait-for-mongo
image: busybox
command: ["sh", "-c", "until nc -z mongo.kernelci-api.svc.cluster.local 27017; do echo waiting for mongo; sleep 2; done"]
containers:
- name: api
image: kernelci/api
image: kernelci/kernelci:api
imagePullPolicy: Always
resources:
requests:
Expand Down
151 changes: 0 additions & 151 deletions kube/minikube/README.md

This file was deleted.

79 changes: 0 additions & 79 deletions kube/minikube/api-deployment.yaml

This file was deleted.

16 changes: 0 additions & 16 deletions kube/minikube/configmap/api-configmap.yaml

This file was deleted.

32 changes: 0 additions & 32 deletions kube/minikube/deployments/db-deployment.yaml

This file was deleted.

32 changes: 0 additions & 32 deletions kube/minikube/deployments/redis-deployment.yaml

This file was deleted.

Loading
Loading