Skip to content
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
19 changes: 19 additions & 0 deletions agent/cleanup_job.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import datetime

from agent.job import JobModel, StepModel


def cleanup():
cutoff = datetime.datetime.now() - datetime.timedelta(days=30) # 1 month old data

old_jobs = JobModel.select(JobModel.id).where(JobModel.enqueue < cutoff)

deleted_steps = StepModel.delete().where(StepModel.job.in_(old_jobs)).execute()

deleted_jobs = JobModel.delete().where(JobModel.id.in_(old_jobs)).execute()

print(f"Deleted {deleted_jobs} jobs and {deleted_steps} steps")


if __name__ == "__main__":
cleanup()
24 changes: 24 additions & 0 deletions agent/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,30 @@ def proxysql(password):
Server().setup_proxysql(password)


@setup.command()
def agent_db_cleanup():
from crontab import CronTab

script_directory = os.path.dirname(__file__)
agent_directory = os.path.dirname(os.path.dirname(script_directory))

cron = CronTab(user=True)

command = (
f"cd {agent_directory} && "
f"{agent_directory}/env/bin/python "
f"{agent_directory}/repo/agent/cleanup_job.py"
)

cron.remove_all(command=command)

job = cron.new(command=command)
job.hour.on(2)
job.minute.on(0)

cron.write()


@cli.group()
def run():
pass
Expand Down
Loading