Project: retrying task queue
Request
The media export team needs a queue prototype whose results explain retries and cancellation.
Data
Each job has an id, a nonnegative failure count, and a nonnegative retry budget. Later releases add optional delay, value, priority, and cancellation fields.
Deliverable
An ordered, duplicate-safe queue runner with explicit attempt and elapsed-time reports. Time is simulated, and no external system receives a job.
A media service creates export jobs that sometimes fail before producing a file. Its queue needs clear retry rules and a result that support staff can read. A retry is an additional attempt, so a budget of zero still permits the first attempt. This distinction causes common off-by-one failures.
Implement a deterministic queue using job records rather than a live service. Each record tells you how many attempts will fail, its retry budget, and later its cancellation, duration, payload, and priority. Logical milliseconds let you test time accounting without waiting. The eight steps retain the previous implementation and add one behavior at a time. Keep failed, canceled, and successful results distinct, then prevent duplicate job identifiers from creating duplicate work.
type Job={id:string;failures:number;retries:number;canceled?:boolean;delay?:number;value?:string;priority?:number};
function solve(value:string):string[] { const jobs=JSON.parse(value) as Job[]; const out:string[]=[]; const seen=new Set<string>(); for(const job of jobs){ let done=false; let elapsed=0; for(let attempt=0;attempt<=job.retries;attempt++){ out.push(job.id+":ok"); done=true; break; } } return out; }
A complete implementation for the first scenario; trace its input before editing.