(trx: DbOrTx)
| 75 | |
| 76 | try { |
| 77 | const writeSchedules = async (trx: DbOrTx) => { |
| 78 | const currentBlockIds = new Set(validatedBlocks.map((b) => b.blockId)) |
| 79 | |
| 80 | const existingSchedules = await trx |
| 81 | .select({ id: workflowSchedule.id, blockId: workflowSchedule.blockId }) |
| 82 | .from(workflowSchedule) |
| 83 | .where( |
| 84 | deploymentVersionId |
| 85 | ? and( |
| 86 | eq(workflowSchedule.workflowId, workflowId), |
| 87 | eq(workflowSchedule.deploymentVersionId, deploymentVersionId), |
| 88 | isNull(workflowSchedule.archivedAt) |
| 89 | ) |
| 90 | : and(eq(workflowSchedule.workflowId, workflowId), isNull(workflowSchedule.archivedAt)) |
| 91 | ) |
| 92 | |
| 93 | const orphanedScheduleIds = existingSchedules |
| 94 | .filter((s) => s.blockId && !currentBlockIds.has(s.blockId)) |
| 95 | .map((s) => s.id) |
| 96 | |
| 97 | if (orphanedScheduleIds.length > 0) { |
| 98 | logger.info( |
| 99 | `Deleting ${orphanedScheduleIds.length} orphaned schedule(s) for workflow ${workflowId}` |
| 100 | ) |
| 101 | await trx.delete(workflowSchedule).where(inArray(workflowSchedule.id, orphanedScheduleIds)) |
| 102 | } |
| 103 | |
| 104 | for (const validated of validatedBlocks) { |
| 105 | const { blockId, cronExpression, nextRunAt, timezone } = validated |
| 106 | const scheduleId = generateId() |
| 107 | const now = new Date() |
| 108 | |
| 109 | const values = { |
| 110 | id: scheduleId, |
| 111 | workflowId, |
| 112 | deploymentVersionId: deploymentVersionId || null, |
| 113 | blockId, |
| 114 | cronExpression, |
| 115 | triggerType: 'schedule', |
| 116 | createdAt: now, |
| 117 | updatedAt: now, |
| 118 | nextRunAt, |
| 119 | timezone, |
| 120 | status: 'active', |
| 121 | failedCount: 0, |
| 122 | infraRetryCount: 0, |
| 123 | } |
| 124 | |
| 125 | const setValues = { |
| 126 | blockId, |
| 127 | cronExpression, |
| 128 | ...(deploymentVersionId ? { deploymentVersionId } : {}), |
| 129 | updatedAt: now, |
| 130 | nextRunAt, |
| 131 | timezone, |
| 132 | status: 'active', |
| 133 | failedCount: 0, |
| 134 | infraRetryCount: 0, |
no test coverage detected