@@ -333,6 +333,60 @@ static P.GetInstanceResponse CreateGetInstanceResponse(OrchestrationState state,
333333 return new P . ResumeResponse ( ) ;
334334 }
335335
336+ public override async Task < P . RestartInstanceResponse > RestartInstance ( P . RestartInstanceRequest request , ServerCallContext context )
337+ {
338+ try
339+ {
340+ // Get the original orchestration state
341+ IList < OrchestrationState > states = await this . client . GetOrchestrationStateAsync ( request . InstanceId , false ) ;
342+ if ( states == null || states . Count == 0 )
343+ {
344+ throw new RpcException ( new Status ( StatusCode . NotFound , $ "An orchestration with the instanceId { request . InstanceId } was not found.") ) ;
345+ }
346+
347+ OrchestrationState state = states [ 0 ] ;
348+ string newInstanceId = request . RestartWithNewInstanceId ? Guid . NewGuid ( ) . ToString ( "N" ) : request . InstanceId ;
349+
350+ // Create a new orchestration instance
351+ OrchestrationInstance newInstance = new ( )
352+ {
353+ InstanceId = newInstanceId ,
354+ ExecutionId = Guid . NewGuid ( ) . ToString ( "N" ) ,
355+ } ;
356+
357+ // Create an ExecutionStartedEvent with the original input
358+ ExecutionStartedEvent startedEvent = new ( - 1 , state . Name )
359+ {
360+ Input = state . Input , // Use the original serialized input
361+ Version = state . Version ?? string . Empty ,
362+ OrchestrationInstance = newInstance ,
363+ } ;
364+
365+ TaskMessage taskMessage = new ( )
366+ {
367+ OrchestrationInstance = newInstance ,
368+ Event = startedEvent ,
369+ } ;
370+
371+ await this . client . CreateTaskOrchestrationAsync ( taskMessage ) ;
372+
373+ return new P . RestartInstanceResponse
374+ {
375+ InstanceId = newInstanceId ,
376+ } ;
377+ }
378+ catch ( RpcException )
379+ {
380+ // Re-throw RpcException as-is
381+ throw ;
382+ }
383+ catch ( Exception ex )
384+ {
385+ this . log . LogError ( ex , "Error restarting orchestration instance {InstanceId}" , request . InstanceId ) ;
386+ throw new RpcException ( new Status ( StatusCode . Internal , ex . Message ) ) ;
387+ }
388+ }
389+
336390 static P . TaskFailureDetails ? GetFailureDetails ( FailureDetails ? failureDetails )
337391 {
338392 if ( failureDetails == null )
0 commit comments