|
| 1 | +/* |
| 2 | +Copyright 2025 The Dapr Authors |
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +Unless required by applicable law or agreed to in writing, software |
| 8 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +See the License for the specific language governing permissions and |
| 11 | +limitations under the License. |
| 12 | +*/ |
| 13 | + |
| 14 | +package activity |
| 15 | + |
| 16 | +import ( |
| 17 | + "context" |
| 18 | + "errors" |
| 19 | + "fmt" |
| 20 | + "strings" |
| 21 | + "time" |
| 22 | + |
| 23 | + "google.golang.org/protobuf/proto" |
| 24 | + |
| 25 | + diag "github.com/dapr/dapr/pkg/diagnostics" |
| 26 | + invokev1 "github.com/dapr/dapr/pkg/messaging/v1" |
| 27 | + internalsv1pb "github.com/dapr/dapr/pkg/proto/internals/v1" |
| 28 | + wferrors "github.com/dapr/dapr/pkg/runtime/wfengine/errors" |
| 29 | + "github.com/dapr/dapr/pkg/runtime/wfengine/todo" |
| 30 | + "github.com/dapr/durabletask-go/api" |
| 31 | + "github.com/dapr/durabletask-go/backend" |
| 32 | +) |
| 33 | + |
| 34 | +func (a *activity) executeActivity(ctx context.Context, name string, taskEvent *backend.HistoryEvent) (todo.RunCompleted, error) { |
| 35 | + activityName := "" |
| 36 | + if ts := taskEvent.GetTaskScheduled(); ts != nil { |
| 37 | + activityName = ts.GetName() |
| 38 | + } else { |
| 39 | + return todo.RunCompletedTrue, fmt.Errorf("invalid activity task event: '%s'", taskEvent.String()) |
| 40 | + } |
| 41 | + |
| 42 | + endIndex := strings.Index(a.actorID, "::") |
| 43 | + if endIndex < 0 { |
| 44 | + return todo.RunCompletedTrue, fmt.Errorf("invalid activity actor ID: '%s'", a.actorID) |
| 45 | + } |
| 46 | + workflowID := a.actorID[0:endIndex] |
| 47 | + |
| 48 | + wi := &backend.ActivityWorkItem{ |
| 49 | + SequenceNumber: int64(taskEvent.GetEventId()), |
| 50 | + InstanceID: api.InstanceID(workflowID), |
| 51 | + NewEvent: taskEvent, |
| 52 | + Properties: make(map[string]any), |
| 53 | + } |
| 54 | + |
| 55 | + // Executing activity code is a one-way operation. We must wait for the app code to report its completion, which |
| 56 | + // will trigger this callback channel. |
| 57 | + // TODO: Need to come up with a design for timeouts. Some activities may need to run for hours but we also need |
| 58 | + // to handle the case where the app crashes and never responds to the workflow. It may be necessary to |
| 59 | + // introduce some kind of heartbeat protocol to help identify such cases. |
| 60 | + callback := make(chan bool, 1) |
| 61 | + wi.Properties[todo.CallbackChannelProperty] = callback |
| 62 | + log.Debugf("Activity actor '%s': scheduling activity '%s' for workflow with instanceId '%s'", a.actorID, name, wi.InstanceID) |
| 63 | + elapsed := float64(0) |
| 64 | + start := time.Now() |
| 65 | + err := a.scheduler(ctx, wi) |
| 66 | + elapsed = diag.ElapsedSince(start) |
| 67 | + |
| 68 | + if errors.Is(err, context.DeadlineExceeded) { |
| 69 | + diag.DefaultWorkflowMonitoring.ActivityOperationEvent(ctx, activityName, diag.StatusRecoverable, elapsed) |
| 70 | + return todo.RunCompletedFalse, wferrors.NewRecoverable(fmt.Errorf("timed-out trying to schedule an activity execution - this can happen if too many activities are running in parallel or if the workflow engine isn't running: %w", err)) |
| 71 | + } else if err != nil { |
| 72 | + diag.DefaultWorkflowMonitoring.ActivityOperationEvent(ctx, activityName, diag.StatusRecoverable, elapsed) |
| 73 | + return todo.RunCompletedFalse, wferrors.NewRecoverable(fmt.Errorf("failed to schedule an activity execution: %w", err)) |
| 74 | + } |
| 75 | + diag.DefaultWorkflowMonitoring.ActivityOperationEvent(ctx, activityName, diag.StatusSuccess, elapsed) |
| 76 | + |
| 77 | + // Activity execution started |
| 78 | + start = time.Now() |
| 79 | + executionStatus := "" |
| 80 | + elapsed = float64(0) |
| 81 | + // Record metrics on exit |
| 82 | + defer func() { |
| 83 | + if executionStatus != "" { |
| 84 | + diag.DefaultWorkflowMonitoring.ActivityExecutionEvent(ctx, activityName, executionStatus, elapsed) |
| 85 | + } |
| 86 | + }() |
| 87 | + |
| 88 | + select { |
| 89 | + case <-ctx.Done(): |
| 90 | + // Activity execution failed with recoverable error |
| 91 | + elapsed = diag.ElapsedSince(start) |
| 92 | + executionStatus = diag.StatusRecoverable |
| 93 | + return todo.RunCompletedFalse, ctx.Err() // will be retried |
| 94 | + case completed := <-callback: |
| 95 | + elapsed = diag.ElapsedSince(start) |
| 96 | + if !completed { |
| 97 | + // Activity execution failed with recoverable error |
| 98 | + executionStatus = diag.StatusRecoverable |
| 99 | + return todo.RunCompletedFalse, wferrors.NewRecoverable(todo.ErrExecutionAborted) // AbandonActivityWorkItem was called |
| 100 | + } |
| 101 | + } |
| 102 | + log.Debugf("Activity actor '%s': activity completed for workflow with instanceId '%s' activityName '%s'", a.actorID, wi.InstanceID, name) |
| 103 | + |
| 104 | + // publish the result back to the workflow actor as a new event to be processed |
| 105 | + resultData, err := proto.Marshal(wi.Result) |
| 106 | + if err != nil { |
| 107 | + // Returning non-recoverable error |
| 108 | + executionStatus = diag.StatusFailed |
| 109 | + return todo.RunCompletedTrue, err |
| 110 | + } |
| 111 | + |
| 112 | + req := internalsv1pb. |
| 113 | + NewInternalInvokeRequest(todo.AddWorkflowEventMethod). |
| 114 | + WithActor(a.workflowActorType, workflowID). |
| 115 | + WithData(resultData). |
| 116 | + WithContentType(invokev1.ProtobufContentType) |
| 117 | + |
| 118 | + _, err = a.router.Call(ctx, req) |
| 119 | + switch { |
| 120 | + case err != nil: |
| 121 | + // Returning recoverable error, record metrics |
| 122 | + executionStatus = diag.StatusRecoverable |
| 123 | + return todo.RunCompletedFalse, wferrors.NewRecoverable(fmt.Errorf("failed to invoke '%s' method on workflow actor: %w", todo.AddWorkflowEventMethod, err)) |
| 124 | + case wi.Result.GetTaskCompleted() != nil: |
| 125 | + // Activity execution completed successfully |
| 126 | + executionStatus = diag.StatusSuccess |
| 127 | + case wi.Result.GetTaskFailed() != nil: |
| 128 | + // Activity execution failed |
| 129 | + executionStatus = diag.StatusFailed |
| 130 | + } |
| 131 | + return todo.RunCompletedTrue, nil |
| 132 | +} |
0 commit comments