@@ -67,6 +67,7 @@ export function createWorkerPlacementMoveService(options: {
6767 identity : MoveSessionIdentity ,
6868 target : WorkerPlacementMoveTarget ,
6969 ) => Promise < WorkerPlacementMoveDestination | undefined > ;
70+ onRecoveredTransition ?: ( placement : WorkerDispatchPlacement ) => void ;
7071} ) {
7172 const recordError = ( intent : WorkerPlacementMoveIntent , error : unknown ) : void => {
7273 options . placements . recordPlacementMoveError ( {
@@ -191,12 +192,15 @@ export function createWorkerPlacementMoveService(options: {
191192 }
192193 } ;
193194
194- const recover = async ( intent : WorkerPlacementMoveIntent ) : Promise < void > => {
195+ const recover = async (
196+ intent : WorkerPlacementMoveIntent ,
197+ ) : Promise < WorkerDispatchPlacement | undefined > => {
195198 try {
196199 let placement = options . placements . get ( intent . sessionId ) ;
197200 if ( ! placement ) {
198201 throw new Error ( `Session ${ intent . sessionId } placement move lost its session placement` ) ;
199202 }
203+ const initialPlacement = placement ;
200204 const identity = {
201205 sessionId : placement . sessionId ,
202206 sessionKey : placement . sessionKey ,
@@ -213,7 +217,7 @@ export function createWorkerPlacementMoveService(options: {
213217 operationId : intent . operationId ,
214218 sessionId : intent . sessionId ,
215219 } ) ;
216- return ;
220+ return placement ;
217221 }
218222 if (
219223 placement . state !== "active" &&
@@ -225,8 +229,7 @@ export function createWorkerPlacementMoveService(options: {
225229 `Session ${ identity . sessionKey } abandonment recovery is waiting in ${ placement . state } ` ,
226230 ) ;
227231 }
228- await options . abandonSource ( identity , intent ) ;
229- return ;
232+ return await options . abandonSource ( identity , intent ) ;
230233 }
231234 if ( placement . state === "failed" ) {
232235 if (
@@ -243,7 +246,7 @@ export function createWorkerPlacementMoveService(options: {
243246 operationId : intent . operationId ,
244247 sessionId : intent . sessionId ,
245248 } ) ;
246- return ;
249+ return placement ;
247250 } else if ( placement . state === "draining" ) {
248251 const local = await options . reclaimSource ( identity , intent ) ;
249252 if ( local . state !== "local" ) {
@@ -258,7 +261,7 @@ export function createWorkerPlacementMoveService(options: {
258261 environment . state !== "failed" &&
259262 environment . state !== "orphaned"
260263 ) {
261- return ;
264+ return undefined ;
262265 }
263266 placement = options . placements . completePlacementMoveSourceToLocal ( {
264267 operationId : intent . operationId ,
@@ -272,29 +275,29 @@ export function createWorkerPlacementMoveService(options: {
272275 if ( stillSource ) {
273276 throw new Error ( `Session ${ identity . sessionKey } move recovery found an active source` ) ;
274277 }
275- options . placements . completePlacementMoveToWorker ( {
278+ return options . placements . completePlacementMoveToWorker ( {
276279 operationId : intent . operationId ,
277280 sessionId : intent . sessionId ,
278281 expectedGeneration : placement . generation ,
279282 environmentId : placement . environmentId ,
280283 ownerEpoch : placement . activeOwnerEpoch ,
281284 } ) ;
282- return ;
283285 } else if ( placement . state !== "local" ) {
284286 // Generic dispatch recovery owns requested through starting. A later
285287 // coordinated sweep either observes active or retries from failed/local.
286- return ;
288+ return undefined ;
287289 }
288290 if ( intent . target . kind === "gateway" ) {
289291 if ( options . placements . getPlacementMove ( intent . sessionId ) ) {
290292 options . placements . cancelPlacementMove ( {
291293 operationId : intent . operationId ,
292294 sessionId : intent . sessionId ,
293295 } ) ;
296+ return placement ;
294297 }
295- return ;
298+ return placement === initialPlacement ? undefined : placement ;
296299 }
297- options . placements . fail ( {
300+ const failed = options . placements . fail ( {
298301 sessionId : placement . sessionId ,
299302 expectedGeneration : placement . generation ,
300303 recoveryError : RESTART_AUTHORITY_EXPIRED ,
@@ -303,6 +306,7 @@ export function createWorkerPlacementMoveService(options: {
303306 operationId : intent . operationId ,
304307 sessionId : intent . sessionId ,
305308 } ) ;
309+ return failed ;
306310 } catch ( error ) {
307311 recordError ( intent , error ) ;
308312 throw error ;
@@ -320,7 +324,13 @@ export function createWorkerPlacementMoveService(options: {
320324 ) {
321325 protectedSessions . add ( intent . sessionId ) ;
322326 }
323- await recover ( intent ) . catch ( ( ) => undefined ) ;
327+ await recover ( intent )
328+ . then ( ( placement ) => {
329+ if ( placement ) {
330+ options . onRecoveredTransition ?.( placement ) ;
331+ }
332+ } )
333+ . catch ( ( ) => undefined ) ;
324334 }
325335 return protectedSessions ;
326336 } ;
0 commit comments