Skip to content

Commit

Permalink
Use systemd.timer instead of a cron job
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mvo5 committed Mar 17, 2016
1 parent a38cec8 commit 4ee4b0b
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 38 deletions.
10 changes: 10 additions & 0 deletions debian/apt-daily.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[Unit]
Description=Apts daily jobs
Documentation=man:apt(8)

[Service]
Type=oneshot
ExecStart=/usr/lib/apt/apt.systemd.daily

[Install]
WantedBy=multi-user.target
11 changes: 11 additions & 0 deletions debian/apt-daily.timer
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[Unit]
Description=Runs apts daily activities

[Timer]
OnActiveSec=10min
OnUnitInactiveSec=daily
RandomizedDelaySec=1h
Unit=apt-daily.service

[Install]
WantedBy=multi-user.target
3 changes: 2 additions & 1 deletion debian/apt.dirs
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 2 additions & 0 deletions debian/apt.install.in
Original file line number Diff line number Diff line change
Expand Up @@ -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/
3 changes: 2 additions & 1 deletion debian/apt.maintscript
Original file line number Diff line number Diff line change
@@ -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

35 changes: 1 addition & 34 deletions debian/apt.cron.daily → debian/apt.systemd.daily
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions debian/rules
Original file line number Diff line number Diff line change
Expand Up @@ -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_systemd_enable -p$@ apt-daily.timer apt-daily.service
dh_systemd_start -p$@ apt-daily.timer
dh_systemd_start -p$@ --no-start apt-daily.service
dh_installdocs -p$@
dh_installchangelogs -p$@
dh_installlogrotate -p$@
Expand Down

0 comments on commit 4ee4b0b

Please sign in to comment.