Repository navigation
fix: clean up expired and failed CSV/JSON exports - #14758
Conversation
shutdown() uploads the export before the worker checks for errors, so a failed export keeps its object in storage with no file row pointing at it.
Security rulesNo new WARNING or ERROR findings from security rules. 6 existing findings tracked in
|
Export file rows store the user's filename without an extension, so the
endsWith('name', '.csv') filter matched nothing and JSON exports were never
selected. The platform default bucket only holds migration transfer files,
so expire everything in it after the export retention.
A failed export already had its file uploaded by shutdown(); the worker
calls error() on failure, which now removes the uploaded file and the
local working copy.
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
…ites The failed-export cases belong with the other destination tests rather than in a file of their own.
…nation The new imports in CSVTest shift the anonymous class from line 124 to 127, and the baseline entry names the class by its line.
|
Review complete. 🟡 1 medium 💬 Inline comments (1)
The PR makes failed export runs clean up after themselves:
Reviewed commit: 21326ba |
🟢 Tier S · Ready to mergeThe PR adds ownership-aware cleanup for failed CSV/JSON exports and creates platform file records for completed exports, while expiring old files from the default bucket and removing stranded export rows. It also updates migration retry and stale-state handling, and adds destination ownership tests plus worker-test adjustments. The latest change narrows stale migration recovery to rows in Latest changes: The newest commit renames the stale-migration recovery helper and limits its query to
Couldn't attach to a lineThese findings could not be placed on a changed line, so they were not posted as inline comments.
📂 Walkthrough · 8
Reviewed the commits since |
There was a problem hiding this comment.
Adds error() cleanup to CSV/JSON migration destinations so failed data-export workers delete staged output files, and widens the weekly Deletes worker cleanup to all expired files in the default bucket instead of .csv files only.
Key findings
- 🟡 Finalization failure deletes an already-published export file — CSV.php:238
A run that never uploaded the file, or already handed it to its caller, must not delete it: a retry of the same migration writes to the same path, and the worker keeps going after the file row is created.
error() deleted the export path unconditionally, so a failure after the file row was created removed the object from under the row, and a retry that failed early could delete the file another run had published. The destination now marks the file as its own when shutdown() starts the upload and gives it up in success(); the worker calls success() right after it creates the file row.
Workers with different storage devices can share one platform database. An S3 delete of a missing key still succeeds, so a worker that does not hold the object would drop the row and the worker that does would never clean the object. Skip rows whose object is not on this device, and keep the row when the delete fails so the next run retries it.
…bles handleDataExportComplete() now takes the destination so it can hand the file over once the file row exists; the worker test asserts it does so for every export it finalizes.
Only an export destination waits for its file row before succeeding; every other destination still succeeds before its source, as before.
…eing The retry endpoint checked for `failed` but never stored the new status, so a second retry could enqueue another attempt of the same migration, and both attempts write to the same export path. Re-read and persist `pending` under a per-migration lock so a concurrent or repeated retry is refused.
A row whose object is already gone (deleted before its row, for instance) was skipped on every run. Past twice the retention every worker sharing the database has had a full cycle to clean its own object, so such a row is stranded and is removed.
Worker behaviour is covered by the package tests and E2E; the worker unit test only needs the destination argument to compile.
The retry now stores `pending` before publishing. If publishing returns false or throws, nothing will pick the migration up, so it stayed pending and every later retry was refused as in progress. Put it back to `failed` (only while it is still pending) and report the error.
A publish exception does not prove the queue rejected the message, so restoring `failed` could let a second retry run alongside the first. Only a `false` from enqueue() restores `failed` now; on an exception the migration stays `pending`, and the stuck-migration sweep, which already fails `processing` migrations untouched for three days, fails `pending` ones too.
The race between two retries of the same migration predates the export cleanup, and each fix to the retry path surfaced a new queue edge case. Restore the retry endpoint and the stuck-migration sweep to their previous behaviour so this change stays about export files.
fix: clean up expired and failed CSV/JSON exports (cherry picked from commit dcc02c9)
What
Expired CSV/JSON exports were never deleted, and a failed export left its file in storage.
deleteOldCSVExportsfilteredendsWith('name', '.csv'), but export rows store the user's filename without an extension, so nothing matched; JSON was never covereddefaultbucket (it only holds migration transfer files) +createdBefore(DATA_EXPORT_RETENTION)CSV/JSONdestinationshutdown()uploads the file before the worker sees the errors; the worker then marks the migration failed and callserror(), which was a no-operror()deletes the file this run uploaded, plus the local working copyThe
defaultbucket also holds files the Console stages for imports; those are copied out when the import is created, so they now expire after the same week too.Ownership
$destination->success()right after creating the row, soerror()leaves the file in placeerror()only deletes what its ownshutdown()uploadedDATA_EXPORT_RETENTION, after every worker sharing the database has had a full cycleTests
CSVTest/JSONTest:test{CSV,JSON}ExportFailureLeavesNoFile(the uploader'serror()removes the file) andtest{CSV,JSON}ExportFailureKeepsAFileItDoesNotOwn(aftersuccess(), or from a run that never uploaded, the file survives). Each failed on CI before its fix (0a81ce9, 734a425) and passes with it.The retention cleanup has no E2E: the retention is a fixed week, so an E2E cannot create an expired export.
The race between two retries of the same migration predates this PR and is left to a separate change.
Related: appwrite-labs/cloud#6628