|
| 1 | +import { Command } from "commander"; |
| 2 | +import { z } from "zod"; |
| 3 | +import { |
| 4 | + CommonCommandOptions, |
| 5 | + commonOptions, |
| 6 | + handleTelemetry, |
| 7 | + wrapCommandAction, |
| 8 | +} from "../../cli/common.js"; |
| 9 | +import { printInitialBanner } from "../../utilities/initialBanner.js"; |
| 10 | +import { isLoggedIn } from "../../utilities/session.js"; |
| 11 | +import { loadConfig } from "../../config.js"; |
| 12 | +import { resolveLocalEnvVars } from "../../utilities/localEnvVars.js"; |
| 13 | +import { CliApiClient } from "../../apiClient.js"; |
| 14 | +import { intro, outro } from "@clack/prompts"; |
| 15 | +import { spinner } from "../../utilities/windows.js"; |
| 16 | +import { logger } from "../../utilities/logger.js"; |
| 17 | +import { tryCatch } from "@trigger.dev/core"; |
| 18 | + |
| 19 | +// --- dlq list --- |
| 20 | + |
| 21 | +const DlqListOptions = CommonCommandOptions.extend({ |
| 22 | + config: z.string().optional(), |
| 23 | + projectRef: z.string().optional(), |
| 24 | + envFile: z.string().optional(), |
| 25 | + eventType: z.string().optional(), |
| 26 | + status: z.string().optional(), |
| 27 | + limit: z.coerce.number().int().optional(), |
| 28 | + cursor: z.string().optional(), |
| 29 | +}); |
| 30 | + |
| 31 | +type DlqListOptions = z.infer<typeof DlqListOptions>; |
| 32 | + |
| 33 | +function configureDlqListCommand(program: Command) { |
| 34 | + return commonOptions( |
| 35 | + program |
| 36 | + .command("list") |
| 37 | + .description("List dead letter queue entries") |
| 38 | + .option("-c, --config <config file>", "The name of the config file") |
| 39 | + .option("-p, --project-ref <project ref>", "The project ref") |
| 40 | + .option("--env-file <env file>", "Path to the .env file") |
| 41 | + .option("--event-type <type>", "Filter by event type") |
| 42 | + .option("--status <status>", "Filter by status (PENDING, RETRIED, DISCARDED)") |
| 43 | + .option("--limit <n>", "Max results (default 50, max 200)") |
| 44 | + .option("--cursor <cursor>", "Pagination cursor from previous response") |
| 45 | + ).action(async (options) => { |
| 46 | + await handleTelemetry(async () => { |
| 47 | + await printInitialBanner(false, options.profile); |
| 48 | + await dlqListCommand(options); |
| 49 | + }); |
| 50 | + }); |
| 51 | +} |
| 52 | + |
| 53 | +async function dlqListCommand(options: unknown) { |
| 54 | + return await wrapCommandAction("dlqListCommand", DlqListOptions, options, async (opts) => { |
| 55 | + return await _dlqListCommand(opts); |
| 56 | + }); |
| 57 | +} |
| 58 | + |
| 59 | +async function _dlqListCommand(options: DlqListOptions) { |
| 60 | + intro("Dead letter queue"); |
| 61 | + |
| 62 | + const envVars = resolveLocalEnvVars(options.envFile); |
| 63 | + |
| 64 | + const authentication = await isLoggedIn(options.profile); |
| 65 | + if (!authentication.ok) { |
| 66 | + outro(`Not logged in. Use \`trigger login\` first.`); |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + const [configError, resolvedConfig] = await tryCatch( |
| 71 | + loadConfig({ |
| 72 | + overrides: { project: options.projectRef ?? envVars.TRIGGER_PROJECT_REF }, |
| 73 | + configFile: options.config, |
| 74 | + warn: false, |
| 75 | + }) |
| 76 | + ); |
| 77 | + |
| 78 | + if (configError || !resolvedConfig?.project) { |
| 79 | + outro("Could not resolve project. Use --project-ref or configure trigger.config.ts."); |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + const loadingSpinner = spinner(); |
| 84 | + loadingSpinner.start("Fetching DLQ entries..."); |
| 85 | + |
| 86 | + const apiClient = new CliApiClient(authentication.auth.apiUrl, authentication.auth.accessToken); |
| 87 | + const result = await apiClient.listDeadLetterEvents(resolvedConfig.project, { |
| 88 | + eventType: options.eventType, |
| 89 | + status: options.status, |
| 90 | + limit: options.limit, |
| 91 | + cursor: options.cursor, |
| 92 | + }); |
| 93 | + |
| 94 | + if (!result.success) { |
| 95 | + loadingSpinner.stop("Failed to fetch DLQ entries"); |
| 96 | + logger.error(result.error); |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + const { data, pagination } = result.data; |
| 101 | + loadingSpinner.stop(`Found ${data.length} DLQ entry/entries`); |
| 102 | + |
| 103 | + if (data.length === 0) { |
| 104 | + outro("No dead letter entries found."); |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + logger.table( |
| 109 | + data.map((entry) => ({ |
| 110 | + id: entry.friendlyId, |
| 111 | + eventType: entry.eventType, |
| 112 | + task: entry.taskSlug, |
| 113 | + status: entry.status, |
| 114 | + attempts: String(entry.attemptCount), |
| 115 | + created: entry.createdAt, |
| 116 | + })) |
| 117 | + ); |
| 118 | + |
| 119 | + if (pagination.hasMore && pagination.cursor) { |
| 120 | + logger.info(`\nMore results available. Use --cursor ${pagination.cursor} to see next page.`); |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +// --- dlq retry --- |
| 125 | + |
| 126 | +const DlqRetryOptions = CommonCommandOptions.extend({ |
| 127 | + config: z.string().optional(), |
| 128 | + projectRef: z.string().optional(), |
| 129 | + envFile: z.string().optional(), |
| 130 | +}); |
| 131 | + |
| 132 | +type DlqRetryOptions = z.infer<typeof DlqRetryOptions>; |
| 133 | + |
| 134 | +function configureDlqRetryCommand(program: Command) { |
| 135 | + return commonOptions( |
| 136 | + program |
| 137 | + .command("retry <id>") |
| 138 | + .description("Retry a dead letter queue entry") |
| 139 | + .option("-c, --config <config file>", "The name of the config file") |
| 140 | + .option("-p, --project-ref <project ref>", "The project ref") |
| 141 | + .option("--env-file <env file>", "Path to the .env file") |
| 142 | + ).action(async (id: string, options) => { |
| 143 | + await handleTelemetry(async () => { |
| 144 | + await printInitialBanner(false, options.profile); |
| 145 | + await dlqRetryCommand({ ...options, id }); |
| 146 | + }); |
| 147 | + }); |
| 148 | +} |
| 149 | + |
| 150 | +const DlqRetryCommandInput = DlqRetryOptions.extend({ |
| 151 | + id: z.string(), |
| 152 | +}); |
| 153 | + |
| 154 | +type DlqRetryCommandInput = z.infer<typeof DlqRetryCommandInput>; |
| 155 | + |
| 156 | +async function dlqRetryCommand(options: unknown) { |
| 157 | + return await wrapCommandAction( |
| 158 | + "dlqRetryCommand", |
| 159 | + DlqRetryCommandInput, |
| 160 | + options, |
| 161 | + async (opts) => { |
| 162 | + return await _dlqRetryCommand(opts); |
| 163 | + } |
| 164 | + ); |
| 165 | +} |
| 166 | + |
| 167 | +async function _dlqRetryCommand(options: DlqRetryCommandInput) { |
| 168 | + intro(`Retrying DLQ entry "${options.id}"`); |
| 169 | + |
| 170 | + const envVars = resolveLocalEnvVars(options.envFile); |
| 171 | + |
| 172 | + const authentication = await isLoggedIn(options.profile); |
| 173 | + if (!authentication.ok) { |
| 174 | + outro(`Not logged in. Use \`trigger login\` first.`); |
| 175 | + return; |
| 176 | + } |
| 177 | + |
| 178 | + const [configError, resolvedConfig] = await tryCatch( |
| 179 | + loadConfig({ |
| 180 | + overrides: { project: options.projectRef ?? envVars.TRIGGER_PROJECT_REF }, |
| 181 | + configFile: options.config, |
| 182 | + warn: false, |
| 183 | + }) |
| 184 | + ); |
| 185 | + |
| 186 | + if (configError || !resolvedConfig?.project) { |
| 187 | + outro("Could not resolve project. Use --project-ref or configure trigger.config.ts."); |
| 188 | + return; |
| 189 | + } |
| 190 | + |
| 191 | + const loadingSpinner = spinner(); |
| 192 | + loadingSpinner.start("Retrying dead letter event..."); |
| 193 | + |
| 194 | + const apiClient = new CliApiClient(authentication.auth.apiUrl, authentication.auth.accessToken); |
| 195 | + const result = await apiClient.retryDeadLetterEvent(resolvedConfig.project, options.id); |
| 196 | + |
| 197 | + if (!result.success) { |
| 198 | + loadingSpinner.stop("Failed to retry DLQ entry"); |
| 199 | + logger.error(result.error); |
| 200 | + return; |
| 201 | + } |
| 202 | + |
| 203 | + loadingSpinner.stop("DLQ entry retried successfully"); |
| 204 | + |
| 205 | + logger.info(`Status: ${result.data.status}`); |
| 206 | + if (result.data.runId) { |
| 207 | + logger.info(`New run ID: ${result.data.runId}`); |
| 208 | + } |
| 209 | +} |
| 210 | + |
| 211 | +// --- Main export --- |
| 212 | + |
| 213 | +export function configureEventsDlqCommand(program: Command) { |
| 214 | + const dlq = program.command("dlq").description("Manage the dead letter queue"); |
| 215 | + |
| 216 | + configureDlqListCommand(dlq); |
| 217 | + configureDlqRetryCommand(dlq); |
| 218 | + |
| 219 | + return dlq; |
| 220 | +} |
0 commit comments