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

Configurable event retention #131

Merged
merged 5 commits into from
Aug 28, 2024
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
39 changes: 39 additions & 0 deletions ayon_server/background/log_cleaner.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,44 @@ async def run(self):
# Repeat every hour hours
await asyncio.sleep(3600)

# TODO
if ayonconfig.event_retention_days is not None:
num_days = ayonconfig.event_retention_days
await self.clear_events(num_days=num_days)

async def clear_events(
self, condition: str | None = None, num_days: int = 90
) -> None:
# Delete events in batches of 1000
# to avoid locking the table for too long and potentially
# causing timeouts in the application

conditions = [f"updated_at < NOW() - INTERVAL '{num_days} days'"]
if condition:
conditions.append(condition)

while True:
query = f"""
WITH deleted AS (
DELETE FROM events WHERE id IN (
SELECT id FROM events
WHERE {' AND '.join(conditions)}
ORDER BY updated_at ASC
LIMIT 1000
)
SELECT count(*) as del FROM deleted;
"""

res = await Postgres.fetch(query)

if res:
deleted = res[0]["del"]
if deleted:
logging.info(f"Deleted {deleted} old event entries")
continue

await asyncio.sleep(1)
break


log_cleaner = LogCleaner()
6 changes: 6 additions & 0 deletions ayon_server/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ class AyonConfig(BaseModel):
description="Number of days to keep logs in the event log",
)

event_retention_days: int | None = Field(
default=None,
description="Number of days to keep events in the event log",
example=90,
)

ynput_cloud_api_url: str | None = Field(
"https://im.ynput.cloud",
description="YnputConnect URL",
Expand Down