@@ 1,4 1,8 @@
#!/usr/bin/env python3
+import math
+import random
+import sqlalchemy as sa
+import subprocess
from srht.config import cfg
from srht.database import DbSession
from gitsrht.repos import GitRepoApi
@@ 21,4 25,21 @@ def cleanup_autocreated():
db.session.delete(r)
db.session.commit()
+def gc():
+ repo_count = Repository.query.count()
+
+ # *srht-periodic scripts are run every twenty minutes,
+ # this gives us 504 runs over the course of a week;
+ # hence, if we GC a 504th of the repository count each time,
+ # on average, we will have GCd every repo around once a week.
+ limit = int(math.ceil(repo_count / (7 * 24 * 60 / 20)))
+
+ repos = (Repository.query
+ .offset(random.randrange(0, repo_count + 1 - limit))
+ .limit(limit)).all()
+ for r in repos:
+ subprocess.run(["git", "-C", r.path, "gc", "--quiet"],
+ stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
+
cleanup_autocreated()
+gc()