|
1 | | -import { db } from '@sim/db' |
2 | 1 | import { createLogger } from '@sim/logger' |
3 | 2 | import { toError } from '@sim/utils/errors' |
4 | 3 | import { type NextRequest, NextResponse } from 'next/server' |
5 | | -import { adminInvitationOperationOutboxHandlers } from '@/lib/admin/invitation-operation' |
6 | | -import { adminMemberOperationOutboxHandlers } from '@/lib/admin/member-operation' |
7 | 4 | import { verifyCronAuth } from '@/lib/auth/internal' |
8 | | -import { enterpriseOwnerClaimOutboxHandlers } from '@/lib/billing/enterprise-owner-claim' |
9 | | -import { enterpriseIssuanceOutboxHandlers } from '@/lib/billing/enterprise-provisioning' |
10 | | -import { membershipBillingOutboxHandlers } from '@/lib/billing/organizations/membership-reconciliation' |
11 | | -import { billingOutboxHandlers } from '@/lib/billing/webhooks/outbox-handlers' |
12 | | -import { processOutboxEvents } from '@/lib/core/outbox/service' |
13 | | -import { DeadlineExceededError } from '@/lib/core/utils/deadline' |
| 5 | +import { enqueueOutboxProcessor } from '@/lib/core/outbox/enqueue' |
14 | 6 | import { generateRequestId } from '@/lib/core/utils/request' |
15 | 7 | import { withRouteHandler } from '@/lib/core/utils/with-route-handler' |
16 | | -import { directGrantOutboxHandlers } from '@/lib/invitations/direct-grant' |
17 | | -import { slackSearchOutboxHandlers } from '@/lib/knowledge/application/slack-search/outbox' |
18 | | -import { getConnectorFailureDiagnostic } from '@/lib/knowledge/connectors/connector-error' |
19 | | -import { knowledgeDocumentProcessingOutboxHandlers } from '@/lib/knowledge/documents/processing-outbox-handler' |
20 | | -import { recoverKnowledgeDocumentProcessing } from '@/lib/knowledge/documents/processing-recovery' |
21 | | -import { organizationResourceCleanupOutboxHandlers } from '@/lib/organizations/resource-cleanup' |
22 | | -import { permissionAccessRequestOutboxHandlers } from '@/lib/permission-access-requests/notifications' |
23 | | -import { workspaceFileLiveDocOutboxHandlers } from '@/lib/uploads/contexts/workspace/workspace-file-live-doc-outbox' |
24 | | -import { workspaceFileStorageCleanupOutboxHandlers } from '@/lib/uploads/contexts/workspace/workspace-file-storage-cleanup-outbox' |
25 | | -import { workflowDeploymentOutboxHandlers } from '@/lib/workflows/deployment-outbox' |
26 | | -import { invitationMigrationOutboxHandlers } from '@/lib/workspaces/admin-move' |
27 | | -import { workspaceOperationOutboxHandlers } from '@/lib/workspaces/operations/outbox' |
28 | | -import { forkContentOutboxHandlers } from '@/ee/workspace-forking/application/content-outbox' |
29 | | -import { reapStaleBackgroundWork } from '@/ee/workspace-forking/lib/background-work/store' |
30 | 8 |
|
31 | 9 | const logger = createLogger('OutboxProcessorAPI') |
32 | 10 |
|
33 | 11 | export const dynamic = 'force-dynamic' |
| 12 | +/** Self-hosted deployments without Trigger.dev retain the synchronous processing window. */ |
34 | 13 | export const maxDuration = 800 |
35 | 14 |
|
36 | | -const handlers = { |
37 | | - ...slackSearchOutboxHandlers, |
38 | | - ...adminInvitationOperationOutboxHandlers, |
39 | | - ...adminMemberOperationOutboxHandlers, |
40 | | - ...billingOutboxHandlers, |
41 | | - ...membershipBillingOutboxHandlers, |
42 | | - ...enterpriseIssuanceOutboxHandlers, |
43 | | - ...enterpriseOwnerClaimOutboxHandlers, |
44 | | - ...invitationMigrationOutboxHandlers, |
45 | | - ...directGrantOutboxHandlers, |
46 | | - ...knowledgeDocumentProcessingOutboxHandlers, |
47 | | - ...organizationResourceCleanupOutboxHandlers, |
48 | | - ...permissionAccessRequestOutboxHandlers, |
49 | | - ...workspaceFileLiveDocOutboxHandlers, |
50 | | - ...workspaceFileStorageCleanupOutboxHandlers, |
51 | | - ...workflowDeploymentOutboxHandlers, |
52 | | - ...workspaceOperationOutboxHandlers, |
53 | | - ...forkContentOutboxHandlers, |
54 | | -} as const |
55 | | - |
| 15 | +/** The cron secret authorizes this lifecycle endpoint; hosted processing runs outside the HTTP request. */ |
56 | 16 | export const GET = withRouteHandler(async (request: NextRequest) => { |
57 | | - const requestId = generateRequestId() |
| 17 | + const authError = verifyCronAuth(request, 'Outbox processor') |
| 18 | + if (authError) return authError |
58 | 19 |
|
| 20 | + const requestId = generateRequestId() |
59 | 21 | try { |
60 | | - const authError = verifyCronAuth(request, 'Outbox processor') |
61 | | - if (authError) { |
62 | | - return authError |
63 | | - } |
64 | | - |
65 | | - const startedAt = Date.now() |
66 | | - const result = await processOutboxEvents(handlers, { |
67 | | - batchSize: 500, |
68 | | - maxRuntimeMs: 760_000, |
69 | | - minRemainingMs: 95_000, |
70 | | - }) |
71 | | - |
72 | | - let recoveredDocuments = 0 |
73 | | - try { |
74 | | - if (Date.now() - startedAt < 770_000) { |
75 | | - recoveredDocuments = await recoverKnowledgeDocumentProcessing() |
76 | | - } |
77 | | - } catch (error) { |
78 | | - logger.error('Stored document recovery failed', { |
79 | | - requestId, |
80 | | - error: getConnectorFailureDiagnostic(error) ?? { |
81 | | - category: error instanceof DeadlineExceededError ? 'deadline' : 'internal', |
82 | | - message: |
83 | | - error instanceof DeadlineExceededError |
84 | | - ? error.message |
85 | | - : 'Unexpected stored-document recovery failure', |
86 | | - }, |
87 | | - }) |
88 | | - } |
89 | | - |
90 | | - // Reap fork background-work rows stuck `processing` past their TTL (worker crash / |
91 | | - // restart has no in-task hook). Independent of the outbox; a failure here must not |
92 | | - // fail the outbox run, so it's guarded separately. |
93 | | - let reapedBackgroundWork = 0 |
94 | | - try { |
95 | | - reapedBackgroundWork = await reapStaleBackgroundWork(db) |
96 | | - } catch (error) { |
97 | | - logger.error('Background-work reap failed', { requestId, error: toError(error).message }) |
| 22 | + const accepted = await enqueueOutboxProcessor() |
| 23 | + if (accepted.backend === 'trigger-dev') { |
| 24 | + logger.info('Outbox processor accepted', { jobId: accepted.jobId }) |
| 25 | + return NextResponse.json( |
| 26 | + { success: true, requestId, triggered: true, ...accepted }, |
| 27 | + { status: 202 } |
| 28 | + ) |
98 | 29 | } |
99 | | - |
100 | | - logger.info('Outbox processing completed', { |
101 | | - requestId, |
102 | | - ...result, |
103 | | - reapedBackgroundWork, |
104 | | - recoveredDocuments, |
105 | | - }) |
106 | | - |
107 | | - return NextResponse.json({ |
108 | | - success: true, |
109 | | - requestId, |
110 | | - result, |
111 | | - reapedBackgroundWork, |
112 | | - recoveredDocuments, |
113 | | - }) |
| 30 | + return NextResponse.json({ success: true, requestId, ...accepted.output }) |
114 | 31 | } catch (error) { |
115 | | - logger.error('Outbox processing failed', { requestId, error: toError(error).message }) |
| 32 | + logger.error('Outbox processing failed', { error: toError(error).message }) |
116 | 33 | return NextResponse.json( |
117 | 34 | { success: false, requestId, error: toError(error).message }, |
118 | 35 | { status: 500 } |
|
0 commit comments