Skip to content

Commit

Permalink
Identifying if logfire is causing the excess memory usage
Browse files Browse the repository at this point in the history
  • Loading branch information
HenryTraill committed Nov 18, 2024
1 parent 737db33 commit 952e8d7
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions chronos/worker.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import gc
import hashlib
import hmac
import json
Expand Down Expand Up @@ -228,25 +229,27 @@ def get_count(date_to_delete_before: datetime) -> int:

@celery_app.task
def _delete_old_logs_job():
with logfire.span('Started to delete old logs'):
with Session(engine) as db:
# Get all logs older than 15 days
date_to_delete_before = datetime.now(UTC) - timedelta(days=15)
with logfire.span(
'Deleting webhooks for {date_to_delete_before=}', date_to_delete_before=date_to_delete_before
):
count = get_count(date_to_delete_before)
delete_limit = 4999
while count > 0:
app_logger.info(f'Deleting {count} logs')
deleting_count = min(count, delete_limit)
with logfire.span('Deleting {count=}', count=deleting_count):
logs_to_delete = db.exec(
select(WebhookLog).where(WebhookLog.timestamp < date_to_delete_before).limit(delete_limit)
).all()
delete_statement = delete(WebhookLog).where(WebhookLog.id.in_(log.id for log in logs_to_delete))
db.exec(delete_statement)
db.commit()
count -= delete_limit
# with logfire.span('Started to delete old logs'):
with Session(engine) as db:
# Get all logs older than 15 days
date_to_delete_before = datetime.now(UTC) - timedelta(days=15)
# with logfire.span(
# 'Deleting webhooks for {date_to_delete_before=}', date_to_delete_before=date_to_delete_before
# ):
count = get_count(date_to_delete_before)
delete_limit = 4999
while count > 0:
app_logger.info(f'Deleting {count} logs')
logs_to_delete = db.exec(
select(WebhookLog).where(WebhookLog.timestamp < date_to_delete_before).limit(delete_limit)
).all()
delete_statement = delete(WebhookLog).where(WebhookLog.id.in_(log.id for log in logs_to_delete))
db.exec(delete_statement)
db.commit()
count -= delete_limit

del logs_to_delete
del delete_statement
gc.collect()

cache.delete(DELETE_JOBS_KEY)

0 comments on commit 952e8d7

Please sign in to comment.