Skip to content

Commit 67de855

Browse files
authored
support set custom status (#1)
* support set custom status Signed-off-by: Fabian Martinez <[email protected]> * update license header Signed-off-by: Fabian Martinez <[email protected]> * fix test Signed-off-by: Fabian Martinez <[email protected]> * fix e2e test Signed-off-by: Fabian Martinez <[email protected]> --------- Signed-off-by: Fabian Martinez <[email protected]>
1 parent 33a8fb6 commit 67de855

File tree

8 files changed

+107
-63
lines changed

8 files changed

+107
-63
lines changed

examples/activity-sequence.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import { TaskHubGrpcWorker } from "../src/worker/task-hub-grpc-worker";
2828
const result3 = yield ctx.callActivity(hello, "London");
2929
cities.push(result3);
3030

31+
ctx.setCustomStatus("sequence done");
32+
3133
return cities;
3234
};
3335

src/task/context/orchestration-context.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,11 @@ export abstract class OrchestrationContext {
8686
* @param saveEvents {boolean} A flag indicating whether to add any unprocessed external events in the new orchestration history.
8787
*/
8888
abstract continueAsNew(newInput: any, saveEvents: boolean): void;
89+
90+
/**
91+
* Sets the custom status
92+
*
93+
* @param status {string} The new custom status
94+
*/
95+
abstract setCustomStatus(status: string): void;
8996
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2024 The Dapr Authors.
2+
// Licensed under the MIT License.
3+
4+
import * as pb from "../proto/orchestrator_service_pb";
5+
6+
export class OrchestrationExecuteResult {
7+
actions: pb.OrchestratorAction[];
8+
customStatus: string;
9+
10+
constructor(actions: pb.OrchestratorAction[], customStatus: string) {
11+
this.actions = actions;
12+
this.customStatus = customStatus;
13+
}
14+
}

src/worker/orchestration-executor.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { enumValueToKey } from "../utils/enum.util";
1616
import { getOrchestrationStatusStr, isEmpty } from "../utils/pb-helper.util";
1717
import { OrchestratorNotRegisteredError } from "./exception/orchestrator-not-registered-error";
1818
import { StopIterationError } from "./exception/stop-iteration-error";
19+
import { OrchestrationExecuteResult } from "./orchestration-execute-result";
1920
import { Registry } from "./registry";
2021
import { RuntimeOrchestrationContext } from "./runtime-orchestration-context";
2122

@@ -36,7 +37,7 @@ export class OrchestrationExecutor {
3637
instanceId: string,
3738
oldEvents: pb.HistoryEvent[],
3839
newEvents: pb.HistoryEvent[],
39-
): Promise<pb.OrchestratorAction[]> {
40+
): Promise<OrchestrationExecuteResult> {
4041
if (!newEvents?.length) {
4142
throw new OrchestrationStateError("The new history event list must have at least one event in it");
4243
}
@@ -79,7 +80,7 @@ export class OrchestrationExecutor {
7980
const actions = ctx.getActions();
8081
console.log(`${instanceId}: Returning ${actions.length} action(s)`);
8182

82-
return actions;
83+
return new OrchestrationExecuteResult(actions, ctx._customStatus);
8384
}
8485

8586
private async processEvent(ctx: RuntimeOrchestrationContext, event: pb.HistoryEvent): Promise<void> {

src/worker/runtime-orchestration-context.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export class RuntimeOrchestrationContext extends OrchestrationContext {
2727
_pendingEvents: Record<string, CompletableTask<any>[]>;
2828
_newInput?: any;
2929
_saveEvents: any;
30+
_customStatus: string;
3031

3132
constructor(instanceId: string) {
3233
super();
@@ -45,6 +46,7 @@ export class RuntimeOrchestrationContext extends OrchestrationContext {
4546
this._pendingEvents = {};
4647
this._newInput = undefined;
4748
this._saveEvents = false;
49+
this._customStatus = "";
4850
}
4951

5052
get instanceId(): string {
@@ -330,4 +332,8 @@ export class RuntimeOrchestrationContext extends OrchestrationContext {
330332

331333
this.setContinuedAsNew(newInput, saveEvents);
332334
}
335+
336+
setCustomStatus(status: string) {
337+
this._customStatus = status;
338+
}
333339
}

src/worker/task-hub-grpc-worker.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,14 @@ export class TaskHubGrpcWorker {
217217

218218
try {
219219
const executor = new OrchestrationExecutor(this._registry);
220-
const actions = await executor.execute(req.getInstanceid(), req.getPasteventsList(), req.getNeweventsList());
220+
const result = await executor.execute(req.getInstanceid(), req.getPasteventsList(), req.getNeweventsList());
221221

222222
res = new pb.OrchestratorResponse();
223223
res.setInstanceid(req.getInstanceid());
224-
res.setActionsList(actions);
224+
res.setActionsList(result.actions);
225+
const cs = new StringValue();
226+
cs.setValue(result.customStatus);
227+
res.setCustomstatus(cs);
225228
} catch (e: any) {
226229
console.error(e);
227230
console.log(`An error occurred while trying to execute instance '${req.getInstanceid()}': ${e.message}`);

test/e2e/orchestration.spec.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ describe("Durable Functions", () => {
6464
numbers.push(current);
6565
}
6666

67+
ctx.setCustomStatus("foobaz");
68+
6769
return numbers;
6870
};
6971

@@ -81,6 +83,7 @@ describe("Durable Functions", () => {
8183
expect(state?.runtimeStatus).toEqual(OrchestrationStatus.ORCHESTRATION_STATUS_COMPLETED);
8284
expect(state?.serializedInput).toEqual(JSON.stringify(1));
8385
expect(state?.serializedOutput).toEqual(JSON.stringify([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]));
86+
expect(state?.serializedCustomStatus).toEqual("foobaz");
8487
}, 31000);
8588

8689
it("should be able to run fan-out/fan-in", async () => {
@@ -179,13 +182,13 @@ describe("Durable Functions", () => {
179182
await taskHubWorker.start();
180183

181184
const id = await taskHubClient.scheduleNewOrchestration(orchestratorParent, SUB_ORCHESTRATION_COUNT);
182-
const state = await taskHubClient.waitForOrchestrationCompletion(id, undefined, 30);
185+
const state = await taskHubClient.waitForOrchestrationCompletion(id, undefined, 43);
183186

184187
expect(state);
185188
expect(state?.runtimeStatus).toEqual(OrchestrationStatus.ORCHESTRATION_STATUS_COMPLETED);
186189
expect(state?.failureDetails).toBeUndefined();
187190
expect(activityCounter).toEqual(SUB_ORCHESTRATION_COUNT * ACTIVITY_COUNT);
188-
}, 31000);
191+
}, 45000);
189192

190193
it("should allow waiting for multiple external events", async () => {
191194
const orchestrator: TOrchestrator = async function* (ctx: OrchestrationContext, _: any): any {

0 commit comments

Comments
 (0)