|
| 1 | +import { Logger } from "@trigger.dev/core/logger"; |
| 2 | +import { Worker as RedisWorker } from "@trigger.dev/redis-worker"; |
| 3 | +import { z } from "zod"; |
| 4 | +import { env } from "~/env.server"; |
| 5 | +import { logger } from "~/services/logger.server"; |
| 6 | +import { singleton } from "~/utils/singleton"; |
| 7 | +import { BillingLimitConvergeEnvironmentsService } from "./services/billingLimit/billingLimitConvergeEnvironmentsService.server"; |
| 8 | +import type { BillingLimitConvergeTargetState } from "./services/billingLimit/billingLimitConstants"; |
| 9 | +import { buildBillingLimitInProgressCancelJobId } from "./services/billingLimit/billingLimitConstants"; |
| 10 | +import { runBillingLimitCancelInProgressRuns } from "./services/billingLimit/billingLimitCancelInProgressRuns.server"; |
| 11 | + |
| 12 | +function initializeWorker() { |
| 13 | + const redisOptions = { |
| 14 | + keyPrefix: "billing-limit:worker:", |
| 15 | + host: env.BILLING_LIMIT_WORKER_REDIS_HOST, |
| 16 | + port: env.BILLING_LIMIT_WORKER_REDIS_PORT, |
| 17 | + username: env.BILLING_LIMIT_WORKER_REDIS_USERNAME, |
| 18 | + password: env.BILLING_LIMIT_WORKER_REDIS_PASSWORD, |
| 19 | + enableAutoPipelining: true, |
| 20 | + ...(env.BILLING_LIMIT_WORKER_REDIS_TLS_DISABLED === "true" ? {} : { tls: {} }), |
| 21 | + }; |
| 22 | + |
| 23 | + logger.debug(`👨🏭 Initializing billing limit worker at host ${env.BILLING_LIMIT_WORKER_REDIS_HOST}`); |
| 24 | + |
| 25 | + const worker = new RedisWorker({ |
| 26 | + name: "billing-limit-worker", |
| 27 | + redisOptions, |
| 28 | + catalog: { |
| 29 | + "billingLimit.convergeEnvironments": { |
| 30 | + schema: z.object({ |
| 31 | + organizationId: z.string(), |
| 32 | + targetState: z.enum(["grace", "rejected", "ok"]), |
| 33 | + }), |
| 34 | + visibilityTimeoutMs: 60_000 * 10, |
| 35 | + retry: { |
| 36 | + maxAttempts: 5, |
| 37 | + }, |
| 38 | + }, |
| 39 | + "billingLimit.reconcileTick": { |
| 40 | + schema: z.object({}), |
| 41 | + visibilityTimeoutMs: 60_000 * 5, |
| 42 | + retry: { |
| 43 | + maxAttempts: 3, |
| 44 | + }, |
| 45 | + }, |
| 46 | + "billingLimit.cancelInProgressRuns": { |
| 47 | + schema: z.object({ |
| 48 | + organizationId: z.string(), |
| 49 | + hitAt: z.string(), |
| 50 | + }), |
| 51 | + visibilityTimeoutMs: 60_000 * 10, |
| 52 | + retry: { |
| 53 | + maxAttempts: 5, |
| 54 | + }, |
| 55 | + }, |
| 56 | + }, |
| 57 | + concurrency: { |
| 58 | + workers: env.BILLING_LIMIT_WORKER_CONCURRENCY_WORKERS, |
| 59 | + tasksPerWorker: env.BILLING_LIMIT_WORKER_CONCURRENCY_TASKS_PER_WORKER, |
| 60 | + limit: env.BILLING_LIMIT_WORKER_CONCURRENCY_LIMIT, |
| 61 | + }, |
| 62 | + pollIntervalMs: env.BILLING_LIMIT_WORKER_POLL_INTERVAL, |
| 63 | + immediatePollIntervalMs: env.BILLING_LIMIT_WORKER_IMMEDIATE_POLL_INTERVAL, |
| 64 | + shutdownTimeoutMs: env.BILLING_LIMIT_WORKER_SHUTDOWN_TIMEOUT_MS, |
| 65 | + logger: new Logger("BillingLimitWorker", env.BILLING_LIMIT_WORKER_LOG_LEVEL), |
| 66 | + jobs: { |
| 67 | + "billingLimit.convergeEnvironments": async ({ payload }) => { |
| 68 | + await BillingLimitConvergeEnvironmentsService.runConverge(payload); |
| 69 | + }, |
| 70 | + "billingLimit.reconcileTick": async () => { |
| 71 | + await BillingLimitConvergeEnvironmentsService.runReconcileTick(); |
| 72 | + await scheduleBillingLimitReconcileTick(worker); |
| 73 | + }, |
| 74 | + "billingLimit.cancelInProgressRuns": async ({ payload }) => { |
| 75 | + await runBillingLimitCancelInProgressRuns(payload.organizationId, payload.hitAt); |
| 76 | + }, |
| 77 | + }, |
| 78 | + }); |
| 79 | + |
| 80 | + if (env.BILLING_LIMIT_WORKER_ENABLED === "true") { |
| 81 | + logger.debug( |
| 82 | + `👨🏭 Starting billing limit worker at host ${env.BILLING_LIMIT_WORKER_REDIS_HOST}, reconcileIntervalMs = ${env.BILLING_LIMIT_RECONCILE_INTERVAL_MS}` |
| 83 | + ); |
| 84 | + worker.start(); |
| 85 | + void scheduleBillingLimitReconcileTick(worker); |
| 86 | + } |
| 87 | + |
| 88 | + return worker; |
| 89 | +} |
| 90 | + |
| 91 | +async function scheduleBillingLimitReconcileTick(worker: ReturnType<typeof initializeWorker>) { |
| 92 | + await worker.enqueue({ |
| 93 | + id: "billingLimit.reconcileTick", |
| 94 | + job: "billingLimit.reconcileTick", |
| 95 | + payload: {}, |
| 96 | + availableAt: new Date(Date.now() + env.BILLING_LIMIT_RECONCILE_INTERVAL_MS), |
| 97 | + }); |
| 98 | +} |
| 99 | + |
| 100 | +export const billingLimitWorker = singleton("billingLimitWorker", initializeWorker); |
| 101 | + |
| 102 | +export async function enqueueBillingLimitConverge( |
| 103 | + organizationId: string, |
| 104 | + targetState: BillingLimitConvergeTargetState |
| 105 | +) { |
| 106 | + return billingLimitWorker.enqueue({ |
| 107 | + id: `billingLimit.converge:${organizationId}:${targetState}`, |
| 108 | + job: "billingLimit.convergeEnvironments", |
| 109 | + payload: { organizationId, targetState }, |
| 110 | + }); |
| 111 | +} |
| 112 | + |
| 113 | +export async function enqueueBillingLimitCancelInProgressRuns( |
| 114 | + organizationId: string, |
| 115 | + hitAt: string |
| 116 | +) { |
| 117 | + return billingLimitWorker.enqueue({ |
| 118 | + id: buildBillingLimitInProgressCancelJobId(organizationId, hitAt), |
| 119 | + job: "billingLimit.cancelInProgressRuns", |
| 120 | + payload: { organizationId, hitAt }, |
| 121 | + }); |
| 122 | +} |
0 commit comments