A shop manager clicks Export for a month of completed jobs and closes the tab. Ten minutes later she opens the export page again. What should she find?
I’d give the export its own record. The API saves the shop, the date range, and the job’s queued status in one transaction. Once that transaction commits, it returns a receipt for job 42 and a link where the manager can check on it. For a first version, a worker can take jobs directly from that table.
The worker claims job 42 for a limited time and gives this attempt a unique token. The page now shows processing. As the worker writes the CSV, it renews its claim. Each attempt writes to its own private file.
Suppose the first worker stalls long enough for its claim to expire. A second worker can take over. If the first one wakes up and finishes, the two files stay separate. To publish a result, a worker must finish writing its file, then update the job only if it still holds the current, unexpired claim. The same update saves the file reference and marks the job ready. The download link serves only that file. The other attempt’s file can be cleaned up later.
If every attempt fails, the manager can still open the receipt and see what happened. In the walkthrough below, watch when the receipt appears and when the download becomes available. An HTTP 202 Accepted response fits the first moment. The work might still fail. HTTP semantics for 202 Accepted.
Follow one CSV export
-
Request the export
The browser sends the chosen date range to the API and asks for a CSV.
-
Return a receipt
The API saves job #42 in a durable queue. It returns the job number and a status URL so the browser can check back.
-
Build the file
A worker claims job #42, reads the matching records, and writes them into a CSV.
-
Make the download available
The worker saves the finished file and marks job #42 ready. The next time the browser checks the status URL, it receives a download link.