Files
WeHub Mirror 6bf8bebf51
CI / Test and Build (push) Failing after 1s
CI / Migrate Dev DB (push) Has been skipped
CI / Migrate DB (push) Has been skipped
CodeQL / Analyze actions (push) Has been cancelled
CodeQL / Analyze javascript-typescript (push) Has been cancelled
CI / Detect Version (push) Has been cancelled
CI / Detect Desktop Changes (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/cron.Dockerfile, ubuntu-latest, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build AMD64 (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/cron.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/db.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/pii.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/realtime.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-8vcpu-ubuntu-2404-arm, ./docker/app.Dockerfile, linux-arm64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
Helm Chart / Lint, test, and validate chart (push) Has been cancelled
Helm Chart / Chart version bumped (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
CI / Build Dev ECR (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core) (push) Has been cancelled
CI / Promote Images (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/cron) (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 / Build Dev ECR (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Check Desktop Signing Secrets (push) Has been cancelled
CI / Desktop Release (push) Has been cancelled
CI / Create Desktop Prerelease (push) Has been cancelled
CI / Desktop Prerelease Build (push) Has been cancelled
CI / Publish Desktop Prerelease (push) Has been cancelled
CI / Prune Desktop Prereleases (push) Has been cancelled
Helm Chart / Install on kind and run helm test (push) Has been cancelled
WeHub snapshot of cb28d14c6f2c081de7a0d8729a8c816c9adef67a
2026-08-10 11:17:50 +08:00

206 lines
6.2 KiB
TypeScript

import { db, workflowSchedule } from '@sim/db'
import { createLogger } from '@sim/logger'
import { getErrorMessage } from '@sim/utils/errors'
import { generateId } from '@sim/utils/id'
import { and, eq, inArray, isNull } from 'drizzle-orm'
import type { DbOrTx } from '@/lib/db/types'
import type { BlockState } from '@/lib/workflows/schedules/utils'
import { findScheduleBlocks, validateScheduleBlock } from '@/lib/workflows/schedules/validation'
const logger = createLogger('ScheduleDeployUtils')
/**
* Result of schedule creation during deploy
*/
export interface ScheduleDeployResult {
success: boolean
error?: string
scheduleId?: string
cronExpression?: string
nextRunAt?: Date
timezone?: string
}
/**
* Create or update schedule records for a workflow during deployment.
* Atomic either way: writes run inside the caller's transaction when `tx`
* is provided, otherwise inside a transaction opened here.
*/
export async function createSchedulesForDeploy(
workflowId: string,
blocks: Record<string, BlockState>,
tx?: DbOrTx,
deploymentVersionId?: string,
deploymentOperationId?: string
): Promise<ScheduleDeployResult> {
const scheduleBlocks = findScheduleBlocks(blocks)
if (scheduleBlocks.length === 0) {
logger.info(`No schedule blocks found in workflow ${workflowId}`)
return { success: true }
}
// Phase 1: Validate ALL blocks before making any DB changes
const validatedBlocks: Array<{
blockId: string
cronExpression: string
nextRunAt: Date
timezone: string
}> = []
for (const block of scheduleBlocks) {
const blockId = block.id as string
const validation = validateScheduleBlock(block)
if (!validation.isValid) {
return {
success: false,
error: validation.error,
}
}
validatedBlocks.push({
blockId,
cronExpression: validation.cronExpression!,
nextRunAt: validation.nextRunAt!,
timezone: validation.timezone!,
})
}
// Phase 2: All validations passed - now do DB operations in a transaction
let lastScheduleInfo: {
scheduleId: string
cronExpression?: string
nextRunAt?: Date
timezone?: string
} | null = null
try {
const writeSchedules = async (trx: DbOrTx) => {
const currentBlockIds = new Set(validatedBlocks.map((b) => b.blockId))
const existingSchedules = await trx
.select({ id: workflowSchedule.id, blockId: workflowSchedule.blockId })
.from(workflowSchedule)
.where(
deploymentVersionId
? and(
eq(workflowSchedule.workflowId, workflowId),
eq(workflowSchedule.deploymentVersionId, deploymentVersionId),
isNull(workflowSchedule.archivedAt)
)
: and(eq(workflowSchedule.workflowId, workflowId), isNull(workflowSchedule.archivedAt))
)
const orphanedScheduleIds = existingSchedules
.filter((s) => s.blockId && !currentBlockIds.has(s.blockId))
.map((s) => s.id)
if (orphanedScheduleIds.length > 0) {
logger.info(
`Deleting ${orphanedScheduleIds.length} orphaned schedule(s) for workflow ${workflowId}`
)
await trx.delete(workflowSchedule).where(inArray(workflowSchedule.id, orphanedScheduleIds))
}
for (const validated of validatedBlocks) {
const { blockId, cronExpression, nextRunAt, timezone } = validated
const scheduleId = generateId()
const now = new Date()
const values = {
id: scheduleId,
workflowId,
deploymentVersionId: deploymentVersionId || null,
deploymentOperationId: deploymentOperationId || null,
blockId,
cronExpression,
triggerType: 'schedule',
createdAt: now,
updatedAt: now,
nextRunAt,
timezone,
status: 'active',
failedCount: 0,
infraRetryCount: 0,
}
const setValues = {
blockId,
cronExpression,
...(deploymentVersionId ? { deploymentVersionId } : {}),
...(deploymentOperationId ? { deploymentOperationId } : {}),
updatedAt: now,
nextRunAt,
timezone,
status: 'active',
failedCount: 0,
infraRetryCount: 0,
}
await trx
.insert(workflowSchedule)
.values(values)
.onConflictDoUpdate({
target: [
workflowSchedule.workflowId,
workflowSchedule.blockId,
workflowSchedule.deploymentVersionId,
],
targetWhere: isNull(workflowSchedule.archivedAt),
set: setValues,
})
logger.info(`Schedule created/updated for workflow ${workflowId}, block ${blockId}`, {
scheduleId: values.id,
cronExpression,
nextRunAt: nextRunAt?.toISOString(),
})
lastScheduleInfo = { scheduleId: values.id, cronExpression, nextRunAt, timezone }
}
}
// The global client is not a transaction — wrap it so the atomicity
// contract holds even if a caller passes `db` explicitly.
await (tx && tx !== db ? writeSchedules(tx) : db.transaction(writeSchedules))
} catch (error) {
logger.error(`Failed to create schedules for workflow ${workflowId}`, error)
return {
success: false,
error: getErrorMessage(error, 'Failed to create schedules'),
}
}
return {
success: true,
...(lastScheduleInfo ?? {}),
}
}
/**
* Delete all schedules for a workflow
* This should be called within a database transaction during undeploy
*/
export async function deleteSchedulesForWorkflow(
workflowId: string,
tx: DbOrTx,
deploymentVersionId?: string
): Promise<void> {
await tx
.delete(workflowSchedule)
.where(
deploymentVersionId
? and(
eq(workflowSchedule.workflowId, workflowId),
eq(workflowSchedule.deploymentVersionId, deploymentVersionId),
isNull(workflowSchedule.archivedAt)
)
: and(eq(workflowSchedule.workflowId, workflowId), isNull(workflowSchedule.archivedAt))
)
logger.info(
deploymentVersionId
? `Deleted schedules for workflow ${workflowId} deployment ${deploymentVersionId}`
: `Deleted all schedules for workflow ${workflowId}`
)
}