A sync every 15 minutes
Pulling orders every 15 minutes is easy. The hard part is making sure no order goes missing when a token expires, an API pushes back, or the server is down for hours.
- Published
- Reading time
- 2 min read
- Project
- Return Management
- Topics
- Python, Cron, Watermark, Retry
One schedule, one process
The sync runs from cron every 15 minutes, wrapped in flock. If the previous pull has not finished, the next tick is skipped rather than stacked, so two processes never write the same data. A full round across 8 marketplaces usually takes a few minutes.
Orders are pulled through each marketplace's official API, shop by shop, with each one's own sign-in: an HMAC signature, a token, or OAuth. No scraping.
A watermark per shop, not a fixed window
The easy way is to always pull the last few hours of orders. The catch is that it only survives outages shorter than the window. A shop whose token expires for six hours under a three-hour window loses those orders for good, and nothing shows it.
So every shop keeps its own resume point: the end of its last successful window. The next pull starts there, moves forward at most 60 minutes per tick to keep each round's length under control, and a new shop starts three hours back. If a shop fails, its watermark does not move, and the same window is tried again 15 minutes later.
When a shop's watermark falls more than 60 minutes behind, the log raises a warning, so a stuck shop shows up before its data has gaps.
Overlap that costs nothing
Orders sometimes show up late in the API. So every pull starts 30 minutes before the watermark, and the edge of each window is pulled twice. Orders are stored with an upsert through MERGE on the marketplace and order number, so pulling again never creates duplicates. The overlap is free.
New orders are not flagged right away either. Only once an order is more than 30 minutes old and still missing from the internal order system is it marked, because imports there have a lag of their own.
Failing in one place, not everywhere
Each shop has its own error handling: on failure, its changes are rolled back and the round is recorded as failed. One marketplace going down does not stop the other seven.
API calls are retried up to six times on rate limits or server errors, following Retry-After when it is given, or a growing delay when it is not. On a rate limit, the pull rate for that marketplace is also halved.
A manual re-pull for a chosen time range deliberately leaves the watermark alone, so the regular schedule and manual pulls never get in each other's way.
A history you can trace
Every decision and claim is recorded: what changed, who changed it, when, and why. When a claim is questioned, the answer is in the history, not in someone's memory.