feat: support durable task cancellation without foreign keys
This commit is contained in:
@@ -32,6 +32,9 @@ func (w *Worker) invoke(ctx context.Context, handler WorkerHandler, execution Wo
|
||||
}()
|
||||
if ctx.Err() != nil {
|
||||
out = unknownHandlerResult(ref, "LEASE_LOST")
|
||||
if errors.Is(context.Cause(ctx), ErrTaskCancellationRequested) {
|
||||
out = cancelledHandlerResult(ref, nil)
|
||||
}
|
||||
completed = true
|
||||
return
|
||||
}
|
||||
@@ -46,7 +49,10 @@ func (w *Worker) invoke(ctx context.Context, handler WorkerHandler, execution Wo
|
||||
}
|
||||
} else {
|
||||
var known *WorkerFailure
|
||||
if errors.As(err, &known) && known != nil && workerName(known.Code, 64) {
|
||||
var cancelled *WorkerCancellationConfirmed
|
||||
if errors.As(err, &cancelled) && cancelled != nil && errors.Is(context.Cause(ctx), ErrTaskCancellationRequested) {
|
||||
out = cancelledHandlerResult(ref, cancelled.Details)
|
||||
} else if errors.As(err, &known) && known != nil && workerName(known.Code, 64) {
|
||||
details := append(json.RawMessage(nil), known.Details...)
|
||||
if len(details) == 0 {
|
||||
details = json.RawMessage(`{}`)
|
||||
@@ -64,6 +70,17 @@ func (w *Worker) invoke(ctx context.Context, handler WorkerHandler, execution Wo
|
||||
}
|
||||
completed = true
|
||||
}
|
||||
|
||||
func cancelledHandlerResult(ref WorkerAttempt, details json.RawMessage) WorkerFinish {
|
||||
details = append(json.RawMessage(nil), details...)
|
||||
if len(details) == 0 {
|
||||
details = json.RawMessage(`{}`)
|
||||
}
|
||||
if !validHandlerResult(details) {
|
||||
return unknownHandlerResult(ref, "INVALID_HANDLER_RESULT")
|
||||
}
|
||||
return WorkerFinish{Ref: ref, Outcome: WorkerCancelled, ErrorCode: "CANCEL_CONFIRMED", Result: details}
|
||||
}
|
||||
func unknownHandlerResult(ref WorkerAttempt, code string) WorkerFinish {
|
||||
return WorkerFinish{Ref: ref, Outcome: WorkerUnknown, ErrorCode: code, Result: json.RawMessage(`{}`)}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user