Add pause resume and abort controls

This commit is contained in:
Mikei386
2026-06-14 22:34:22 +02:00
parent 82d00bd116
commit 656d569b27
13 changed files with 228 additions and 11 deletions
+59
View File
@@ -12,6 +12,8 @@ import (
"os/exec"
"path/filepath"
"strings"
"sync"
"syscall"
"github.com/backupper-unraid/backupper/internal/model"
)
@@ -23,6 +25,47 @@ type Runner struct {
RuntimeDir string
Secrets SecretGetter
Log *slog.Logger
mu sync.Mutex
processes map[string]*os.Process
}
type runIDKey struct{}
func WithRunID(ctx context.Context, runID string) context.Context {
return context.WithValue(ctx, runIDKey{}, runID)
}
func (r *Runner) Pause(runID string) error { return r.signal(runID, syscall.SIGSTOP) }
func (r *Runner) Resume(runID string) error { return r.signal(runID, syscall.SIGCONT) }
func (r *Runner) signal(runID string, signal syscall.Signal) error {
r.mu.Lock()
process := r.processes[runID]
r.mu.Unlock()
if process == nil {
return errors.New("restic process is not active yet")
}
if err := syscall.Kill(-process.Pid, signal); err != nil {
return fmt.Errorf("signal restic process group: %w", err)
}
return nil
}
func (r *Runner) registerProcess(runID string, process *os.Process) func() {
if runID == "" {
return func() {}
}
r.mu.Lock()
if r.processes == nil {
r.processes = make(map[string]*os.Process)
}
r.processes[runID] = process
r.mu.Unlock()
return func() {
r.mu.Lock()
delete(r.processes, runID)
r.mu.Unlock()
}
}
type Summary struct {
@@ -203,6 +246,19 @@ func (r *Runner) run(ctx context.Context, repo model.Repository, args []string,
}
fullArgs := append(globalArgs, args...)
cmd := exec.CommandContext(ctx, r.Binary, fullArgs...)
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
cmd.Cancel = func() error {
if cmd.Process == nil {
return os.ErrProcessDone
}
if err := syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL); err != nil {
if errors.Is(err, syscall.ESRCH) {
return os.ErrProcessDone
}
return err
}
return nil
}
cmd.Env = []string{"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "HOME=/root", "RESTIC_PASSWORD_FILE=" + passwordPath}
stdout, err := cmd.StdoutPipe()
if err != nil {
@@ -215,6 +271,9 @@ func (r *Runner) run(ctx context.Context, repo model.Repository, args []string,
if err := cmd.Start(); err != nil {
return classify(err)
}
runID, _ := ctx.Value(runIDKey{}).(string)
unregister := r.registerProcess(runID, cmd.Process)
defer unregister()
var captured []byte
done := make(chan struct{})
go func() {
+58
View File
@@ -7,6 +7,7 @@ import (
"path/filepath"
"strings"
"testing"
"time"
"github.com/backupper-unraid/backupper/internal/model"
)
@@ -88,3 +89,60 @@ func TestResticErrorExplainsCiphertextVerification(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}
}
func TestRunnerPausesResumesAndCancelsProcessGroup(t *testing.T) {
dir := t.TempDir()
ticksPath := filepath.Join(dir, "ticks")
script := filepath.Join(dir, "restic")
body := fmt.Sprintf("#!/bin/sh\nwhile :; do printf x >> '%s'; sleep 0.05; done\n", ticksPath)
if err := os.WriteFile(script, []byte(body), 0700); err != nil {
t.Fatal(err)
}
runner := &Runner{Binary: script, RuntimeDir: dir, Secrets: fakeSecrets{"password": "secret"}}
repo := model.Repository{Type: model.RepositoryLocal, Location: "/repo", PasswordRef: "password"}
ctx, cancel := context.WithCancel(WithRunID(context.Background(), "run"))
done := make(chan error, 1)
go func() { done <- runner.run(ctx, repo, []string{"backup"}, nil, nil) }()
waitForSize := func(minimum int64) int64 {
deadline := time.Now().Add(2 * time.Second)
for time.Now().Before(deadline) {
if info, err := os.Stat(ticksPath); err == nil && info.Size() >= minimum {
return info.Size()
}
time.Sleep(10 * time.Millisecond)
}
t.Fatalf("tick file did not reach %d bytes", minimum)
return 0
}
waitForSize(2)
if err := runner.Pause("run"); err != nil {
t.Fatal(err)
}
time.Sleep(100 * time.Millisecond)
pausedSize := waitForSize(1)
time.Sleep(150 * time.Millisecond)
info, err := os.Stat(ticksPath)
if err != nil {
t.Fatal(err)
}
if info.Size() != pausedSize {
t.Fatalf("process continued while paused: size=%d, expected=%d", info.Size(), pausedSize)
}
if err := runner.Resume("run"); err != nil {
t.Fatal(err)
}
waitForSize(pausedSize + 2)
if err := runner.Pause("run"); err != nil {
t.Fatal(err)
}
cancel()
select {
case err := <-done:
if err == nil || !strings.Contains(err.Error(), "cancelled") {
t.Fatalf("cancel result = %v", err)
}
case <-time.After(2 * time.Second):
t.Fatal("paused process group was not cancelled")
}
}