chore: import upstream snapshot with attribution
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,337 @@
|
||||
import { createLogger } from '@sim/logger'
|
||||
import { toError } from '@sim/utils/errors'
|
||||
import { generateId } from '@sim/utils/id'
|
||||
import { type NextRequest, NextResponse } from 'next/server'
|
||||
import {
|
||||
getPauseContextDetailContract,
|
||||
resumeWorkflowExecutionContextContract,
|
||||
} from '@/lib/api/contracts/workflows'
|
||||
import { parseRequest } from '@/lib/api/server'
|
||||
import { AuthType } from '@/lib/auth/hybrid'
|
||||
import { getJobQueue } from '@/lib/core/async-jobs'
|
||||
import { generateRequestId } from '@/lib/core/utils/request'
|
||||
import { SSE_HEADERS } from '@/lib/core/utils/sse'
|
||||
import { getBaseUrl } from '@/lib/core/utils/urls'
|
||||
import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
|
||||
import { preprocessExecution } from '@/lib/execution/preprocessing'
|
||||
import { PauseResumeManager } from '@/lib/workflows/executor/human-in-the-loop-manager'
|
||||
import { createStreamingResponse } from '@/lib/workflows/streaming/streaming'
|
||||
import { getWorkspaceBilledAccountUserId } from '@/lib/workspaces/utils'
|
||||
import { validateWorkflowAccess } from '@/app/api/workflows/middleware'
|
||||
import type { ResumeExecutionPayload } from '@/background/resume-execution'
|
||||
import { ExecutionSnapshot } from '@/executor/execution/snapshot'
|
||||
import type { SerializedSnapshot } from '@/executor/types'
|
||||
|
||||
const logger = createLogger('WorkflowResumeAPI')
|
||||
|
||||
export const runtime = 'nodejs'
|
||||
export const dynamic = 'force-dynamic'
|
||||
|
||||
function getStoredSnapshotConfig(pausedExecution: { executionSnapshot: unknown }): {
|
||||
executionMode?: 'sync' | 'stream' | 'async'
|
||||
selectedOutputs?: string[]
|
||||
} {
|
||||
try {
|
||||
const serialized = pausedExecution.executionSnapshot as SerializedSnapshot
|
||||
const snapshot = ExecutionSnapshot.fromJSON(serialized.snapshot)
|
||||
return {
|
||||
executionMode: snapshot.metadata.executionMode,
|
||||
selectedOutputs: snapshot.selectedOutputs,
|
||||
}
|
||||
} catch {
|
||||
return {}
|
||||
}
|
||||
}
|
||||
|
||||
export const POST = withRouteHandler(
|
||||
async (
|
||||
request: NextRequest,
|
||||
context: {
|
||||
params: Promise<{ workflowId: string; executionId: string; contextId: string }>
|
||||
}
|
||||
) => {
|
||||
const parsed = await parseRequest(resumeWorkflowExecutionContextContract, request, context)
|
||||
if (!parsed.success) return parsed.response
|
||||
const { workflowId, executionId, contextId } = parsed.data.params
|
||||
|
||||
const access = await validateWorkflowAccess(request, workflowId, false)
|
||||
if (access.error) {
|
||||
return NextResponse.json({ error: access.error.message }, { status: access.error.status })
|
||||
}
|
||||
|
||||
const workflow = access.workflow
|
||||
|
||||
let payload: unknown = {}
|
||||
try {
|
||||
payload = await request.json()
|
||||
} catch {
|
||||
payload = {}
|
||||
}
|
||||
|
||||
const resumeInput =
|
||||
typeof payload === 'object' && payload !== null && 'input' in payload
|
||||
? payload.input
|
||||
: (payload ?? {})
|
||||
const isPersonalApiKeyCaller =
|
||||
access.auth?.authType === AuthType.API_KEY && access.auth?.apiKeyType === 'personal'
|
||||
|
||||
let userId: string
|
||||
if (isPersonalApiKeyCaller && access.auth?.userId) {
|
||||
userId = access.auth.userId
|
||||
} else {
|
||||
const billedAccountUserId = await getWorkspaceBilledAccountUserId(workflow.workspaceId)
|
||||
if (!billedAccountUserId) {
|
||||
logger.error('Unable to resolve workspace billed account for resume execution', {
|
||||
workflowId,
|
||||
workspaceId: workflow.workspaceId,
|
||||
})
|
||||
return NextResponse.json(
|
||||
{ error: 'Unable to resolve billing account for this workspace' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
userId = billedAccountUserId
|
||||
}
|
||||
|
||||
const resumeExecutionId = generateId()
|
||||
const requestId = generateRequestId()
|
||||
|
||||
logger.info(`[${requestId}] Preprocessing resume execution`, {
|
||||
workflowId,
|
||||
parentExecutionId: executionId,
|
||||
resumeExecutionId,
|
||||
userId,
|
||||
})
|
||||
|
||||
const preprocessResult = await preprocessExecution({
|
||||
workflowId,
|
||||
userId,
|
||||
triggerType: 'manual',
|
||||
executionId: resumeExecutionId,
|
||||
requestId,
|
||||
checkRateLimit: false,
|
||||
checkDeployment: false,
|
||||
skipUsageLimits: true,
|
||||
useAuthenticatedUserAsActor: isPersonalApiKeyCaller,
|
||||
workspaceId: workflow.workspaceId || undefined,
|
||||
})
|
||||
|
||||
if (!preprocessResult.success) {
|
||||
logger.warn(`[${requestId}] Preprocessing failed for resume`, {
|
||||
workflowId,
|
||||
parentExecutionId: executionId,
|
||||
error: preprocessResult.error?.message,
|
||||
statusCode: preprocessResult.error?.statusCode,
|
||||
})
|
||||
|
||||
return NextResponse.json(
|
||||
{
|
||||
error:
|
||||
preprocessResult.error?.message ||
|
||||
'Failed to validate resume execution. Please try again.',
|
||||
},
|
||||
{ status: preprocessResult.error?.statusCode || 400 }
|
||||
)
|
||||
}
|
||||
|
||||
logger.info(`[${requestId}] Preprocessing passed, proceeding with resume`, {
|
||||
workflowId,
|
||||
parentExecutionId: executionId,
|
||||
resumeExecutionId,
|
||||
actorUserId: preprocessResult.actorUserId,
|
||||
})
|
||||
|
||||
try {
|
||||
const enqueueResult = await PauseResumeManager.enqueueOrStartResume({
|
||||
executionId,
|
||||
workflowId,
|
||||
contextId,
|
||||
resumeInput,
|
||||
userId,
|
||||
allowedPauseKinds: ['human'],
|
||||
})
|
||||
|
||||
if (enqueueResult.status === 'queued') {
|
||||
return NextResponse.json({
|
||||
status: 'queued',
|
||||
executionId: enqueueResult.resumeExecutionId,
|
||||
queuePosition: enqueueResult.queuePosition,
|
||||
message: 'Resume queued. It will run after current resumes finish.',
|
||||
})
|
||||
}
|
||||
|
||||
const resumeArgs = {
|
||||
resumeEntryId: enqueueResult.resumeEntryId,
|
||||
resumeExecutionId: enqueueResult.resumeExecutionId,
|
||||
pausedExecution: enqueueResult.pausedExecution,
|
||||
contextId: enqueueResult.contextId,
|
||||
resumeInput: enqueueResult.resumeInput,
|
||||
userId: enqueueResult.userId,
|
||||
}
|
||||
|
||||
const isApiCaller = access.auth?.authType === AuthType.API_KEY
|
||||
const snapshotConfig = isApiCaller
|
||||
? getStoredSnapshotConfig(enqueueResult.pausedExecution)
|
||||
: {}
|
||||
const executionMode = isApiCaller ? (snapshotConfig.executionMode ?? 'sync') : undefined
|
||||
|
||||
if (isApiCaller && executionMode === 'stream') {
|
||||
const stream = await createStreamingResponse({
|
||||
requestId,
|
||||
streamConfig: {
|
||||
selectedOutputs: snapshotConfig.selectedOutputs,
|
||||
timeoutMs: preprocessResult.executionTimeout?.sync,
|
||||
},
|
||||
executionId: enqueueResult.resumeExecutionId,
|
||||
workspaceId: workflow.workspaceId || undefined,
|
||||
workflowId,
|
||||
userId: enqueueResult.userId,
|
||||
allowLargeValueWorkflowScope: true,
|
||||
executeFn: async ({ onStream, onBlockComplete, abortSignal }) =>
|
||||
PauseResumeManager.startResumeExecution({
|
||||
...resumeArgs,
|
||||
onStream,
|
||||
onBlockComplete,
|
||||
abortSignal,
|
||||
}),
|
||||
})
|
||||
|
||||
return new NextResponse(stream, {
|
||||
headers: {
|
||||
...SSE_HEADERS,
|
||||
'X-Execution-Id': enqueueResult.resumeExecutionId,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
if (isApiCaller && executionMode === 'sync') {
|
||||
const result = await PauseResumeManager.startResumeExecution(resumeArgs)
|
||||
|
||||
return NextResponse.json({
|
||||
success: result.success,
|
||||
status: result.status ?? (result.success ? 'completed' : 'failed'),
|
||||
executionId: enqueueResult.resumeExecutionId,
|
||||
output: result.output,
|
||||
error: result.error,
|
||||
metadata: result.metadata
|
||||
? {
|
||||
duration: result.metadata.duration,
|
||||
startTime: result.metadata.startTime,
|
||||
endTime: result.metadata.endTime,
|
||||
}
|
||||
: undefined,
|
||||
})
|
||||
}
|
||||
|
||||
if (isApiCaller && executionMode === 'async') {
|
||||
const resumePayload: ResumeExecutionPayload = {
|
||||
resumeEntryId: enqueueResult.resumeEntryId,
|
||||
resumeExecutionId: enqueueResult.resumeExecutionId,
|
||||
pausedExecutionId: enqueueResult.pausedExecution.id,
|
||||
contextId: enqueueResult.contextId,
|
||||
resumeInput: enqueueResult.resumeInput,
|
||||
userId: enqueueResult.userId,
|
||||
workflowId,
|
||||
parentExecutionId: executionId,
|
||||
}
|
||||
|
||||
let jobId: string
|
||||
try {
|
||||
const jobQueue = await getJobQueue()
|
||||
jobId = await jobQueue.enqueue('resume-execution', resumePayload, {
|
||||
metadata: { workflowId, workspaceId: workflow.workspaceId, userId },
|
||||
})
|
||||
logger.info('Enqueued async resume execution', {
|
||||
jobId,
|
||||
resumeExecutionId: enqueueResult.resumeExecutionId,
|
||||
})
|
||||
} catch (dispatchError) {
|
||||
logger.error('Failed to dispatch async resume execution', {
|
||||
error: toError(dispatchError).message,
|
||||
resumeExecutionId: enqueueResult.resumeExecutionId,
|
||||
})
|
||||
await PauseResumeManager.markResumeAttemptFailed({
|
||||
resumeEntryId: enqueueResult.resumeEntryId,
|
||||
pausedExecutionId: enqueueResult.pausedExecution.id,
|
||||
parentExecutionId: executionId,
|
||||
contextId: enqueueResult.contextId,
|
||||
failureReason: 'Failed to queue async resume execution',
|
||||
})
|
||||
await PauseResumeManager.processQueuedResumes(executionId, workflowId)
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to queue resume execution. Please try again.' },
|
||||
{ status: 503 }
|
||||
)
|
||||
}
|
||||
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: true,
|
||||
async: true,
|
||||
jobId,
|
||||
executionId: enqueueResult.resumeExecutionId,
|
||||
message: 'Resume execution queued',
|
||||
statusUrl: `${getBaseUrl()}/api/jobs/${jobId}`,
|
||||
},
|
||||
{ status: 202 }
|
||||
)
|
||||
}
|
||||
|
||||
PauseResumeManager.startResumeExecution(resumeArgs).catch((error) => {
|
||||
logger.error('Failed to start resume execution', {
|
||||
workflowId,
|
||||
parentExecutionId: executionId,
|
||||
resumeExecutionId: enqueueResult.resumeExecutionId,
|
||||
error,
|
||||
})
|
||||
})
|
||||
|
||||
return NextResponse.json({
|
||||
status: 'started',
|
||||
executionId: enqueueResult.resumeExecutionId,
|
||||
message: 'Resume execution started.',
|
||||
})
|
||||
} catch (error) {
|
||||
logger.error('Resume request failed', {
|
||||
workflowId,
|
||||
executionId,
|
||||
contextId,
|
||||
error,
|
||||
})
|
||||
return NextResponse.json(
|
||||
{ error: toError(error).message || 'Failed to queue resume request' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
export const GET = withRouteHandler(
|
||||
async (
|
||||
request: NextRequest,
|
||||
context: {
|
||||
params: Promise<{ workflowId: string; executionId: string; contextId: string }>
|
||||
}
|
||||
) => {
|
||||
const parsed = await parseRequest(getPauseContextDetailContract, request, context)
|
||||
if (!parsed.success) return parsed.response
|
||||
const { workflowId, executionId, contextId } = parsed.data.params
|
||||
|
||||
const access = await validateWorkflowAccess(request, workflowId, false)
|
||||
if (access.error) {
|
||||
return NextResponse.json({ error: access.error.message }, { status: access.error.status })
|
||||
}
|
||||
|
||||
const detail = await PauseResumeManager.getPauseContextDetail({
|
||||
workflowId,
|
||||
executionId,
|
||||
contextId,
|
||||
})
|
||||
|
||||
if (!detail) {
|
||||
return NextResponse.json({ error: 'Pause context not found' }, { status: 404 })
|
||||
}
|
||||
|
||||
return NextResponse.json(detail)
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,51 @@
|
||||
import { createLogger } from '@sim/logger'
|
||||
import { type NextRequest, NextResponse } from 'next/server'
|
||||
import { resumeWorkflowExecutionContract } from '@/lib/api/contracts/workflows'
|
||||
import { parseRequest } from '@/lib/api/server'
|
||||
import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
|
||||
import { PauseResumeManager } from '@/lib/workflows/executor/human-in-the-loop-manager'
|
||||
import { validateWorkflowAccess } from '@/app/api/workflows/middleware'
|
||||
|
||||
const logger = createLogger('WorkflowResumeExecutionAPI')
|
||||
|
||||
export const runtime = 'nodejs'
|
||||
export const dynamic = 'force-dynamic'
|
||||
|
||||
export const GET = withRouteHandler(
|
||||
async (
|
||||
request: NextRequest,
|
||||
context: { params: Promise<{ workflowId: string; executionId: string }> }
|
||||
) => {
|
||||
const parsed = await parseRequest(resumeWorkflowExecutionContract, request, context)
|
||||
if (!parsed.success) return parsed.response
|
||||
const { workflowId, executionId } = parsed.data.params
|
||||
|
||||
const access = await validateWorkflowAccess(request, workflowId, false)
|
||||
if (access.error) {
|
||||
return NextResponse.json({ error: access.error.message }, { status: access.error.status })
|
||||
}
|
||||
|
||||
try {
|
||||
const detail = await PauseResumeManager.getPausedExecutionDetail({
|
||||
workflowId,
|
||||
executionId,
|
||||
})
|
||||
|
||||
if (!detail) {
|
||||
return NextResponse.json({ error: 'Paused execution not found' }, { status: 404 })
|
||||
}
|
||||
|
||||
return NextResponse.json(detail)
|
||||
} catch (error: any) {
|
||||
logger.error('Failed to load paused execution detail', {
|
||||
workflowId,
|
||||
executionId,
|
||||
error,
|
||||
})
|
||||
return NextResponse.json(
|
||||
{ error: error?.message || 'Failed to load paused execution detail' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,187 @@
|
||||
import { db } from '@sim/db'
|
||||
import { pausedExecutions } from '@sim/db/schema'
|
||||
import { createLogger } from '@sim/logger'
|
||||
import { toError } from '@sim/utils/errors'
|
||||
import { generateShortId } from '@sim/utils/id'
|
||||
import { and, asc, inArray, isNotNull, lte } from 'drizzle-orm'
|
||||
import { type NextRequest, NextResponse } from 'next/server'
|
||||
import { verifyCronAuth } from '@/lib/auth/internal'
|
||||
import { acquireLock, releaseLock } from '@/lib/core/config/redis'
|
||||
import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
|
||||
import {
|
||||
computeEarliestResumeAt,
|
||||
PauseResumeManager,
|
||||
} from '@/lib/workflows/executor/human-in-the-loop-manager'
|
||||
import type { PausePoint } from '@/executor/types'
|
||||
|
||||
const logger = createLogger('TimePauseResumePoll')
|
||||
|
||||
export const dynamic = 'force-dynamic'
|
||||
export const maxDuration = 120
|
||||
|
||||
const LOCK_KEY = 'time-pause-resume-poll-lock'
|
||||
const LOCK_TTL_SECONDS = 180
|
||||
const POLL_BATCH_LIMIT = 200
|
||||
|
||||
interface DispatchFailure {
|
||||
executionId: string
|
||||
contextId: string
|
||||
error: string
|
||||
}
|
||||
|
||||
interface RowResult {
|
||||
dispatched: number
|
||||
failures: DispatchFailure[]
|
||||
}
|
||||
|
||||
export const GET = withRouteHandler(async (request: NextRequest) => {
|
||||
const requestId = generateShortId()
|
||||
|
||||
const authError = verifyCronAuth(request, 'Time-pause resume poll')
|
||||
if (authError) return authError
|
||||
|
||||
const lockAcquired = await acquireLock(LOCK_KEY, requestId, LOCK_TTL_SECONDS)
|
||||
if (!lockAcquired) {
|
||||
return NextResponse.json(
|
||||
{ success: true, message: 'Polling already in progress – skipped', requestId },
|
||||
{ status: 202 }
|
||||
)
|
||||
}
|
||||
|
||||
try {
|
||||
const now = new Date()
|
||||
|
||||
const dueRows = await db
|
||||
.select({
|
||||
id: pausedExecutions.id,
|
||||
executionId: pausedExecutions.executionId,
|
||||
workflowId: pausedExecutions.workflowId,
|
||||
pausePoints: pausedExecutions.pausePoints,
|
||||
metadata: pausedExecutions.metadata,
|
||||
})
|
||||
.from(pausedExecutions)
|
||||
.where(
|
||||
and(
|
||||
// 'partially_resumed' rows occur when a chained-pause workflow advanced past
|
||||
// an earlier wait — e.g. wait1 → agent → wait2 — and now wait2's time pause
|
||||
// is the one waiting for the cron. Include it alongside fresh 'paused' rows.
|
||||
inArray(pausedExecutions.status, ['paused', 'partially_resumed']),
|
||||
isNotNull(pausedExecutions.nextResumeAt),
|
||||
lte(pausedExecutions.nextResumeAt, now)
|
||||
)
|
||||
)
|
||||
.orderBy(asc(pausedExecutions.nextResumeAt))
|
||||
.limit(POLL_BATCH_LIMIT)
|
||||
|
||||
const results = await Promise.all(dueRows.map((row) => dispatchRow(row, now)))
|
||||
const dispatched = results.reduce((sum, r) => sum + r.dispatched, 0)
|
||||
const failures = results.flatMap((r) => r.failures)
|
||||
|
||||
logger.info('Time-pause resume poll completed', {
|
||||
requestId,
|
||||
claimedRows: dueRows.length,
|
||||
dispatched,
|
||||
failureCount: failures.length,
|
||||
})
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
requestId,
|
||||
claimedRows: dueRows.length,
|
||||
dispatched,
|
||||
failures,
|
||||
})
|
||||
} catch (error) {
|
||||
const message = toError(error).message
|
||||
logger.error('Time-pause resume poll failed', { requestId, error: message })
|
||||
return NextResponse.json({ success: false, requestId, error: message }, { status: 500 })
|
||||
} finally {
|
||||
await releaseLock(LOCK_KEY, requestId).catch(() => {})
|
||||
}
|
||||
})
|
||||
|
||||
interface DueRow {
|
||||
id: string
|
||||
executionId: string
|
||||
workflowId: string
|
||||
pausePoints: unknown
|
||||
metadata: unknown
|
||||
}
|
||||
|
||||
async function dispatchRow(row: DueRow, now: Date): Promise<RowResult> {
|
||||
const points = (row.pausePoints ?? {}) as Record<string, PausePoint>
|
||||
const metadata = (row.metadata ?? {}) as Record<string, unknown>
|
||||
const userId = typeof metadata.executorUserId === 'string' ? metadata.executorUserId : ''
|
||||
|
||||
const eligiblePoints = Object.values(points).filter(
|
||||
(point) =>
|
||||
point.pauseKind === 'time' && (!point.resumeStatus || point.resumeStatus === 'paused')
|
||||
)
|
||||
const duePoints = eligiblePoints.filter((point) => {
|
||||
if (!point.resumeAt) return false
|
||||
const at = new Date(point.resumeAt)
|
||||
return !Number.isNaN(at.getTime()) && at <= now
|
||||
})
|
||||
|
||||
const failures: DispatchFailure[] = []
|
||||
let dispatched = 0
|
||||
|
||||
for (const point of duePoints) {
|
||||
if (!point.contextId) continue
|
||||
try {
|
||||
const enqueueResult = await PauseResumeManager.enqueueOrStartResume({
|
||||
executionId: row.executionId,
|
||||
workflowId: row.workflowId,
|
||||
contextId: point.contextId,
|
||||
resumeInput: {},
|
||||
userId,
|
||||
allowedPauseKinds: ['time'],
|
||||
})
|
||||
|
||||
if (enqueueResult.status === 'starting') {
|
||||
// Route through `executeResumeJob` (not `PauseResumeManager.startResumeExecution`
|
||||
// directly) so cell-context restoration + cascade-loop continuation
|
||||
// fires. This is the same primitive the trigger.dev `resumeExecutionTask`
|
||||
// wraps — calling it directly handles both trigger.dev-disabled local
|
||||
// dev and trigger.dev-enabled prod identically.
|
||||
const { executeResumeJob } = await import('@/background/resume-execution')
|
||||
void executeResumeJob({
|
||||
resumeEntryId: enqueueResult.resumeEntryId,
|
||||
resumeExecutionId: enqueueResult.resumeExecutionId,
|
||||
pausedExecutionId: enqueueResult.pausedExecution.id,
|
||||
contextId: enqueueResult.contextId,
|
||||
resumeInput: enqueueResult.resumeInput,
|
||||
userId: enqueueResult.userId,
|
||||
workflowId: row.workflowId,
|
||||
parentExecutionId: row.executionId,
|
||||
}).catch((error) => {
|
||||
logger.error('Background time-pause resume failed', {
|
||||
executionId: row.executionId,
|
||||
contextId: point.contextId,
|
||||
error: toError(error).message,
|
||||
})
|
||||
})
|
||||
}
|
||||
dispatched++
|
||||
} catch (error) {
|
||||
const message = toError(error).message
|
||||
logger.warn('Failed to dispatch time-pause resume', {
|
||||
executionId: row.executionId,
|
||||
contextId: point.contextId,
|
||||
error: message,
|
||||
})
|
||||
failures.push({ executionId: row.executionId, contextId: point.contextId, error: message })
|
||||
}
|
||||
}
|
||||
|
||||
// We never auto-retry a failed dispatch: workflow blocks aren't idempotent, and
|
||||
// an operator must investigate stranded rows by hand. The status='paused' guard
|
||||
// also prevents clobbering when a concurrent manual resume has already advanced
|
||||
// the row's state since we read it.
|
||||
await PauseResumeManager.setNextResumeAt({
|
||||
pausedExecutionId: row.id,
|
||||
nextResumeAt: computeEarliestResumeAt(eligiblePoints, { after: now }),
|
||||
})
|
||||
|
||||
return { dispatched, failures }
|
||||
}
|
||||
Reference in New Issue
Block a user