Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 3.8k
fix(tables): per-batch delete-job commits, real trigger.dev retries, post-index ANALYZE guard#4997
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,13 @@ | ||
| import { createLogger } from '@sim/logger' | ||
| import { getErrorMessage } from '@sim/utils/errors' | ||
| import { getErrorMessage, toError } from '@sim/utils/errors' | ||
| import { generateId } from '@sim/utils/id' | ||
| import { truncate } from '@sim/utils/string' | ||
| import type { Filter } from '@/lib/table' | ||
| import { TABLE_LIMITS, USER_TABLE_ROWS_SQL_NAME } from '@/lib/table/constants' | ||
| import { appendTableEvent } from '@/lib/table/events' | ||
| import { | ||
| deletePageByIds, | ||
| getJobProgress, | ||
| getTableById, | ||
| markJobFailed, | ||
| markJobReady, | ||
| @@ -38,12 +40,17 @@ export interface TableDeletePayload { | ||
| } | ||
| /** | ||
| * Background worker for large filtered row deletes. Runs detached on the web container (see the | ||
| * delete-async kickoff route). Deletes in keyset-paginated pages — `created_at <= cutoff` spares | ||
| * rows inserted while the job runs, and `excludeRowIds` spares specific rows (the | ||
| * "select all then deselect a few" case). Ownership-gated per page so a cancel/supersede stops | ||
| * it within one page; committed pages are never rolled back. Progress and the terminal state are | ||
| * surfaced via the table-events SSE stream. | ||
| * Background worker for large filtered row deletes (trigger.dev task, or detached on the web | ||
| * container when trigger.dev is disabled — see the delete-async kickoff route). Deletes in | ||
| * keyset-paginated pages — `created_at <= cutoff` spares rows inserted while the job runs, and | ||
| * `excludeRowIds` spares specific rows (the "select all then deselect a few" case). | ||
| * Ownership-gated per page so a cancel/supersede stops it within one page; committed batches are | ||
| * never rolled back. Progress and the terminal state are surfaced via the table-events SSE | ||
| * stream. | ||
| * | ||
| * Unexpected errors are rethrown so the caller's retry machinery sees them — the caller marks | ||
| * the job failed via `markTableDeleteFailed` once it gives up. A superseded run (cancel, or a | ||
| * newer job took the table) returns quietly. | ||
cursor[bot] marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| */ | ||
| export async function runTableDelete(payload: TableDeletePayload): Promise<void> { | ||
| const { jobId, tableId, workspaceId, filter, excludeRowIds, cutoff } = payload | ||
| @@ -58,8 +65,14 @@ export async function runTableDelete(payload: TableDeletePayload): Promise<void> | ||
| : undefined | ||
| const excluded = new Set(excludeRowIds ?? []) | ||
| let processed = 0 | ||
| let lastReported = 0 | ||
| // Resume the persisted count: a retried attempt's earlier batches are already committed, | ||
| // so starting at zero would overwrite cumulative progress with this attempt's smaller | ||
| // number. Doubles as the initial ownership gate. | ||
| const resumed = await getJobProgress(tableId, jobId) | ||
| if (resumed === null) throw new JobSupersededError() | ||
| let processed = resumed | ||
| let lastReported = resumed | ||
| let afterId: string | undefined | ||
| while (true) { | ||
| @@ -128,19 +141,37 @@ export async function runTableDelete(payload: TableDeletePayload): Promise<void> | ||
| } catch (err) { | ||
| if (err instanceof JobSupersededError) { | ||
| logger.info(`[${requestId}] Delete superseded by a newer run; stopping`, { tableId, jobId }) | ||
| } else { | ||
| const message = getErrorMessage(err, 'Delete failed') | ||
| logger.error(`[${requestId}] Delete failed for table ${tableId}:`, err) | ||
| // Scoped to jobId — a no-op if a newer job has taken over. | ||
| await markJobFailed(tableId, jobId, message).catch(() => {}) | ||
| void appendTableEvent({ | ||
| kind: 'job', | ||
| type: 'delete', | ||
| tableId, | ||
| jobId, | ||
| status: 'failed', | ||
| error: message, | ||
| }) | ||
| return | ||
| } | ||
| // Rethrow the root cause, not the wrapper: drizzle query errors embed the full SQL + params | ||
| // list (tens of KB for a batch delete) in `message`, and `cause` does not survive | ||
| // trigger.dev's serialization between the failed `run` and `onFailure` — the clean message | ||
| // must already be the thrown error's own `message`. | ||
| const cause = toError(err).cause | ||
| const error = cause ? toError(cause) : toError(err) | ||
| logger.error(`[${requestId}] Delete failed for table ${tableId}:`, error) | ||
| throw error | ||
| } | ||
| } | ||
| /** | ||
| * Marks the delete job failed and emits the failed SSE event. Called once the caller gives up on | ||
| * the run: the trigger.dev task's `onFailure` (after retries are exhausted) or the detached | ||
| * web-container fallback (no retries). Scoped to jobId — a no-op if a newer job has taken over. | ||
| */ | ||
| export async function markTableDeleteFailed( | ||
| tableId: string, | ||
| jobId: string, | ||
| error: unknown | ||
| ): Promise<void> { | ||
| const message = truncate(getErrorMessage(toError(error).cause ?? error, 'Delete failed'), 500) | ||
| await markJobFailed(tableId, jobId, message).catch(() => {}) | ||
| void appendTableEvent({ | ||
| kind: 'job', | ||
| type: 'delete', | ||
| tableId, | ||
| jobId, | ||
| status: 'failed', | ||
| error: message, | ||
| }) | ||
| } | ||
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.