Skip to content

Task 19: Polling Loop Needs a State Machine to Avoid Infinite Loops on Stuck Jobs #315

Description

@grantfox-oss

The Fastify API returns a dynamic job status that can mutate through various phases: "queued", "running", "completed", or "failed". The polling loop must intelligently parse and react to these distinct states. If the client does not rigorously handle every possible state transition, it may mistakenly assume a failed job has succeeded, or it may enter an infinite, unbreakable loop if the server's backend worker dies and the job is permanently stuck in a "running" zombie state. A robust client must be deterministic and immune to infinite loops.

Issues/what to fix

Inside the core polling loop, a strict switch case State Machine must be implemented to evaluate the API JSON response. If the state evaluates to "queued" or "running", the machine transitions to the backoff logic to trigger the next polling cycle. If the state evaluates to "failed", the machine must instantly break the loop, extract the error_reason field from the API response, and throw a fatal exception. If the state evaluates to "completed", it must extract the nested simulation results, cleanly print them to the console, and gracefully terminate the Node process (process.exit(0)). Crucially, an overarching absolute timeout (e.g., 60 seconds) or a maximum iteration counter must be injected into the loop to serve as a circuit breaker, ensuring the process terminates even if the remote server completely hangs.

Files location

examples/api clients/mock replay submitter/index.js

Expected result

The script will function as a highly deterministic state machine, flawlessly reacting to the lifecycle mutations of the remote simulation job. It will successfully extract the final payload, cleanly exit upon completion, and absolutely guarantee that it will never hang indefinitely in the event of a catastrophic server side failure.

Contributor telegram group

https://t.me/+sII7WPhll2liMGNk

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions