From b2ddc75f0b66debe09b7c6af4837b7817a1a018d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=BD=D0=B0=D0=B1?= Date: Wed, 12 Aug 2020 19:44:34 +0200 Subject: [PATCH] Run GC in gitsrht-periodic This doesn't run gc --aggressive at all, which git-gc(1) says should be run occasionally and provides long-lasting benefits, but it's a step in the right direxion Ref: ~sircmpwn/git.sr.ht#166 --- gitsrht-periodic | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/gitsrht-periodic b/gitsrht-periodic index bfb8fed..afd8204 100755 --- a/gitsrht-periodic +++ b/gitsrht-periodic @@ -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() -- 2.38.4