Add cancellation reason tracking with user/system codes and UI

This commit is contained in:
2026-07-08 17:31:22 -04:00
parent 3c1bbce9e7
commit 69c4898a56
9 changed files with 137 additions and 32 deletions
+17 -4
View File
@@ -8,6 +8,7 @@ import (
"os"
"path/filepath"
"sync"
"sync/atomic"
"time"
"github.com/syncserver/internal/config"
@@ -61,7 +62,12 @@ func (e *Engine) Run(ctx context.Context, jobID int64, pairID int64) error {
}
jobCtx, cancel := context.WithCancel(ctx)
enqueueErr := e.queue.Enqueue(pairID, jobID, cancel)
cancelledByUser := atomic.Bool{}
wrappedCancel := func() {
cancelledByUser.Store(true)
cancel()
}
enqueueErr := e.queue.Enqueue(pairID, jobID, wrappedCancel)
if enqueueErr != nil {
return enqueueErr
}
@@ -199,8 +205,15 @@ func (e *Engine) Run(ctx context.Context, jobID int64, pairID int64) error {
if err != nil {
if jobCtx.Err() != nil {
code := "cancelled_shutdown"
msg := "Job was cancelled due to server shutdown"
if cancelledByUser.Load() {
code = "cancelled_user"
msg = "Job was cancelled by user"
}
e.setJobError(jobID, code, msg)
e.setJobStatus(jobID, "cancelled")
e.emit(Event{Type: "status", JobID: jobID, Key: "status", Value: "cancelled"})
e.emit(Event{Type: "status", JobID: jobID, Key: "status", Value: "cancelled", Line: msg})
return jobCtx.Err()
}
e.setJobStatus(jobID, "failed")
@@ -239,9 +252,9 @@ func (e *Engine) resolveSSHKey(machine *models.Machine) (string, error) {
return sshKey.PrivateKeyPath, nil
}
func (e *Engine) Cancel(jobID int64, syncPairID int64) bool {
func (e *Engine) Cancel(jobID int64, syncPairID int64, byUser bool) bool {
if e.queue.IsRunning(syncPairID) {
e.queue.Cancel(syncPairID)
e.queue.Cancel(syncPairID, byUser)
return true
}
return false