diff --git a/dapr/proto/runtime/v1/workflow.proto b/dapr/proto/runtime/v1/workflow.proto index ac5fee9d..a5425e00 100644 --- a/dapr/proto/runtime/v1/workflow.proto +++ b/dapr/proto/runtime/v1/workflow.proto @@ -84,6 +84,7 @@ message TerminateWorkflowRequest { // Defaults to the local app. Must be in the same namespace; the target // app's WorkflowAccessPolicy governs whether the call is permitted. optional string app_id = 3 [json_name = "appID"]; + optional string terminate_reason = 4 [json_name = "terminateReason"]; } // PauseWorkflowRequest is the request for PauseWorkflowBeta1. diff --git a/src/implementation/Client/GRPCClient/workflow.ts b/src/implementation/Client/GRPCClient/workflow.ts index b58c1e48..f85b827b 100644 --- a/src/implementation/Client/GRPCClient/workflow.ts +++ b/src/implementation/Client/GRPCClient/workflow.ts @@ -35,7 +35,7 @@ export default class GRPCClientWorkflow implements IClientWorkflow { throw new GRPCNotSupportedError(); } - terminate(_instanceId: string): Promise { + terminate(_instanceId: string, _reason?: string): Promise { throw new GRPCNotSupportedError(); } diff --git a/src/implementation/Client/HTTPClient/workflow.ts b/src/implementation/Client/HTTPClient/workflow.ts index ea1033cf..a14b5268 100644 --- a/src/implementation/Client/HTTPClient/workflow.ts +++ b/src/implementation/Client/HTTPClient/workflow.ts @@ -135,8 +135,9 @@ export default class HTTPClientWorkflow implements IClientWorkflow { } } - async terminate(instanceId: string): Promise { - await this._invokeMethod(instanceId, "terminate"); + async terminate(instanceId: string, reason?: string): Promise { + const method = reason === undefined ? "terminate" : `terminate?reason=${encodeURIComponent(reason)}`; + await this._invokeMethod(instanceId, method); } async pause(instanceId: string): Promise { diff --git a/src/interfaces/Client/IClientWorkflow.ts b/src/interfaces/Client/IClientWorkflow.ts index 30b1d28d..8966bf8a 100644 --- a/src/interfaces/Client/IClientWorkflow.ts +++ b/src/interfaces/Client/IClientWorkflow.ts @@ -69,7 +69,7 @@ export default interface IClientWorkflow { * * @see https://docs.dapr.io/reference/api/workflow_api/ */ - terminate(instanceId: string): Promise; + terminate(instanceId: string, reason?: string): Promise; /** * Pauses a running workflow instance. diff --git a/src/proto/dapr/proto/runtime/v1/workflow.proto b/src/proto/dapr/proto/runtime/v1/workflow.proto index ac5fee9d..a5425e00 100644 --- a/src/proto/dapr/proto/runtime/v1/workflow.proto +++ b/src/proto/dapr/proto/runtime/v1/workflow.proto @@ -84,6 +84,7 @@ message TerminateWorkflowRequest { // Defaults to the local app. Must be in the same namespace; the target // app's WorkflowAccessPolicy governs whether the call is permitted. optional string app_id = 3 [json_name = "appID"]; + optional string terminate_reason = 4 [json_name = "terminateReason"]; } // PauseWorkflowRequest is the request for PauseWorkflowBeta1. diff --git a/src/proto/dapr/proto/runtime/v1/workflow_pb.d.ts b/src/proto/dapr/proto/runtime/v1/workflow_pb.d.ts index 0e6b085b..8ebb3a17 100644 --- a/src/proto/dapr/proto/runtime/v1/workflow_pb.d.ts +++ b/src/proto/dapr/proto/runtime/v1/workflow_pb.d.ts @@ -219,6 +219,13 @@ export declare type TerminateWorkflowRequest = Message<"dapr.proto.runtime.v1.Te * @generated from field: optional string app_id = 3 [json_name = "appID"]; */ appId?: string | undefined; + + /** + * Optional reason describing why the workflow is being terminated. + * + * @generated from field: optional string terminate_reason = 4 [json_name = "terminateReason"]; + */ + terminateReason?: string | undefined; }; /** diff --git a/src/proto/dapr/proto/runtime/v1/workflow_pb.js b/src/proto/dapr/proto/runtime/v1/workflow_pb.js index 3decb053..ddc53f5a 100644 --- a/src/proto/dapr/proto/runtime/v1/workflow_pb.js +++ b/src/proto/dapr/proto/runtime/v1/workflow_pb.js @@ -24,7 +24,7 @@ const { file_google_protobuf_timestamp } = require("@bufbuild/protobuf/wkt"); * Describes the file dapr/proto/runtime/v1/workflow.proto. */ const file_dapr_proto_runtime_v1_workflow = /*@__PURE__*/ - fileDesc("CiRkYXByL3Byb3RvL3J1bnRpbWUvdjEvd29ya2Zsb3cucHJvdG8SFWRhcHIucHJvdG8ucnVudGltZS52MSJ4ChJHZXRXb3JrZmxvd1JlcXVlc3QSHwoLaW5zdG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xvd19jb21wb25lbnQYAiABKAkSGgoGYXBwX2lkGAMgASgJSABSBWFwcElEiAEBQgkKB19hcHBfaWRC1AEKGWNvbS5kYXByLnByb3RvLnJ1bnRpbWUudjFCDVdvcmtmbG93UHJvdG9QAVoxZ2l0aHViLmNvbS9kYXByL2RhcHIvcGtnL3Byb3RvL3J1bnRpbWUvdjE7cnVudGltZaICA0RQUqoCFURhcHIuUHJvdG8uUnVudGltZS5WMcoCFURhcHJcUHJvdG9cUnVudGltZVxWMeICIURhcHJcUHJvdG9cUnVudGltZVxWMVxHUEJNZXRhZGF0YeoCGERhcHI6OlByb3RvOjpSdW50aW1lOjpWMWIGcHJvdG8z", [file_google_protobuf_timestamp]); /** * Describes the message dapr.proto.runtime.v1.GetWorkflowRequest. diff --git a/src/workflow/client/DaprWorkflowClient.ts b/src/workflow/client/DaprWorkflowClient.ts index 4366740a..8c92082b 100644 --- a/src/workflow/client/DaprWorkflowClient.ts +++ b/src/workflow/client/DaprWorkflowClient.ts @@ -63,6 +63,24 @@ export default class DaprWorkflowClient { await this._innerClient.terminateOrchestration(workflowInstanceId, output, signal); } + /** + * Terminates a workflow with a durable, retrievable reason. + * + * The reason is stored as the existing termination input and exposed through + * {@link WorkflowState.customStatus}. It cannot be combined with an output. + */ + public async terminateWorkflowWithReason( + workflowInstanceId: string, + reason: string, + output?: unknown, + signal?: AbortSignal, + ): Promise { + if (output !== undefined) { + throw new TypeError("output and reason cannot be supplied together"); + } + await this._innerClient.terminateOrchestration(workflowInstanceId, undefined, signal, reason); + } + /** * Gets a snapshot of the current workflow state, or undefined if the instance does not exist. * Compare runtimeStatus with WorkflowRuntimeStatus members, not hard-coded numbers or strings. diff --git a/src/workflow/engine/transport/TaskHubClient.ts b/src/workflow/engine/transport/TaskHubClient.ts index e7d3048c..33afb5ea 100644 --- a/src/workflow/engine/transport/TaskHubClient.ts +++ b/src/workflow/engine/transport/TaskHubClient.ts @@ -302,13 +302,24 @@ export class TaskHubClient { await this.client.raiseEvent(req, this.callOptions(signal)); } - async terminateOrchestration(instanceId: string, output: unknown = null, signal?: AbortSignal): Promise { + async terminateOrchestration( + instanceId: string, + output: unknown = null, + signal?: AbortSignal, + reason?: string, + ): Promise { const req = create(TerminateRequestSchema, { instanceId, recursive: true, }); - if (output !== undefined) { + if (output !== undefined && reason !== undefined) { + throw new TypeError("output and reason cannot be supplied together"); + } + + if (reason !== undefined) { + req.output = reason; + } else if (output !== undefined) { req.output = await this.encodePayload(output, { kind: PayloadKind.WorkflowOutput, instanceId }, signal); } diff --git a/test/unit/protocols/http/workflow.test.ts b/test/unit/protocols/http/workflow.test.ts index c7083cfd..8a9949e0 100644 --- a/test/unit/protocols/http/workflow.test.ts +++ b/test/unit/protocols/http/workflow.test.ts @@ -42,4 +42,55 @@ describe("workflow", () => { it("should throw PropertyRequiredError when eventName variable is not provided in raiseEvent method", async () => { await expect(workflow.raiseEvent(randomUUID(), "")).rejects.toThrow(PropertyRequiredError); }); + describe("terminate", () => { + it("sends a termination reason as a query parameter", async () => { + const reason = "operator requested & confirmed"; + const execute = jest.spyOn(client, "executeWithApiVersion").mockResolvedValue({}); + + try { + await workflow.terminate("instance-1", reason); + + expect(execute).toHaveBeenCalledWith( + "v1.0-beta1", + `/workflows/dapr/instance-1/terminate?reason=${encodeURIComponent(reason)}`, + { method: "POST" }, + ); + } finally { + execute.mockRestore(); + } + }); + + it("preserves an explicitly empty termination reason", async () => { + const execute = jest.spyOn(client, "executeWithApiVersion").mockResolvedValue({}); + + try { + await workflow.terminate("instance-1", ""); + + expect(execute).toHaveBeenCalledWith( + "v1.0-beta1", + "/workflows/dapr/instance-1/terminate?reason=", + { method: "POST" }, + ); + } finally { + execute.mockRestore(); + } + }); + + it("omits the termination reason when it is undefined", async () => { + const execute = jest.spyOn(client, "executeWithApiVersion").mockResolvedValue({}); + + try { + await workflow.terminate("instance-1"); + + expect(execute).toHaveBeenCalledWith( + "v1.0-beta1", + "/workflows/dapr/instance-1/terminate", + { method: "POST" }, + ); + } finally { + execute.mockRestore(); + } + }); + }); + }); diff --git a/test/unit/workflow/workflowClient.test.ts b/test/unit/workflow/workflowClient.test.ts index 0e6cdf0a..21296e0f 100644 --- a/test/unit/workflow/workflowClient.test.ts +++ b/test/unit/workflow/workflowClient.test.ts @@ -353,6 +353,27 @@ describe("DaprWorkflowClient", () => { }); }); + describe("terminateWorkflowWithReason", () => { + it("maps the reason to the existing termination input", async () => { + mockTerminateOrchestration.mockResolvedValue(undefined); + + await client.terminateWorkflowWithReason("instance-1", "operator requested"); + + expect(mockTerminateOrchestration).toHaveBeenCalledWith( + "instance-1", + null, + undefined, + "operator requested", + ); + }); + + it("rejects separate output and reason values", async () => { + await expect( + client.terminateWorkflowWithReason("instance-1", "operator requested", { result: "cancelled" }), + ).rejects.toThrow("output and reason cannot be supplied together"); + }); + }); + describe("raiseEvent", () => { it("should raise an event with payload", async () => { mockRaiseOrchestrationEvent.mockResolvedValue(undefined);