Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

fix(rexctl): spread cron auto-upgrades over the window (#3667)

Hampusa342f77ace43
diff --git a/fluxer_rexctl/cmd/autoupgrade.go b/fluxer_rexctl/cmd/autoupgrade.go
index f15c1508f..bf6af776f 100644
--- a/fluxer_rexctl/cmd/autoupgrade.go
+++ b/fluxer_rexctl/cmd/autoupgrade.go
@@ -28,7 +28,6 @@ import (
const (
autoUpgradeLog = "auto-upgrade.log"
autoUpgradeDefault = "04:00"
- autoUpgradeWindow = 30 * time.Minute
notifyTimeout = 15 * time.Second
)
@@ -155,7 +154,11 @@ func runAutoUpgradeOn(ctx context.Context, app *App, at, notify string) error {
return ui.Wrap(err, "Couldn't save your instance's state.", "Check that you can write to "+inst.Dir+", then try again.")
}
}
- app.UI.OK("rexctl upgrades Fluxer every day %s.", autoUpgradeWhen(a.Scheduler, at))
+ start, err := a.Start()
+ if err != nil {
+ return ui.Usage("Pass --at as a time of day like 04:00.")
+ }
+ app.UI.OK("rexctl upgrades Fluxer every day %s.", autoUpgradeWhen(a.Scheduler, start))
if notify != "" {
app.UI.Info("rexctl tells %s when an upgrade installs a new release or fails.", maskURL(notify))
}
@@ -271,7 +274,7 @@ func autoUpgradeWhen(s host.Scheduler, at string) string {
if err != nil || s != host.Systemd {
return "at " + at
}
- return fmt.Sprintf("between %s and %s", at, t.Add(autoUpgradeWindow).Format("15:04"))
+ return fmt.Sprintf("between %s and %s", at, t.Add(host.UpgradeWindow).Format("15:04"))
}
func autoUpgradeLogs(a host.AutoUpgrade) string {
diff --git a/fluxer_rexctl/internal/host/autoupgrade.go b/fluxer_rexctl/internal/host/autoupgrade.go
index 42f6cebc7..95618667c 100644
--- a/fluxer_rexctl/internal/host/autoupgrade.go
+++ b/fluxer_rexctl/internal/host/autoupgrade.go
@@ -7,6 +7,7 @@ import (
"bytes"
"context"
"fmt"
+ "hash/fnv"
"os"
"os/exec"
"os/user"
@@ -26,12 +27,13 @@ const (
const (
cronDir = "/etc/cron.d"
- upgradeDelay = "30min"
cronPath = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
cronExitPrefix = "rexctl exited with "
systemdTimeLayout = "Mon 2006-01-02 15:04:05 MST"
)
+const UpgradeWindow = 30 * time.Minute
+
func UpgradeScheduler() Scheduler {
if BootUnitSupported() {
return Systemd
@@ -104,6 +106,25 @@ func (a AutoUpgrade) clock() (int, int, error) {
return t.Hour(), t.Minute(), nil
}
+func (a AutoUpgrade) start() (int, int, error) {
+ hour, minute, err := a.clock()
+ if err != nil || a.Scheduler != Cron {
+ return hour, minute, err
+ }
+ h := fnv.New32a()
+ h.Write([]byte(a.Project + "\x00" + a.Dir))
+ m := (hour*60 + minute + int(h.Sum32()%uint32(UpgradeWindow.Minutes()))) % (24 * 60)
+ return m / 60, m % 60, nil
+}
+
+func (a AutoUpgrade) Start() (string, error) {
+ hour, minute, err := a.start()
+ if err != nil {
+ return "", err
+ }
+ return fmt.Sprintf("%02d:%02d", hour, minute), nil
+}
+
func (a AutoUpgrade) service(runAs string) string {
after := []string{a.Project + ".service"}
var b strings.Builder
@@ -132,7 +153,7 @@ func (a AutoUpgrade) timer(hour, minute int) string {
fmt.Fprintf(&b, "Description=Upgrade Fluxer in %s every day\n", strings.ReplaceAll(a.Dir, "%", "%%"))
b.WriteString("\n[Timer]\n")
fmt.Fprintf(&b, "OnCalendar=*-*-* %02d:%02d:00\n", hour, minute)
- fmt.Fprintf(&b, "RandomizedDelaySec=%s\n", upgradeDelay)
+ fmt.Fprintf(&b, "RandomizedDelaySec=%dmin\n", int(UpgradeWindow.Minutes()))
b.WriteString("Persistent=true\n")
b.WriteString("\n[Install]\n")
b.WriteString("WantedBy=timers.target\n")
@@ -160,7 +181,7 @@ func (a AutoUpgrade) files() ([][]byte, error) {
if !filepath.IsAbs(a.Binary) || !filepath.IsAbs(a.Dir) {
return nil, fmt.Errorf("host: automatic upgrades need absolute paths, got %q and %q", a.Binary, a.Dir)
}
- hour, minute, err := a.clock()
+ hour, minute, err := a.start()
if err != nil {
return nil, err
}
diff --git a/fluxer_rexctl/internal/host/autoupgrade_test.go b/fluxer_rexctl/internal/host/autoupgrade_test.go
index dc4af181e..57823270a 100644
--- a/fluxer_rexctl/internal/host/autoupgrade_test.go
+++ b/fluxer_rexctl/internal/host/autoupgrade_test.go
@@ -111,6 +111,44 @@ func TestAutoUpgradeRefusesBadInput(t *testing.T) {
}
}
+func TestAutoUpgradeCronStart(t *testing.T) {
+ a := AutoUpgrade{Scheduler: Cron, Project: "fluxer", Dir: "/srv/fluxer", Binary: "/usr/local/bin/rexctl", At: "04:00", Log: "/srv/fluxer/.rexctl/auto-upgrade.log"}
+ start, err := a.Start()
+ if err != nil {
+ t.Fatal(err)
+ }
+ if again, _ := a.Start(); again != start {
+ t.Errorf("Start() = %q then %q, want the same time every time", start, again)
+ }
+ if start < "04:00" || start >= "04:30" {
+ t.Errorf("Start() = %q, want a time from 04:00 to 04:29", start)
+ }
+ files, err := a.files()
+ if err != nil || cronAt(files[0]) != start {
+ t.Errorf("cron file runs at %q, want %q (%v)", cronAt(files[0]), start, err)
+ }
+ starts := map[string]bool{}
+ for _, dir := range []string{"/srv/a", "/srv/b", "/srv/c", "/srv/d", "/srv/e", "/srv/f"} {
+ b := a
+ b.Dir = dir
+ s, _ := b.Start()
+ starts[s] = true
+ }
+ if len(starts) < 2 {
+ t.Errorf("six instances all start at %v, want them spread out", starts)
+ }
+ late := a
+ late.At = "23:59"
+ if s, _ := late.Start(); s >= "00:29" && s < "23:59" {
+ t.Errorf("Start() for 23:59 = %q, want it to wrap past midnight", s)
+ }
+ sd := a
+ sd.Scheduler = Systemd
+ if s, _ := sd.Start(); s != "04:00" {
+ t.Errorf("systemd Start() = %q, want 04:00 because the timer adds its own delay", s)
+ }
+}
+
func TestCronExit(t *testing.T) {
log := filepath.Join(t.TempDir(), "auto-upgrade.log")
if ran, _ := cronExit(log); ran {