Files
Saadi Myftija 28b6be2491 feat: introduce run templates for reusing test run configs (#2253)
* Add new prisma model for task run templates

* Create run templates in a new service

* Add modal to create run templates in the test page

* Show templates list and apply values when selected

* Hide template creation time in the dropdown list, only show date

* Enable deleting run templates

* Show success toast on template creation

* Validate template label length

* Improve the template creation success indicator

* Use formAction consistently to differentiate submissions

* Type formAction for better editor support

* Prettify run template payload and metadata

* Add triggerSource, concurrencyKey and ttl to run templates
2025-07-09 19:09:25 +02:00

83 lines
2.9 KiB
TypeScript

import { stringifyIO } from "@trigger.dev/core/v3";
import { type AuthenticatedEnvironment } from "~/services/apiAuth.server";
import { type TestTaskData } from "../testTask";
import { BaseService } from "./baseService.server";
import { TriggerTaskService } from "./triggerTask.server";
export class TestTaskService extends BaseService {
public async call(environment: AuthenticatedEnvironment, data: TestTaskData) {
const triggerTaskService = new TriggerTaskService();
const { triggerSource } = data;
switch (triggerSource) {
case "STANDARD": {
const result = await triggerTaskService.call(data.taskIdentifier, environment, {
payload: data.payload,
options: {
test: true,
metadata: data.metadata,
delay: data.delaySeconds ? new Date(Date.now() + data.delaySeconds * 1000) : undefined,
ttl: data.ttlSeconds,
idempotencyKey: data.idempotencyKey,
idempotencyKeyTTL: data.idempotencyKeyTTLSeconds
? `${data.idempotencyKeyTTLSeconds}s`
: undefined,
queue: data.queue ? { name: data.queue } : undefined,
concurrencyKey: data.concurrencyKey,
maxAttempts: data.maxAttempts,
maxDuration: data.maxDurationSeconds,
tags: data.tags,
machine: data.machine,
lockToVersion: data.version === "latest" ? undefined : data.version,
},
});
return result?.run;
}
case "SCHEDULED": {
const payload = {
scheduleId: "sched_1234",
type: "IMPERATIVE",
timestamp: data.timestamp,
lastTimestamp: data.lastTimestamp,
timezone: data.timezone,
externalId: data.externalId,
upcoming: [],
};
const payloadPacket = await stringifyIO(payload);
const result = await triggerTaskService.call(
data.taskIdentifier,
environment,
{
payload: payloadPacket.data,
options: {
payloadType: payloadPacket.dataType,
test: true,
ttl: data.ttlSeconds,
idempotencyKey: data.idempotencyKey,
idempotencyKeyTTL: data.idempotencyKeyTTLSeconds
? `${data.idempotencyKeyTTLSeconds}s`
: undefined,
queue: data.queue ? { name: data.queue } : undefined,
concurrencyKey: data.concurrencyKey,
maxAttempts: data.maxAttempts,
maxDuration: data.maxDurationSeconds,
tags: data.tags,
machine: data.machine,
lockToVersion: data.version === "latest" ? undefined : data.version,
},
},
{ customIcon: "scheduled" }
);
return result?.run;
}
default: {
triggerSource satisfies never;
throw new Error("Invalid trigger source");
}
}
}
}