Skip to content

Celery: cleanup pidbox keys #10002

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

Merged
merged 2 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
30 changes: 30 additions & 0 deletions readthedocs/core/tasks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Basic tasks."""

import math

import structlog
from django.conf import settings
from django.core.mail import EmailMultiAlternatives
Expand Down Expand Up @@ -56,3 +58,31 @@ def clear_persistent_messages():
expires__lt=timezone.now(),
)
expired_messages.delete()


@app.task(queue="web")
def cleanup_pidbox_keys():
"""
Remove "pidbox" objects from Redis.

Celery creates some "pidbox" objects with TTL=-1,
producing a OOM on our Redis instance.

This task is executed periodically to remove "pdibox" objects with an
idletime bigger than 5 minutes from Redis and free some RAM.

https://github.com/celery/celery/issues/6089
https://github.com/readthedocs/readthedocs-ops/issues/1260

"""
total_memory = 0
keys = app.backend.client.keys("*reply.celery.pidbox*")
for key in keys:
idletime = app.backend.client.object("idletime", key) # seconds
memory = math.ceil(app.backend.client.memory_usage(key) / 1024 / 1024) # Mb
total_memory += memory

if idletime > (60 * 15): # 15 minutes
app.backend.client.delete([key])

log.info("Redis pidbox objects.", memory=total_memory, keys=len(keys))
7 changes: 6 additions & 1 deletion readthedocs/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,12 @@ def TEMPLATES(self):
'task': 'readthedocs.domains.tasks.email_pending_custom_domains',
'schedule': crontab(minute=0, hour=3),
'options': {'queue': 'web'},
}
},
'every-15m-delete-pidbox-objects': {
'task': 'readthedocs.core.tasks.cleanup_pidbox_keys',
'schedule': crontab(minute='*/15'),
'options': {'queue': 'web'},
},
}

MULTIPLE_BUILD_SERVERS = [CELERY_DEFAULT_QUEUE]
Expand Down