Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ Create a workflow with a Schedule trigger set to every minute, deploy it, and wa
| Outbox processing | `/api/webhooks/outbox/process` | `*/1 * * * *` | Transactional-outbox retries for billing, membership, enterprise issuance, and workflow-deployment side effects |
| Workspace file search dispatch | `/api/cron/workspace-file-search-dispatch` | `*/1 * * * *` | Dispatches indexing work for workspace file search |
| Knowledge projection | `/api/cron/knowledge-projection` | `*/1 * * * *` | Brings knowledge base search up to date with document, permission, and chunk changes |
| Activity rollup | `/api/cron/activity-rollup` | `*/2 * * * *` | Maintains the hourly workflow activity rollup that serves organization activity; idle unless `ACTIVITY_ROLLUP_MAINTENANCE` is set |
| Table row-change fold | `/api/cron/fold-table-row-changes` | `*/1 * * * *` | Folds the table row-change log into each table's stored row count and version |
| Connector sync | `/api/knowledge/connectors/sync` | `*/5 * * * *` | Knowledge base connector syncs |
| Connector member sync | `/api/knowledge/connectors/member-sync` | `*/5 * * * *` | Per-member content/access sync for indexed connectors; live Search sources are skipped |
Expand Down
28 changes: 28 additions & 0 deletions apps/sim/app/api/cron/activity-rollup/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { createLogger } from '@sim/logger'
import { getErrorMessage } from '@sim/utils/errors'
import { type NextRequest, NextResponse } from 'next/server'
import { verifyCronAuth } from '@/lib/auth/internal'
import { enqueueActivityRollupPass } from '@/lib/billing/activity-rollup/enqueue'
import { withRouteHandler } from '@/lib/core/utils/with-route-handler'

const logger = createLogger('ActivityRollupCronRoute')

export const dynamic = 'force-dynamic'
export const maxDuration = 60

/**
* The activity rollup's cron: enqueues one maintenance pass per window while the
* `activity-rollup-maintenance` flag is on, and returns once Trigger.dev accepts it.
*/
export const GET = withRouteHandler(async (request: NextRequest) => {
const authError = verifyCronAuth(request, 'Activity rollup')
if (authError) return authError

try {
const result = await enqueueActivityRollupPass()
return NextResponse.json({ success: true, ...result }, { status: result.triggered ? 202 : 200 })
} catch (error) {
logger.error('Activity rollup enqueue failed', { error: getErrorMessage(error) })
return NextResponse.json({ success: false, error: 'Enqueue failed' }, { status: 500 })
}
})
19 changes: 19 additions & 0 deletions apps/sim/background/activity-rollup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { task } from '@trigger.dev/sdk'
import {
ACTIVITY_ROLLUP_TASK_ID,
runActivityRollupMaintenance,
} from '@/lib/billing/activity-rollup/enqueue'

/**
* Runs one activity rollup pass. One runs at a time; the cron enqueues one every two minutes, so a
* failed pass is not retried: the next one starts from the state the failed one committed, since
* every hour is written together with the progress it completes.
*/
export const activityRollupTask = task({
id: ACTIVITY_ROLLUP_TASK_ID,
machine: 'small-1x',
maxDuration: 5 * 60,
retry: { maxAttempts: 1 },
queue: { name: ACTIVITY_ROLLUP_TASK_ID, concurrencyLimit: 1 },
run: async () => runActivityRollupMaintenance(),
})
10 changes: 8 additions & 2 deletions apps/sim/background/cleanup-logs-files.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,15 @@ const payload = {
retentionHours: 720,
workspaceIds: ['workspace-1'],
}
const startedAt = new Date('2026-01-01T10:00:00.000Z')
const rows = [
{ id: 'log-1', files: [{ key: 'file-a' }, { key: 'file-b' }] },
{ id: 'log-2', files: [{ key: 'file-c' }] },
{
id: 'log-1',
files: [{ key: 'file-a' }, { key: 'file-b' }],
workspaceId: 'workspace-1',
startedAt,
},
{ id: 'log-2', files: [{ key: 'file-c' }], workspaceId: 'workspace-1', startedAt },
]

describe('bounded log cleanup file failures', () => {
Expand Down
4 changes: 4 additions & 0 deletions apps/sim/background/cleanup-logs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ describe('cleanup logs worker', () => {
expect(dbChainMockFns.select).toHaveBeenCalledWith({
id: schemaMock.workflowExecutionLogs.id,
files: schemaMock.workflowExecutionLogs.files,
workspaceId: schemaMock.workflowExecutionLogs.workspaceId,
startedAt: schemaMock.workflowExecutionLogs.startedAt,
})
expect(dbChainMockFns.execute).not.toHaveBeenCalled()
expect(mockDeleteFiles).toHaveBeenCalledWith(
Expand Down Expand Up @@ -159,6 +161,8 @@ describe('cleanup logs worker', () => {
expect(dbChainMockFns.select).toHaveBeenCalledWith({
id: schemaMock.workflowExecutionLogs.id,
files: schemaMock.workflowExecutionLogs.files,
workspaceId: schemaMock.workflowExecutionLogs.workspaceId,
startedAt: schemaMock.workflowExecutionLogs.startedAt,
})
expect(dbChainMockFns.select).not.toHaveBeenCalledWith(
expect.objectContaining({ executionData: expect.anything() })
Expand Down
4 changes: 4 additions & 0 deletions apps/sim/background/cleanup-logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { createLogger } from '@sim/logger'
import { chunkArray } from '@sim/utils/helpers'
import { task } from '@trigger.dev/sdk'
import { and, asc, eq, inArray, isNull, lt, notInArray, or, sql } from 'drizzle-orm'
import { markWorkflowActivityDirty } from '@/lib/billing/activity-rollup/marks'
import { type CleanupJobPayload, runCleanupWithLimits } from '@/lib/billing/cleanup-dispatcher'
import {
batchDeleteByWorkspaceAndTimestamp,
Expand Down Expand Up @@ -415,6 +416,8 @@ async function cleanupWorkflowExecutionLogs(
.select({
id: workflowExecutionLogs.id,
files: workflowExecutionLogs.files,
workspaceId: workflowExecutionLogs.workspaceId,
startedAt: workflowExecutionLogs.startedAt,
})
.from(workflowExecutionLogs)
.leftJoin(
Expand All @@ -438,6 +441,7 @@ async function cleanupWorkflowExecutionLogs(
if (budget && fileStats.filesDeleteFailed) throw new Error('Log file cleanup failed')
}
},
afterDelete: (tx, rows) => markWorkflowActivityDirty(tx, rows),
batchSize: WORKFLOW_LOG_CLEANUP_BATCH_SIZE,
maxBatches: WORKFLOW_LOG_CLEANUP_MAX_BATCHES,
totalRowLimit: WORKFLOW_LOG_CLEANUP_ROW_LIMIT,
Expand Down
65 changes: 65 additions & 0 deletions apps/sim/lib/billing/activity-rollup/cells.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { describe, expect, it, vi } from 'vitest'
import { type ActivityCell, aggregateHour } from '@/lib/billing/activity-rollup/cells'
import type { InstantRange } from '@/lib/billing/activity-rollup/source'

const HOUR = 60 * 60 * 1000
const hour = new Date('2026-05-01T10:00:00.000Z')

function cell(workflowId: string, runs: number, durationSumMs: number): ActivityCell {
return {
workspaceId: 'ws',
workflowId,
trigger: 'api',
runs,
completed: runs,
failed: 0,
cancelled: 0,
inFlight: 0,
durationSumMs,
durationCount: runs,
}
}

function cancellation(message: string, code: string) {
return Object.assign(new Error(message), { code })
}

describe('aggregateHour', () => {
it.each([
['times out', cancellation('canceling statement due to statement timeout', '57014')],
[
'is cancelled by replica recovery',
cancellation('canceling statement due to conflict with recovery', '40001'),
],
])(
'reads an hour whose whole read %s in quarter-hour slices summed by cell',
async (_, error) => {
const read = vi.fn(async (range: InstantRange) => {
if (range.to.getTime() - range.from.getTime() === HOUR) throw error
const quarter = (range.from.getTime() - hour.getTime()) / (HOUR / 4)
return quarter === 2 ? [cell('a', 1, 10), cell('b', 2, 30)] : [cell('a', 1, 5)]
})

const cells = await aggregateHour(hour, read)

expect(read.mock.calls.map(([range]) => range.from.toISOString())).toEqual([
'2026-05-01T10:00:00.000Z',
'2026-05-01T10:00:00.000Z',
'2026-05-01T10:15:00.000Z',
'2026-05-01T10:30:00.000Z',
'2026-05-01T10:45:00.000Z',
])
expect(read.mock.calls.at(-1)?.[0].to.toISOString()).toBe('2026-05-01T11:00:00.000Z')
expect(cells).toEqual([cell('a', 4, 25), cell('b', 2, 30)])
}
)

it('fails without slicing when the read fails for any other reason', async () => {
const read = vi.fn(async () => {
throw cancellation('relation "workflow_execution_logs" does not exist', '42P01')
})

await expect(aggregateHour(hour, read)).rejects.toThrow('does not exist')
expect(read).toHaveBeenCalledTimes(1)
})
})
Loading
Loading