6df8069c0e
* WIP batch trigger v2 * Fix for the DateField being one month out… getUTCMonth() is zero indexed 🤦♂️ * Added a custom date range filter * Deal with closing the custom date range * Child runs filter * Fix for the clear button untoggling the child runs * WIP batchTriggerV2 * Finished removing rate limit from the webapp * Added an index TaskRun to make useRealtimeBatch performant * Renamed the period filter labels to be “Last X mins” * Denormalize background worker columns into TaskRun * Use the runTags column on TaskRun * Add TaskRun ("projectId", "id" DESC) index * Improved the v2 batch trigger endpoint to process items in parallel and also added a threshold, below which the processing of items is async * Added a runId filter, and WIP for batchId filter * WIP triggerAll * Add new batch methods for triggering multiple different tasks in a single batch * Disabled switch styling * Batch filtering, force child runs to show if filtering by batch/run * Added schedule ID filtering * Force child runs to show when filtering by scheduleId, for consistency * realtime: allow setting enabled: false on useApiClient * Batches page * Always complete batches, not only batchTriggerAndWait in deployed tasks * Add batch.retrieve and allow filtering by batch in runs.list * Renamed pending to “In progress” * Tidied up the table a bit * Deal with old batches: “Legacy batch” * Added the Batch to the run inspector * Fixed the migration that created the new idempotency key index on BatchTaskRun * Fixed the name of the idempotencyKeyExpiresAt option and now default idempotency key TTL is 30 days, not 24 hours * Timezone fix: wrong month in Usage page dropdown * The DateField now defaults to local time, but can be overriden to use utc with an option * Don’t allow the task icon to get squished * BatchFilters removed unused imports * In the batch filtering, use `id` instead of `batchId` in the URL * BatchFilters: we don’t need a child tasks hidden input field * Creates some common filter components/functions * Fix for batchVersion check when filtering by batch status * Add additional logging around telemetry and more attributes for trigger spans * Show clear button for specific id filters * Batch list: only allow environments that are part of this project * Unnecessary optional chain Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Add JSDocs --------- Co-authored-by: Matt Aitken <matt@mattaitken.com> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
139 lines
4.1 KiB
TypeScript
139 lines
4.1 KiB
TypeScript
import type {
|
|
TaskRunExecutionResult,
|
|
TaskRunFailedExecutionResult,
|
|
TaskRunSuccessfulExecutionResult,
|
|
} from "@trigger.dev/core/v3";
|
|
import { TaskRunError, TaskRunErrorCodes } from "@trigger.dev/core/v3";
|
|
|
|
import type {
|
|
BatchTaskRunItemStatus as BatchTaskRunItemStatusType,
|
|
TaskRun,
|
|
TaskRunAttempt,
|
|
TaskRunStatus as TaskRunStatusType,
|
|
} from "@trigger.dev/database";
|
|
|
|
import { assertNever } from "assert-never";
|
|
import { BatchTaskRunItemStatus, TaskRunAttemptStatus, TaskRunStatus } from "~/database-types";
|
|
import { logger } from "~/services/logger.server";
|
|
|
|
const SUCCESSFUL_STATUSES = [TaskRunStatus.COMPLETED_SUCCESSFULLY];
|
|
const FAILURE_STATUSES = [
|
|
TaskRunStatus.CANCELED,
|
|
TaskRunStatus.INTERRUPTED,
|
|
TaskRunStatus.COMPLETED_WITH_ERRORS,
|
|
TaskRunStatus.SYSTEM_FAILURE,
|
|
TaskRunStatus.CRASHED,
|
|
];
|
|
|
|
export type TaskRunWithAttempts = TaskRun & {
|
|
attempts: TaskRunAttempt[];
|
|
};
|
|
|
|
export function executionResultForTaskRun(
|
|
taskRun: TaskRunWithAttempts
|
|
): TaskRunExecutionResult | undefined {
|
|
if (SUCCESSFUL_STATUSES.includes(taskRun.status)) {
|
|
// find the last attempt that was successful
|
|
const attempt = taskRun.attempts.find((a) => a.status === TaskRunAttemptStatus.COMPLETED);
|
|
|
|
if (!attempt) {
|
|
logger.error("Task run is successful but no successful attempt found", {
|
|
taskRunId: taskRun.id,
|
|
taskRunStatus: taskRun.status,
|
|
taskRunAttempts: taskRun.attempts.map((a) => a.status),
|
|
});
|
|
|
|
return undefined;
|
|
}
|
|
|
|
return {
|
|
ok: true,
|
|
id: taskRun.friendlyId,
|
|
taskIdentifier: taskRun.taskIdentifier,
|
|
output: attempt.output ?? undefined,
|
|
outputType: attempt.outputType,
|
|
} satisfies TaskRunSuccessfulExecutionResult;
|
|
}
|
|
|
|
if (FAILURE_STATUSES.includes(taskRun.status)) {
|
|
if (taskRun.status === TaskRunStatus.CANCELED) {
|
|
return {
|
|
ok: false,
|
|
id: taskRun.friendlyId,
|
|
taskIdentifier: taskRun.taskIdentifier,
|
|
error: {
|
|
type: "INTERNAL_ERROR",
|
|
code: TaskRunErrorCodes.TASK_RUN_CANCELLED,
|
|
},
|
|
} satisfies TaskRunFailedExecutionResult;
|
|
}
|
|
|
|
const attempt = taskRun.attempts.find((a) => a.status === TaskRunAttemptStatus.FAILED);
|
|
|
|
if (!attempt) {
|
|
logger.error("Task run is failed but no failed attempt found", {
|
|
taskRunId: taskRun.id,
|
|
taskRunStatus: taskRun.status,
|
|
taskRunAttempts: taskRun.attempts.map((a) => a.status),
|
|
});
|
|
|
|
return undefined;
|
|
}
|
|
|
|
const error = TaskRunError.safeParse(attempt.error);
|
|
|
|
if (!error.success) {
|
|
logger.error("Failed to parse error from failed task run attempt", {
|
|
taskRunId: taskRun.id,
|
|
taskRunStatus: taskRun.status,
|
|
taskRunAttempts: taskRun.attempts.map((a) => a.status),
|
|
error: attempt.error,
|
|
});
|
|
|
|
return {
|
|
ok: false,
|
|
id: taskRun.friendlyId,
|
|
taskIdentifier: taskRun.taskIdentifier,
|
|
error: {
|
|
type: "INTERNAL_ERROR",
|
|
code: TaskRunErrorCodes.CONFIGURED_INCORRECTLY,
|
|
},
|
|
} satisfies TaskRunFailedExecutionResult;
|
|
}
|
|
|
|
return {
|
|
ok: false,
|
|
id: taskRun.friendlyId,
|
|
taskIdentifier: taskRun.taskIdentifier,
|
|
error: error.data,
|
|
} satisfies TaskRunFailedExecutionResult;
|
|
}
|
|
}
|
|
|
|
export function batchTaskRunItemStatusForRunStatus(
|
|
status: TaskRunStatusType
|
|
): BatchTaskRunItemStatusType {
|
|
switch (status) {
|
|
case TaskRunStatus.COMPLETED_SUCCESSFULLY:
|
|
return BatchTaskRunItemStatus.COMPLETED;
|
|
case TaskRunStatus.CANCELED:
|
|
case TaskRunStatus.INTERRUPTED:
|
|
case TaskRunStatus.COMPLETED_WITH_ERRORS:
|
|
case TaskRunStatus.SYSTEM_FAILURE:
|
|
case TaskRunStatus.CRASHED:
|
|
case TaskRunStatus.EXPIRED:
|
|
case TaskRunStatus.TIMED_OUT:
|
|
return BatchTaskRunItemStatus.FAILED;
|
|
case TaskRunStatus.PENDING:
|
|
case TaskRunStatus.WAITING_FOR_DEPLOY:
|
|
case TaskRunStatus.WAITING_TO_RESUME:
|
|
case TaskRunStatus.RETRYING_AFTER_FAILURE:
|
|
case TaskRunStatus.EXECUTING:
|
|
case TaskRunStatus.PAUSED:
|
|
case TaskRunStatus.DELAYED:
|
|
return BatchTaskRunItemStatus.PENDING;
|
|
default:
|
|
assertNever(status);
|
|
}
|
|
}
|