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,234 @@
|
||||
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 { cleanupWebhooksForWorkflow } from '@/lib/webhooks/deploy'
|
||||
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
|
||||
): 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,
|
||||
blockId,
|
||||
cronExpression,
|
||||
triggerType: 'schedule',
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
nextRunAt,
|
||||
timezone,
|
||||
status: 'active',
|
||||
failedCount: 0,
|
||||
infraRetryCount: 0,
|
||||
}
|
||||
|
||||
const setValues = {
|
||||
blockId,
|
||||
cronExpression,
|
||||
...(deploymentVersionId ? { deploymentVersionId } : {}),
|
||||
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}`
|
||||
)
|
||||
}
|
||||
|
||||
async function cleanupDeploymentVersion(params: {
|
||||
workflowId: string
|
||||
workflow: Record<string, unknown>
|
||||
requestId: string
|
||||
deploymentVersionId: string
|
||||
/**
|
||||
* If true, skip external subscription cleanup (already done by saveTriggerWebhooksForDeploy).
|
||||
* Only deletes DB records.
|
||||
*/
|
||||
skipExternalCleanup?: boolean
|
||||
strictExternalCleanup?: boolean
|
||||
}): Promise<void> {
|
||||
const {
|
||||
workflowId,
|
||||
workflow,
|
||||
requestId,
|
||||
deploymentVersionId,
|
||||
skipExternalCleanup = false,
|
||||
strictExternalCleanup = false,
|
||||
} = params
|
||||
await cleanupWebhooksForWorkflow(
|
||||
workflowId,
|
||||
workflow,
|
||||
requestId,
|
||||
deploymentVersionId,
|
||||
skipExternalCleanup,
|
||||
strictExternalCleanup
|
||||
)
|
||||
await deleteSchedulesForWorkflow(workflowId, db, deploymentVersionId)
|
||||
}
|
||||
Reference in New Issue
Block a user