From 5766ff851c5ccb571468c945c997f1bb1285b830 Mon Sep 17 00:00:00 2001 From: Pavel Raiskup Date: Sun, 17 Dec 2023 11:34:50 +0100 Subject: [PATCH] copr-dist-git: run the cleanup cron twice per month https://github.com/fedora-copr/copr/issues/3020 --- ...-dist-git.monthly => copr-dist-git.weekly} | 5 +++ roles/copr/dist_git/files/test-too-soon | 38 +++++++++++++++++++ roles/copr/dist_git/tasks/main.yml | 29 ++++++++++++-- 3 files changed, 68 insertions(+), 4 deletions(-) rename roles/copr/dist_git/files/{copr-dist-git.monthly => copr-dist-git.weekly} (69%) create mode 100755 roles/copr/dist_git/files/test-too-soon diff --git a/roles/copr/dist_git/files/copr-dist-git.monthly b/roles/copr/dist_git/files/copr-dist-git.weekly similarity index 69% rename from roles/copr/dist_git/files/copr-dist-git.monthly rename to roles/copr/dist_git/files/copr-dist-git.weekly index f057673ab6..0abf1a3eca 100644 --- a/roles/copr/dist_git/files/copr-dist-git.monthly +++ b/roles/copr/dist_git/files/copr-dist-git.weekly @@ -4,6 +4,11 @@ # When you uncomment this, all the sources from the dist-git lookaside cache # will be periodically unlinked, except for those sources mentioned in the # last commits of the git repo branches. + +# We run this on Sunday 4.22 every week (default cron weekly timing). To run +# this bi-weekly, we have to skip every second run. +/usr/local/bin/test-too-soon "cleaning-dist-git-data-volume" 10 && exit 0 + runuser -c 'dist-git-clear-tarballs.py' - copr-dist-git # From time to time assure that the CGIT caches are consistent, and that the diff --git a/roles/copr/dist_git/files/test-too-soon b/roles/copr/dist_git/files/test-too-soon new file mode 100755 index 0000000000..8e3ae3b933 --- /dev/null +++ b/roles/copr/dist_git/files/test-too-soon @@ -0,0 +1,38 @@ +#! /bin/bash + +# Return non-zero exit status if this script has been run before more than N +# days (or never). Arguments are $ID (word or description) and number of days. +# Use like 'test-not-before last-dist-git-cleanup-run 10 || do_something' + +id=$1 +days=$2 +file=/var/tmp/ +umask 0077 +dir="/var/tmp/test-too-soon-script-$(id -u -n)" +file=$dir/$id +lock=$file.lock +coeficient="24*3600" +log() { echo >&2 "LOG: $id: $*"; } +mkdir -p "$dir" +( + flock 9 + stamp=$(date +%s) + if test ! -e "$file"; then + log "running for the first time" + echo "$stamp" > "$file" + exit 1 + fi + + old_stamp=$(cat "$file") + + last=$(( (stamp - old_stamp) / ("$coeficient") )) + if test $(( stamp - old_stamp )) -ge $(( days * ("$coeficient") )); then + log "Last run $old_stamp, before $last days, running." + echo "$stamp" > "$file" + exit 1 + else + log "Last run $old_stamp, before $last days, skipping." + exit 0 + fi +) 9>"$lock" +# no more tasks, exit status inherited from sub-shell diff --git a/roles/copr/dist_git/tasks/main.yml b/roles/copr/dist_git/tasks/main.yml index b258a10275..e404f170e2 100644 --- a/roles/copr/dist_git/tasks/main.yml +++ b/roles/copr/dist_git/tasks/main.yml @@ -133,12 +133,33 @@ - name: install .config/copr for copr-dist-git user (required for pruning) template: src="copr.conf" dest="/home/copr-dist-git/.config/copr" owner=copr-dist-git group=copr-dist-git +- name: install the helper cron scripts + copy: + src: "{{ item }}" + dest: "/usr/local/bin/{{ item }}" + mode: '0755' + tags: + - config + - cron + loop: + - test-too-soon + - name: install copr-dist-git.daily cron file copy: src="copr-dist-git.daily" dest="/etc/cron.daily/copr-dist-git" mode=755 tags: - - config + - config + - cron -- name: install copr-dist-git.monthly cron file - copy: src="copr-dist-git.monthly" dest="/etc/cron.monthly/copr-dist-git" mode=755 +- name: install copr-dist-git.weekly cron file + copy: src="copr-dist-git.weekly" dest="/etc/cron.weekly/copr-dist-git" mode=755 tags: - - config + - config + - cron + +- name: drop monthly cron job + file: + path: /etc/cron.weekly/copr-dist-git + state: absent + tags: + - config + - cron