Skip to content
38 changes: 38 additions & 0 deletions apps/sim/background/table-update.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { task } from '@trigger.dev/sdk'
import {
markTableUpdateFailed,
runTableUpdate,
type TableUpdatePayload,
} from '@/lib/table/update-runner'

/**
* `TableUpdatePayload` with the cutoff as an ISO string — task payloads cross a JSON boundary, so
* the Date is rehydrated in `run` rather than trusting payload serialization.
*/
export interface TableUpdateTaskPayload extends Omit<TableUpdatePayload, 'cutoff'> {
cutoff: string
}

/**
* Trigger.dev wrapper around `runTableUpdate`. Errors propagate out of `run` so the retry policy
* fires; the job is marked failed only in `onFailure`, after the final attempt. Retry-safe: the
* worker keysets by id with a `created_at <= cutoff` floor and the JSONB-merge patch is idempotent
* (re-applying the same patch to an already-patched row is a no-op), so a retried attempt re-walks
* and re-applies whatever remains. The `table_jobs` ownership gate stops a retried run that lost
* the job within one page.
*/
export const tableUpdateTask = task({
id: 'table-update',
machine: 'small-1x',
retry: { maxAttempts: 3 },
queue: {
name: 'table-update',
concurrencyLimit: 10,
},
run: async (payload: TableUpdateTaskPayload) => {
await runTableUpdate({ ...payload, cutoff: new Date(payload.cutoff) })
},
onFailure: async ({ payload, error }) => {
await markTableUpdateFailed(payload.tableId, payload.jobId, error)
},
})
3 changes: 2 additions & 1 deletion apps/sim/lib/copilot/generated/tool-catalog-v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3958,7 +3958,8 @@ export const UserTable: ToolCatalogEntry = {
},
limit: {
type: 'number',
description: 'Maximum rows to return or affect (optional, default 100)',
description:
'Maximum rows to return or affect (optional, default 100). Omit on update_rows_by_filter / delete_rows_by_filter to act on every match.',
},
mapping: {
type: 'object',
Expand Down
3 changes: 2 additions & 1 deletion apps/sim/lib/copilot/generated/tool-schemas-v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3686,7 +3686,8 @@ export const TOOL_RUNTIME_SCHEMAS: Record<string, ToolRuntimeSchemaEntry> = {
},
limit: {
type: 'number',
description: 'Maximum rows to return or affect (optional, default 100)',
description:
'Maximum rows to return or affect (optional, default 100). Omit on update_rows_by_filter / delete_rows_by_filter to act on every match.',
},
mapping: {
type: 'object',
Expand Down
Loading
Loading