Browse Source

Use systemd.timer instead of a cron job

The rational is that we need to spread the load on the mirrors
that apt update and unattended-upgrades cause. To do so, we
leverage the RandomizeDelay feature of systemd. The other advantage
is that the timer is not run at a fixed daily.daily time but
instead every 24h. This also fixes the problem that the randomized
deplay in the current apt.cron.daily causes other cron jobs to
be deplayed.

A compatibility cron job is also provided for systems that do not
use systemd.

Note that the time is fired two times a day, but the logic inside
of apt.systemd.daily will ensure (via stamp files) that the
servers are hit at most every 24h. Firing two times a day helps
with the worst case update time and it also helps with systems
that are not always on.

LP: #246381, #727685
Closes: #600262, #709675, #663290
Michael Vogt 8 years ago
parent
commit
14669d4b95

+ 8 - 0
debian/apt-daily.service

@@ -0,0 +1,8 @@
+[Unit]
+Description=Daily apt activities
+Documentation=man:apt(8)
+
+[Service]
+Type=oneshot
+ExecStart=/usr/lib/apt/apt.systemd.daily
+

+ 11 - 0
debian/apt-daily.timer

@@ -0,0 +1,11 @@
+[Unit]
+Description=Daily apt activities
+
+[Timer]
+OnCalendar=*-*-* 6:00,18:00
+RandomizedDelaySec=12h
+AccuracySec=1h
+Persistent=true
+
+[Install]
+WantedBy=timers.target

+ 33 - 0
debian/apt.apt-compat.cron.daily

@@ -0,0 +1,33 @@
+#!/bin/sh
+
+set -e
+
+# Systemd systems use a systemd timer unit which is preferable to
+# run. We want to randomize the apt update and unattended-upgrade
+# runs as much as possible to avoid hitting the mirrors all at the
+# same time. The systemd time is better at this than the fixed
+# cron.daily time
+if [ -d /run/systemd/system ]; then
+    exit 0
+fi
+
+# sleep for a random interval of time (default 30min)
+# (some code taken from cron-apt, thanks)
+random_sleep()
+{
+    RandomSleep=1800
+    eval $(apt-config shell RandomSleep APT::Periodic::RandomSleep)
+    if [ $RandomSleep -eq 0 ]; then
+	return
+    fi
+    if [ -z "$RANDOM" ] ; then
+        # A fix for shells that do not have this bash feature.
+	RANDOM=$(( $(dd if=/dev/urandom bs=2 count=1 2> /dev/null | cksum | cut -d' ' -f1) % 32767 ))
+    fi
+    TIME=$(($RANDOM % $RandomSleep))
+    sleep $TIME
+}
+
+# run daily job
+random_sleep
+exec /usr/lib/apt/apt.systemd.daily

+ 2 - 1
debian/apt.dirs

@@ -15,4 +15,5 @@ var/lib/apt/mirrors/partial
 var/lib/apt/periodic
 var/log/apt
 usr/share/bug/apt
-usr/share/bash-completion/completions/
+usr/share/bash-completion/completions/
+lib/systemd/system/

+ 2 - 0
debian/apt.install.in

@@ -5,3 +5,5 @@ scripts/dselect/* usr/lib/dpkg/methods/apt/
 usr/share/locale/*/*/apt.mo
 bin/libapt-private.so.* usr/lib/@DEB_HOST_MULTIARCH@/
 ../completions/bash/* /usr/share/bash-completion/completions/
+../debian/*.service /lib/systemd/system/
+../debian/*.timer /lib/systemd/system/

+ 2 - 1
debian/apt.maintscript

@@ -1,3 +1,4 @@
 rm_conffile /etc/apt/apt.conf.d/20changelog 1.2.4~
-
+# we use a systemd timer unit now
+rm_conffile /etc/cron.daily/apt 1.2.10~
 

+ 1 - 34
debian/apt.cron.daily

@@ -72,18 +72,6 @@
 #      2:  + command outputs     (remove -qq, remove 2>/dev/null, add -d)
 #      3:  + trace on            
 #
-#  APT::Periodic::RandomSleep "1800";
-#  - The apt cron job will delay its execution by a random
-#    time span between zero and 'APT::Periodic::RandomSleep'
-#    seconds.
-#    This is done because otherwise everyone would access the
-#    mirror servers at the same time and put them collectively
-#    under very high strain.
-#    You can set this to '0' if you are using a local mirror and
-#    do not care about the load spikes.
-#    Note that sleeping in the apt job will be delaying the
-#    execution of all subsequent cron.daily jobs.
-#
 
 check_stamp()
 {
@@ -294,25 +282,6 @@ do_cache_backup()
     fi
 }
 
-# sleep for a random interval of time (default 30min)
-# (some code taken from cron-apt, thanks)
-random_sleep()
-{
-    RandomSleep=1800
-    eval $(apt-config shell RandomSleep APT::Periodic::RandomSleep)
-    if [ $RandomSleep -eq 0 ]; then
-	return
-    fi
-    if [ -z "$RANDOM" ] ; then
-        # A fix for shells that do not have this bash feature.
-	RANDOM=$(( $(dd if=/dev/urandom bs=2 count=1 2> /dev/null | cksum | cut -d' ' -f1) % 32767 ))
-    fi
-    TIME=$(($RANDOM % $RandomSleep))
-    debug_echo "sleeping for $TIME seconds"
-    sleep $TIME
-}
-
-
 debug_echo()
 {
     # Display message if $VERBOSE >= 1
@@ -440,9 +409,7 @@ fi
 # deal with BackupArchiveInterval
 do_cache_backup $BackupArchiveInterval
 
-# sleep random amount of time to avoid hitting the 
-# mirrors at the same time
-random_sleep
+# ensure we don't do this on battery
 check_power || exit 0
 
 # include default system language so that "apt-get update" will

+ 1 - 1
debian/control

@@ -9,7 +9,7 @@ Build-Depends: dpkg-dev (>= 1.17.14), debhelper (>= 9.20141010), libdb-dev,
  gettext (>= 0.12), libcurl4-gnutls-dev (>= 7.19.4~),
  zlib1g-dev, libbz2-dev, liblzma-dev, liblz4-dev (>= 0.0~r126),
  xsltproc, docbook-xsl, docbook-xml, po4a (>= 0.34-2),
- autotools-dev, autoconf, automake, libgtest-dev <!nocheck>
+ autotools-dev, autoconf, automake, libgtest-dev <!nocheck>, dh-systemd
 Build-Depends-Indep: doxygen, w3m, graphviz
 Build-Conflicts: autoconf2.13, automake1.4
 Vcs-Git: git://anonscm.debian.org/apt/apt.git

+ 6 - 2
debian/rules

@@ -222,13 +222,17 @@ apt: build-binary build-manpages debian/apt.install
 	#   debian/$@/usr/lib/apt/apt-report-mirror-failure \
 
 	# move the apt-helper in place
-	mv debian/$@/usr/bin/apt-helper debian/$@/usr/lib/apt/apt-helper 
+	mv debian/$@/usr/bin/apt-helper debian/$@/usr/lib/apt/apt-helper
+	# install apt.systemd.daily helper in the right place
+	install -m755 debian/apt.systemd.daily debian/$@/usr/lib/apt/
 
 	dh_bugfiles -p$@
 	dh_lintian -p$@
 	dh_installexamples -p$@ $(BLD)/docs/examples/*
 	dh_installman -p$@ $(wildcard $(patsubst %,doc/en/%.[158],$(apt_MANPAGES)) $(patsubst %,doc/*/%.*.[158],$(apt_MANPAGES)))
-	dh_installcron -p$@
+	dh_installcron -p$@ --name=apt-compat
+	dh_systemd_enable -p$@ apt-daily.timer
+	dh_systemd_start -p$@ apt-daily.timer
 	dh_installdocs -p$@
 	dh_installchangelogs -p$@
 	dh_installlogrotate -p$@