Automation · Salsify · PowerShell

The Bug Was the Retry Window

Sixty product export failures across nine brands looked like nine different problems. They were one: the importer stopped waiting before the exports were done.

Nine brands, sixty failures

Our product importer is a large PowerShell script that pulls product data from Salsify, our product information system, into Sitecore for a group of brand websites. For each brand, it asks Salsify for an export, waits for the export to finish, downloads it, and updates the product pages.

Exports kept failing. Different brands, different days, no obvious pattern. Sixty failures across nine brands. The tempting move is to treat each brand as its own mystery. That's nine investigations, and probably nine patches.

Line them up

Instead, I put all sixty failures side by side and looked for what they had in common. It wasn't the brand, the products, or the data. It was time.

Salsify exports are asynchronous. You request one, then keep checking back until it's ready. The importer would check a set number of times and then give up. Smaller brands finished well inside that window. Bigger brands, with lots of products and attributes, sometimes didn't. The export would have succeeded. The script just stopped waiting before it got there.

Request export
        ↓
Check: done yet?  (retry, retry, retry...)
        ↓
Window runs out → script gives up
        ↓
Big exports "fail" even though they would have finished

Why the window was too short

A retry window is really a guess about how long something takes. That guess was probably fine when the importer was first written. But catalogs grow. Brands add products, and products gain attributes. One brand's export covers hundreds of products and hundreds of attributes. The work got bigger, and the guess stayed the same.

Here's the shape of the difference. The first loop gives up after a fixed number of tries. The second waits until a real deadline that's sized for the slowest legitimate export:

# Illustrative: fixed attempts vs. a deadline sized for real exports

# Before: gives up after N checks, however long exports really take
for ($i = 0; $i -lt $maxAttempts; $i++) {
    if (Test-ExportReady $exportId) { break }
    Start-Sleep -Seconds $delay
}

# After: keep checking until a deadline that fits the biggest brands
$deadline = (Get-Date).AddMinutes($maxWaitMinutes)
while (-not (Test-ExportReady $exportId)) {
    if ((Get-Date) -gt $deadline) { throw "Export timed out after $maxWaitMinutes minutes" }
    Start-Sleep -Seconds $delay
}

The fix

It was a bug in the importer: the retry window was shorter than the time the larger exports actually needed. I fixed the retry handling so it waits long enough for real-world export times, and the cross-brand failures stopped being a mystery.

Along the way I added other safeguards too, like preflight checks and stopping on errors before bad data gets written.

Same lesson, different bug

A later issue followed the same pattern. One product with a bad page URL in Salsify, a full domain and query string where a simple path belonged, caused the importer to hold back that brand's entire update. One bad record, a whole brand stuck.

In the next version, the importer normalizes URLs automatically (stripping domains and query strings down to the path) and holds back only the one product with a problem, not the whole brand. I also added optional email alerts when a brand fails repeatedly, so someone hears about it before a stakeholder does.

What I took away

  • Many failures in many places can still be one bug. Line them up before you split them up.
  • A retry limit is a guess about timing. Revisit it as the data grows.
  • Prefer a deadline sized for real-world timing over a fixed number of attempts.
  • Let one bad record fail by itself. Don't take everything else down with it.
  • If a process can fail quietly, give it a way to speak up.