Skip to content

Commit 355a611

Browse files
authored
add purge instance by ID support (#15)
1 parent b9a8fc3 commit 355a611

File tree

4 files changed

+67
-6
lines changed

4 files changed

+67
-6
lines changed

src/client.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ import * as pb from "./proto/orchestrator_service_pb";
44
import * as stubs from "./proto/orchestrator_service_grpc_pb";
55
import { TOrchestrator } from "./types/orchestrator.type";
66
import { TInput } from "./types/input.type";
7-
import { TOutput } from "./types/output.type";
87
import { getName } from "./task";
98
import { randomUUID } from "crypto";
109
import { promisify } from "util";
11-
import { newOrchestrationState } from "./orchestration";
10+
import { newOrchestrationState, newPurgeResult } from "./orchestration";
1211
import { OrchestrationState } from "./orchestration/orchestration-state";
1312
import { GrpcClient } from "./client-grpc";
1413
import { OrchestrationStatus } from "./orchestration/enum/orchestration-status.enum";
1514
import { TimeoutError } from "./exception/timeout-error";
15+
import { PurgeResult } from "./orchestration/orchestration-purge-result";
1616

1717
export class TaskHubGrpcClient {
1818
private _stub: stubs.TaskHubSidecarServiceClient;
@@ -191,4 +191,14 @@ export class TaskHubGrpcClient {
191191
const prom = promisify(this._stub.resumeInstance.bind(this._stub));
192192
await prom(req);
193193
}
194+
195+
async purgeInstanceById(instanceId: string,): Promise<PurgeResult | undefined> {
196+
const req = new pb.PurgeInstancesRequest;
197+
req.setInstanceid(instanceId);
198+
console.log(`Purging Instance '${instanceId}'`);
199+
const prom = promisify(this._stub.purgeInstances.bind(this._stub));
200+
// Execute the request and wait for the first response or timeout
201+
const res = (await prom(req)) as pb.PurgeInstancesResponse;
202+
return newPurgeResult(res);
203+
}
194204
}

src/orchestration/index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as pb from "../proto/orchestrator_service_pb";
22
import { FailureDetails } from "../task/failure-details";
33
import { OrchestrationStatus, parseGrpcValue } from "./enum/orchestration-status.enum";
4+
import { PurgeResult } from "./orchestration-purge-result";
45
import { OrchestrationState } from "./orchestration-state";
56

67
export function newOrchestrationState(
@@ -54,3 +55,15 @@ export function newOrchestrationState(
5455
failureDetails,
5556
);
5657
}
58+
59+
export function newPurgeResult(
60+
res: pb.PurgeInstancesResponse
61+
): PurgeResult | undefined {
62+
if (!res || !res.getDeletedinstancecount()) {
63+
return;
64+
}
65+
66+
return new PurgeResult(
67+
res.getDeletedinstancecount(),
68+
);
69+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export class PurgeResult {
2+
deletedInstanceCount: number;
3+
4+
constructor(
5+
deletedInstanceCount: number,
6+
) {
7+
this.deletedInstanceCount = deletedInstanceCount;
8+
}
9+
}

test/e2e/orchestration.spec.ts

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -343,22 +343,51 @@ describe("Durable Functions", () => {
343343
}, 31000);
344344

345345
it("should be able to run an single orchestration without activity", async () => {
346-
const sequence: TOrchestrator = async function* (ctx: OrchestrationContext, startVal: number): any {
346+
const orchestrator: TOrchestrator = async function* (ctx: OrchestrationContext, startVal: number): any {
347347
return startVal + 1;
348348
};
349349

350-
taskHubWorker.addOrchestrator(sequence);
350+
taskHubWorker.addOrchestrator(orchestrator);
351351
await taskHubWorker.start();
352352

353-
const id = await taskHubClient.scheduleNewOrchestration(sequence, 15);
353+
const id = await taskHubClient.scheduleNewOrchestration(orchestrator, 15);
354354
const state = await taskHubClient.waitForOrchestrationCompletion(id, undefined, 30);
355355

356356
expect(state);
357-
expect(state?.name).toEqual(getName(sequence));
357+
expect(state?.name).toEqual(getName(orchestrator));
358358
expect(state?.instanceId).toEqual(id);
359359
expect(state?.failureDetails).toBeUndefined();
360360
expect(state?.runtimeStatus).toEqual(OrchestrationStatus.ORCHESTRATION_STATUS_COMPLETED);
361361
expect(state?.serializedInput).toEqual(JSON.stringify(15));
362362
expect(state?.serializedOutput).toEqual(JSON.stringify(16));
363363
}, 31000);
364+
365+
it("should be able purge orchestration", async () => {
366+
const plusOne = async (_: ActivityContext, input: number) => {
367+
return input + 1;
368+
};
369+
370+
const orchestrator: TOrchestrator = async function* (ctx: OrchestrationContext, startVal: number): any {
371+
return yield ctx.callActivity(plusOne, startVal);
372+
};
373+
374+
taskHubWorker.addOrchestrator(orchestrator);
375+
taskHubWorker.addActivity(plusOne);
376+
await taskHubWorker.start();
377+
378+
const id = await taskHubClient.scheduleNewOrchestration(orchestrator, 1);
379+
const state = await taskHubClient.waitForOrchestrationCompletion(id, undefined, 30);
380+
381+
expect(state);
382+
expect(state?.name).toEqual(getName(orchestrator));
383+
expect(state?.instanceId).toEqual(id);
384+
expect(state?.failureDetails).toBeUndefined();
385+
expect(state?.runtimeStatus).toEqual(OrchestrationStatus.ORCHESTRATION_STATUS_COMPLETED);
386+
expect(state?.serializedInput).toEqual(JSON.stringify(1));
387+
expect(state?.serializedOutput).toEqual(JSON.stringify(2));
388+
389+
const purgeResult = await taskHubClient.purgeInstanceById(id);
390+
expect(purgeResult);
391+
expect(purgeResult?.deletedInstanceCount).toEqual(1);
392+
}, 31000);
364393
});

0 commit comments

Comments
 (0)