(params: {
workflowId: string
tx?: DbOrTx
onUndeployTransaction?: (tx: DbOrTx, result: { deploymentVersionIds: string[] }) => Promise<void>
})
| 936 | } |
| 937 | |
| 938 | export async function undeployWorkflow(params: { |
| 939 | workflowId: string |
| 940 | tx?: DbOrTx |
| 941 | onUndeployTransaction?: (tx: DbOrTx, result: { deploymentVersionIds: string[] }) => Promise<void> |
| 942 | }): Promise<{ |
| 943 | success: boolean |
| 944 | error?: string |
| 945 | }> { |
| 946 | const { workflowId, tx } = params |
| 947 | |
| 948 | const executeUndeploy = async (dbCtx: DbOrTx) => { |
| 949 | if (!(await lockWorkflowForUpdate(dbCtx, workflowId))) { |
| 950 | throw new Error('Workflow not found') |
| 951 | } |
| 952 | |
| 953 | const deploymentVersions = await dbCtx |
| 954 | .select({ id: workflowDeploymentVersion.id }) |
| 955 | .from(workflowDeploymentVersion) |
| 956 | .where(eq(workflowDeploymentVersion.workflowId, workflowId)) |
| 957 | const deploymentVersionIds = deploymentVersions.map((version) => version.id) |
| 958 | |
| 959 | const { deleteSchedulesForWorkflow } = await import('@/lib/workflows/schedules/deploy') |
| 960 | await deleteSchedulesForWorkflow(workflowId, dbCtx) |
| 961 | |
| 962 | await dbCtx |
| 963 | .update(workflowDeploymentVersion) |
| 964 | .set({ isActive: false }) |
| 965 | .where(eq(workflowDeploymentVersion.workflowId, workflowId)) |
| 966 | |
| 967 | await dbCtx |
| 968 | .update(workflow) |
| 969 | .set({ isDeployed: false, deployedAt: null }) |
| 970 | .where(eq(workflow.id, workflowId)) |
| 971 | |
| 972 | await params.onUndeployTransaction?.(dbCtx, { deploymentVersionIds }) |
| 973 | } |
| 974 | |
| 975 | try { |
| 976 | if (tx) { |
| 977 | await executeUndeploy(tx) |
| 978 | } else { |
| 979 | await db.transaction(async (txn) => { |
| 980 | await executeUndeploy(txn) |
| 981 | }) |
| 982 | } |
| 983 | |
| 984 | logger.info(`Undeployed workflow ${workflowId}`) |
| 985 | return { success: true } |
| 986 | } catch (error) { |
| 987 | logger.error(`Error undeploying workflow ${workflowId}:`, error) |
| 988 | return { |
| 989 | success: false, |
| 990 | error: getErrorMessage(error, 'Failed to undeploy workflow'), |
| 991 | } |
| 992 | } |
| 993 | } |
| 994 | |
| 995 | export async function activateWorkflowVersion(params: { |
no test coverage detected