From 91544629fa29e44c748857a7cefa9f0b0dadb119 Mon Sep 17 00:00:00 2001 From: jjbrunton Date: Fri, 28 Aug 2026 15:11:12 +0100 Subject: [PATCH] fix(worker): keep polling process alive --- apps/api/src/workers/index.ts | 3 +-- infra/backend/verify-production.sh | 5 +++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/apps/api/src/workers/index.ts b/apps/api/src/workers/index.ts index d0d2ed0..89a4d04 100644 --- a/apps/api/src/workers/index.ts +++ b/apps/api/src/workers/index.ts @@ -10,8 +10,7 @@ const runner=new OperationsRunner(operations,new ExpoPushProvider(),new DiscordP {enabled:config.classifierEnabled,apiKey:config.openRouterApiKey,model:config.classifierModel,prompt:config.classifierPrompt,adminPrivateKeyJwk:config.adminPrivateKeyJwk,mediaRoot:config.mediaRoot}), new MediaJanitor(mediaRepository,mediaStorage)); let stopped=false;let timer:NodeJS.Timeout|undefined; -async function tick(){if(stopped)return;try{const result=await runner.runOnce(new Date(),config.batchSize);if(result.claimed||result.failed)console.log('Operations worker tick',result);}catch(cause){console.error('Operations worker tick failed',cause);}finally{if(!stopped){timer=setTimeout(()=>void tick(),config.pollIntervalMs);timer.unref();}}} +async function tick(){if(stopped)return;try{const result=await runner.runOnce(new Date(),config.batchSize);if(result.claimed||result.failed)console.log('Operations worker tick',result);}catch(cause){console.error('Operations worker tick failed',cause);}finally{if(!stopped){timer=setTimeout(()=>void tick(),config.pollIntervalMs);}}} async function shutdown(signal:string){if(stopped)return;stopped=true;if(timer)clearTimeout(timer);console.log(`Received ${signal}; stopping operations worker`);await Promise.all([operations.close(),mediaRepository.close()]);} process.on('SIGINT',()=>void shutdown('SIGINT').then(()=>process.exit(0))); process.on('SIGTERM',()=>void shutdown('SIGTERM').then(()=>process.exit(0))); console.log('Sauci operations worker started');void tick(); - diff --git a/infra/backend/verify-production.sh b/infra/backend/verify-production.sh index 207de66..0623c50 100755 --- a/infra/backend/verify-production.sh +++ b/infra/backend/verify-production.sh @@ -32,6 +32,11 @@ docker compose -p "$project_name" -f "$compose_file" up -d --build --wait postgr # turn that intentional contract into a false failure. docker compose -p "$project_name" -f "$compose_file" up -d worker test "$(docker compose -p "$project_name" -f "$compose_file" ps --status running -q worker | wc -l | tr -d ' ')" = "1" +worker_id="$(docker compose -p "$project_name" -f "$compose_file" ps -q worker)" +worker_restarts_before="$(docker inspect --format '{{.RestartCount}}' "$worker_id")" +sleep 3 +test "$(docker inspect --format '{{.State.Running}}' "$worker_id")" = "true" +test "$(docker inspect --format '{{.RestartCount}}' "$worker_id")" = "$worker_restarts_before" docker compose -p "$project_name" -f "$compose_file" exec -T api node -e \ "Promise.all(['/health/live','/health/ready','/v1/me'].map(async p=>[p,(await fetch('http://127.0.0.1:3003'+p)).status])).then(results=>{const expected=[200,200,401];if(results.some((result,index)=>result[1]!==expected[index])){console.error(results);process.exit(1)};console.log(results)})"