package orchestrator import ( "context" "fmt" "time" v1 "github.com/garethgeorge/resticui/gen/go/v1" "github.com/garethgeorge/resticui/internal/oplog" "github.com/hashicorp/go-multierror" ) type Task interface { Name() string // huamn readable name for this task. Next(now time.Time) *time.Time // when this task would like to be run. Run(ctx context.Context) error // run the task. Cancel(withStatus v1.OperationStatus) error // cancel the task's execution with the given status (either STATUS_USER_CANCELLED or STATUS_SYSTEM_CANCELLED). } // WithOperation is a utility that creates an operation to track the function's execution. // timestamps are automatically added and the status is automatically updated if an error occurs. func WithOperation(oplog *oplog.OpLog, op *v1.Operation, do func() error) error { if op.Id != 0 { if err := oplog.Update(op); err != nil { return fmt.Errorf("failed to add operation to oplog: %w", err) } } else { if err := oplog.Add(op); err != nil { return fmt.Errorf("failed to add operation to oplog: %w", err) } } if op.Status == v1.OperationStatus_STATUS_PENDING || op.Status == v1.OperationStatus_STATUS_UNKNOWN { op.Status = v1.OperationStatus_STATUS_INPROGRESS } err := do() if err != nil { op.Status = v1.OperationStatus_STATUS_ERROR op.DisplayMessage = err.Error() } op.UnixTimeEndMs = curTimeMillis() if op.Status == v1.OperationStatus_STATUS_INPROGRESS { op.Status = v1.OperationStatus_STATUS_SUCCESS } if e := oplog.Update(op); e != nil { return multierror.Append(err, fmt.Errorf("failed to update operation in oplog: %w", e)) } return err } func timeToUnixMillis(t time.Time) int64 { return t.Unix()*1000 + int64(t.Nanosecond()/1000000) } func curTimeMillis() int64 { return timeToUnixMillis(time.Now()) }