Skip to content

Commit 39bb823

Browse files
committed
Restart Instance
1 parent b1e1d53 commit 39bb823

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
@@ -312,6 +312,60 @@ export class TaskHubGrpcClient {
312312
);
313313
}
314314

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

0 commit comments

Comments
 (0)