|
| 1 | +import { BulkActionId } from "@trigger.dev/core/v3/isomorphic"; |
| 2 | +import { |
| 3 | + BulkActionNotificationType, |
| 4 | + BulkActionType, |
| 5 | + Prisma, |
| 6 | + type PrismaClient, |
| 7 | + type TaskRunStatus, |
| 8 | +} from "@trigger.dev/database"; |
| 9 | +import { QUEUED_STATUSES, RUNNING_STATUSES } from "~/components/runs/v3/TaskRunStatus"; |
| 10 | +import { prisma } from "~/db.server"; |
| 11 | +import type { RunsRepository } from "~/services/runsRepository/runsRepository.server"; |
| 12 | +import { commonWorker } from "~/v3/commonWorker.server"; |
| 13 | +import { |
| 14 | + countInProgressRunsForBillableEnvironment, |
| 15 | + countQueuedRunsForBillableEnvironment, |
| 16 | + createBillingLimitRunsRepository, |
| 17 | + getBillableEnvironmentsForBillingLimit, |
| 18 | +} from "./billingLimitQueuedRuns.server"; |
| 19 | + |
| 20 | +export const BILLING_LIMIT_RESOLVE_CANCEL_SOURCE = "billing_limit_resolve_new_only"; |
| 21 | +export const BILLING_LIMIT_IN_PROGRESS_CANCEL_SOURCE = "billing_limit_in_progress"; |
| 22 | + |
| 23 | +type BulkCancelSource = |
| 24 | + | typeof BILLING_LIMIT_RESOLVE_CANCEL_SOURCE |
| 25 | + | typeof BILLING_LIMIT_IN_PROGRESS_CANCEL_SOURCE; |
| 26 | + |
| 27 | +export type BillingLimitBulkCancelDeps = { |
| 28 | + prismaClient?: PrismaClient; |
| 29 | + createRunsRepository?: (organizationId: string) => Promise<RunsRepository>; |
| 30 | + enqueueProcessBulkAction?: (bulkActionId: string) => Promise<unknown>; |
| 31 | +}; |
| 32 | + |
| 33 | +function resolveBulkCancelDeps(deps?: BillingLimitBulkCancelDeps) { |
| 34 | + return { |
| 35 | + prismaClient: deps?.prismaClient ?? prisma, |
| 36 | + createRunsRepository: deps?.createRunsRepository ?? createBillingLimitRunsRepository, |
| 37 | + enqueueProcessBulkAction: |
| 38 | + deps?.enqueueProcessBulkAction ?? |
| 39 | + (async (bulkActionId: string) => { |
| 40 | + await commonWorker.enqueue({ |
| 41 | + id: `processBulkAction-${bulkActionId}`, |
| 42 | + job: "processBulkAction", |
| 43 | + payload: { bulkActionId }, |
| 44 | + }); |
| 45 | + }), |
| 46 | + }; |
| 47 | +} |
| 48 | + |
| 49 | +export class BillingLimitBulkCancelService { |
| 50 | + static async cancelQueuedRuns( |
| 51 | + organizationId: string, |
| 52 | + options?: { dedupeKey?: string }, |
| 53 | + deps?: BillingLimitBulkCancelDeps |
| 54 | + ): Promise<{ bulkActionIds: string[] }> { |
| 55 | + return this.cancelRunsForBillableEnvironments( |
| 56 | + organizationId, |
| 57 | + { |
| 58 | + source: BILLING_LIMIT_RESOLVE_CANCEL_SOURCE, |
| 59 | + statuses: [...QUEUED_STATUSES], |
| 60 | + name: "Billing limit resolve — cancel queued runs", |
| 61 | + countRuns: countQueuedRunsForBillableEnvironment, |
| 62 | + dedupeKey: options?.dedupeKey, |
| 63 | + }, |
| 64 | + deps |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + static async cancelInProgressRuns( |
| 69 | + organizationId: string, |
| 70 | + options: { hitAt: string }, |
| 71 | + deps?: BillingLimitBulkCancelDeps |
| 72 | + ): Promise<{ bulkActionIds: string[] }> { |
| 73 | + return this.cancelRunsForBillableEnvironments( |
| 74 | + organizationId, |
| 75 | + { |
| 76 | + source: BILLING_LIMIT_IN_PROGRESS_CANCEL_SOURCE, |
| 77 | + statuses: [...RUNNING_STATUSES], |
| 78 | + name: "Billing limit hit — cancel in-progress runs", |
| 79 | + countRuns: countInProgressRunsForBillableEnvironment, |
| 80 | + dedupeKey: options.hitAt, |
| 81 | + }, |
| 82 | + deps |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + private static async cancelRunsForBillableEnvironments( |
| 87 | + organizationId: string, |
| 88 | + options: { |
| 89 | + source: BulkCancelSource; |
| 90 | + statuses: TaskRunStatus[]; |
| 91 | + name: string; |
| 92 | + countRuns: typeof countQueuedRunsForBillableEnvironment; |
| 93 | + dedupeKey?: string; |
| 94 | + }, |
| 95 | + deps?: BillingLimitBulkCancelDeps |
| 96 | + ): Promise<{ bulkActionIds: string[] }> { |
| 97 | + const { prismaClient, createRunsRepository, enqueueProcessBulkAction } = |
| 98 | + resolveBulkCancelDeps(deps); |
| 99 | + |
| 100 | + const environments = await getBillableEnvironmentsForBillingLimit( |
| 101 | + organizationId, |
| 102 | + prismaClient |
| 103 | + ); |
| 104 | + |
| 105 | + if (environments.length === 0) { |
| 106 | + return { bulkActionIds: [] }; |
| 107 | + } |
| 108 | + |
| 109 | + const runsRepository = await createRunsRepository(organizationId); |
| 110 | + const bulkActionIds: string[] = []; |
| 111 | + |
| 112 | + for (const environment of environments) { |
| 113 | + if (options.dedupeKey) { |
| 114 | + const existing = await prismaClient.bulkActionGroup.findFirst({ |
| 115 | + where: { |
| 116 | + environmentId: environment.id, |
| 117 | + type: BulkActionType.CANCEL, |
| 118 | + AND: [ |
| 119 | + { |
| 120 | + params: { |
| 121 | + path: ["source"], |
| 122 | + equals: options.source, |
| 123 | + }, |
| 124 | + }, |
| 125 | + { |
| 126 | + params: { |
| 127 | + path: ["dedupeKey"], |
| 128 | + equals: options.dedupeKey, |
| 129 | + }, |
| 130 | + }, |
| 131 | + ], |
| 132 | + }, |
| 133 | + select: { friendlyId: true }, |
| 134 | + }); |
| 135 | + |
| 136 | + if (existing) { |
| 137 | + bulkActionIds.push(existing.friendlyId); |
| 138 | + continue; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + const count = await options.countRuns(runsRepository, organizationId, environment); |
| 143 | + |
| 144 | + if (count === 0) { |
| 145 | + continue; |
| 146 | + } |
| 147 | + |
| 148 | + const { id, friendlyId } = BulkActionId.generate(); |
| 149 | + |
| 150 | + await prismaClient.bulkActionGroup.create({ |
| 151 | + data: { |
| 152 | + id, |
| 153 | + friendlyId, |
| 154 | + projectId: environment.projectId, |
| 155 | + environmentId: environment.id, |
| 156 | + name: options.name, |
| 157 | + type: BulkActionType.CANCEL, |
| 158 | + params: { |
| 159 | + statuses: options.statuses, |
| 160 | + finalizeRun: true, |
| 161 | + source: options.source, |
| 162 | + ...(options.dedupeKey ? { dedupeKey: options.dedupeKey } : {}), |
| 163 | + } as Prisma.InputJsonValue, |
| 164 | + queryName: "bulk_action_v1", |
| 165 | + totalCount: count, |
| 166 | + completionNotification: BulkActionNotificationType.NONE, |
| 167 | + }, |
| 168 | + }); |
| 169 | + |
| 170 | + await enqueueProcessBulkAction(id); |
| 171 | + |
| 172 | + bulkActionIds.push(friendlyId); |
| 173 | + } |
| 174 | + |
| 175 | + return { bulkActionIds }; |
| 176 | + } |
| 177 | +} |
0 commit comments