Skip to content

Commit 434c8a3

Browse files
committed
cleanup code
1 parent 0719879 commit 434c8a3

5 files changed

Lines changed: 31 additions & 10 deletions

File tree

apps/sim/background/cleanup-logs.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ vi.mock('@/lib/cleanup/batch-delete', () => ({
164164
}))
165165

166166
vi.mock('@/lib/execution/payloads/large-value-metadata', () => ({
167+
LIVE_PAUSED_REFERENCE_STATUSES: ['paused', 'partially_resumed', 'cancelling'],
167168
markLargeValuesDeleted: mockMarkLargeValuesDeleted,
168169
pruneLargeValueMetadata: mockPruneLargeValueMetadata,
169170
unreferencedLargeValuePredicate: vi.fn(() => ({ op: 'unreferencedLargeValuePredicate' })),

apps/sim/background/cleanup-logs.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
type TableCleanupResult,
2020
} from '@/lib/cleanup/batch-delete'
2121
import {
22+
LIVE_PAUSED_REFERENCE_STATUSES,
2223
markLargeValuesDeleted,
2324
pruneLargeValueMetadata,
2425
unreferencedLargeValuePredicate,
@@ -35,8 +36,6 @@ interface FileDeleteStats {
3536
filesDeleteFailed: number
3637
}
3738

38-
const RESUMABLE_PAUSED_STATUSES = ['paused', 'partially_resumed', 'cancelling']
39-
4039
const WORKFLOW_LOG_CLEANUP_BATCH_SIZE = 500
4140
const WORKFLOW_LOG_CLEANUP_MAX_BATCHES = 50
4241
const WORKFLOW_LOG_CLEANUP_ROW_LIMIT =
@@ -254,7 +253,7 @@ async function cleanupLegacyLargeExecutionValues(
254253
SELECT 1
255254
FROM ${pausedExecutions} AS ref_pe
256255
WHERE ref_pe.execution_id = ref.execution_id
257-
AND ref_pe.status = ANY(${RESUMABLE_PAUSED_STATUSES}::text[])
256+
AND ref_pe.status = ANY(${LIVE_PAUSED_REFERENCE_STATUSES}::text[])
258257
)
259258
)
260259
)
@@ -277,7 +276,7 @@ async function cleanupLegacyLargeExecutionValues(
277276
SELECT 1
278277
FROM ${pausedExecutions} AS parent_owner_pe
279278
WHERE parent_owner_pe.execution_id = parent_value.owner_execution_id
280-
AND parent_owner_pe.status = ANY(${RESUMABLE_PAUSED_STATUSES}::text[])
279+
AND parent_owner_pe.status = ANY(${LIVE_PAUSED_REFERENCE_STATUSES}::text[])
281280
)
282281
OR EXISTS (
283282
SELECT 1
@@ -298,7 +297,7 @@ async function cleanupLegacyLargeExecutionValues(
298297
SELECT 1
299298
FROM ${pausedExecutions} AS parent_ref_pe
300299
WHERE parent_ref_pe.execution_id = parent_ref.execution_id
301-
AND parent_ref_pe.status = ANY(${RESUMABLE_PAUSED_STATUSES}::text[])
300+
AND parent_ref_pe.status = ANY(${LIVE_PAUSED_REFERENCE_STATUSES}::text[])
302301
)
303302
)
304303
)
@@ -314,7 +313,7 @@ async function cleanupLegacyLargeExecutionValues(
314313
SELECT 1
315314
FROM ${pausedExecutions} AS pe
316315
WHERE pe.execution_id = split_part(${workspaceFiles.key}, '/', 4)
317-
AND pe.status = ANY(${RESUMABLE_PAUSED_STATUSES}::text[])
316+
AND pe.status = ANY(${LIVE_PAUSED_REFERENCE_STATUSES}::text[])
318317
)`
319318
)
320319
)
@@ -395,7 +394,7 @@ async function cleanupWorkflowExecutionLogs(
395394
lt(workflowExecutionLogs.startedAt, retentionDate),
396395
or(
397396
isNull(pausedExecutions.status),
398-
notInArray(pausedExecutions.status, RESUMABLE_PAUSED_STATUSES)
397+
notInArray(pausedExecutions.status, [...LIVE_PAUSED_REFERENCE_STATUSES])
399398
)
400399
)
401400
)

apps/sim/lib/execution/payloads/large-value-metadata.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const LARGE_VALUE_METADATA_WRITE_CHUNK_SIZE = 500
1919
const LARGE_VALUE_METADATA_WORKSPACE_CHUNK_SIZE = 50
2020
const LARGE_VALUE_METADATA_PRUNE_BATCH_SIZE = 1_000
2121
const LARGE_VALUE_METADATA_PRUNE_MAX_ROWS_PER_TABLE = 5_000
22-
const LIVE_PAUSED_REFERENCE_STATUSES = ['paused', 'partially_resumed', 'cancelling'] as const
22+
export const LIVE_PAUSED_REFERENCE_STATUSES = ['paused', 'partially_resumed', 'cancelling'] as const
2323

2424
export interface LargeValueOwner {
2525
key: string

apps/sim/lib/execution/payloads/store.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,27 @@ describe('large execution payload store', () => {
155155
expect(mockDeleteFileMetadata).not.toHaveBeenCalled()
156156
})
157157

158+
it('falls back to memory-only refs for non-durable writes when orphan cleanup fails', async () => {
159+
const value = { payload: 'x'.repeat(2048) }
160+
const json = JSON.stringify(value)
161+
mockRegisterLargeValueOwner.mockResolvedValueOnce(false)
162+
mockDeleteFiles.mockImplementationOnce(async ([key]) => ({
163+
deleted: 0,
164+
failed: [{ key }],
165+
}))
166+
167+
const ref = await storeLargeValue(value, json, Buffer.byteLength(json, 'utf8'), {
168+
workspaceId: 'workspace-1',
169+
workflowId: 'workflow-1',
170+
executionId: 'execution-1',
171+
userId: 'user-1',
172+
})
173+
174+
expect(ref.key).toBeUndefined()
175+
expect(materializeLargeValueRefSync(ref, { executionId: 'execution-1' })).toEqual(value)
176+
expect(mockDeleteFileMetadata).not.toHaveBeenCalled()
177+
})
178+
158179
it('passes nested large value refs to owner metadata registration', async () => {
159180
const nestedKey =
160181
'execution/workspace-1/workflow-1/source-execution/large-value-lv_abcdefghijkl.json'

apps/sim/lib/execution/payloads/store.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,8 @@ export async function storeLargeValue(
175175
if (key) {
176176
const registered = await registerPersistedValueOwner(key, size, referencedKeys, context)
177177
if (!registered) {
178-
const cleaned = await deleteUntrackedPersistedValue(key)
179-
if (!cleaned || context.requireDurable) {
178+
await deleteUntrackedPersistedValue(key)
179+
if (context.requireDurable) {
180180
throw new Error('Failed to persist large execution value metadata')
181181
}
182182
key = undefined

0 commit comments

Comments
 (0)