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() {