Skip to content

Commit fc50a49

Browse files
authored
Restart Instance (#80)
1 parent d24ebc5 commit fc50a49

File tree

2 files changed

+451
-0
lines changed

2 files changed

+451
-0
lines changed

packages/durabletask-js/src/client/client.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,60 @@ export class TaskHubGrpcClient {
315315
);
316316
}
317317

318+
/**
319+
* Restarts an existing orchestration instance with its original input.
320+
*
321+
* This method allows you to restart a completed, failed, or terminated orchestration
322+
* instance. The restarted orchestration will use the same input that was provided
323+
* when the orchestration was originally started.
324+
*
325+
* @param instanceId - The unique ID of the orchestration instance to restart.
326+
* @param restartWithNewInstanceId - If true, the restarted orchestration will be assigned
327+
* a new instance ID. If false (default), the same instance ID will be reused.
328+
* When reusing the same instance ID, the orchestration must be in a terminal state
329+
* (Completed, Failed, or Terminated).
330+
* @returns A Promise that resolves to the instance ID of the restarted orchestration.
331+
* This will be the same as the input instanceId if restartWithNewInstanceId is false,
332+
* or a new ID if restartWithNewInstanceId is true.
333+
* @throws Error if the orchestration instance is not found.
334+
* @throws Error if the orchestration cannot be restarted (e.g., it's still running
335+
* and restartWithNewInstanceId is false).
336+
*/
337+
async restartOrchestration(instanceId: string, restartWithNewInstanceId: boolean = false): Promise<string> {
338+
if (!instanceId) {
339+
throw new Error("instanceId cannot be null or empty");
340+
}
341+
342+
const req = new pb.RestartInstanceRequest();
343+
req.setInstanceid(instanceId);
344+
req.setRestartwithnewinstanceid(restartWithNewInstanceId);
345+
346+
console.log(`Restarting '${instanceId}' with restartWithNewInstanceId=${restartWithNewInstanceId}`);
347+
348+
try {
349+
const res = await callWithMetadata<pb.RestartInstanceRequest, pb.RestartInstanceResponse>(
350+
this._stub.restartInstance.bind(this._stub),
351+
req,
352+
this._metadataGenerator,
353+
);
354+
return res.getInstanceid();
355+
} catch (e) {
356+
if (e instanceof Error && "code" in e) {
357+
const grpcError = e as grpc.ServiceError;
358+
if (grpcError.code === grpc.status.NOT_FOUND) {
359+
throw new Error(`An orchestration with the instanceId '${instanceId}' was not found.`);
360+
}
361+
if (grpcError.code === grpc.status.FAILED_PRECONDITION) {
362+
throw new Error(`An orchestration with the instanceId '${instanceId}' cannot be restarted.`);
363+
}
364+
if (grpcError.code === grpc.status.CANCELLED) {
365+
throw new Error(`The restartOrchestration operation was canceled.`);
366+
}
367+
}
368+
throw e;
369+
}
370+
}
371+
318372
/**
319373
* Purges orchestration instance metadata from the durable store.
320374
*

0 commit comments

Comments
 (0)