Skip to content

Require max duration in trigger.config. Validate TRIGGER_ACCESS_TOKEN is a PAT. #1620

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/tiny-seals-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"trigger.dev": patch
"@trigger.dev/core": patch
---

Require maxDuration config and have a better error for bad CI tokens
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export default defineConfig({
build: {
extensions: [emitDecoratorMetadata()],
},
maxDuration: 3600,
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export default defineConfig({
build: {
external: ["mupdf"],
},
maxDuration: 3600,
});
1 change: 1 addition & 0 deletions packages/cli-v3/e2e/fixtures/hello-world/trigger.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ import { defineConfig } from "@trigger.dev/sdk/v3";
export default defineConfig({
project: "<fixture project>",
dirs: ["./src/trigger"],
maxDuration: 3600,
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ import { defineConfig } from "@trigger.dev/sdk/v3";
export default defineConfig({
project: "<fixture project>",
dirs: ["./src"],
maxDuration: 3600,
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export default defineConfig({
project: "<fixture project>",
dirs: ["./src/trigger"],
instrumentations: [new OpenAIInstrumentation()],
maxDuration: 3600,
});
14 changes: 14 additions & 0 deletions packages/cli-v3/src/commands/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ import { spinner } from "../utilities/windows.js";
import { isLinuxServer } from "../utilities/linux.js";
import { VERSION } from "../version.js";
import { env } from "std-env";
import {
isPersonalAccessToken,
NotPersonalAccessTokenError,
} from "../utilities/isPersonalAccessToken.js";

export const LoginCommandOptions = CommonCommandOptions.extend({
apiUrl: z.string(),
Expand Down Expand Up @@ -84,6 +88,12 @@ export async function login(options?: LoginOptions): Promise<LoginResult> {
const accessTokenFromEnv = env.TRIGGER_ACCESS_TOKEN;

if (accessTokenFromEnv) {
if (!isPersonalAccessToken(accessTokenFromEnv)) {
throw new NotPersonalAccessTokenError(
"Your TRIGGER_ACCESS_TOKEN is not a Personal Access Token, they start with 'tr_pat_'. You can generate one here: https://cloud.trigger.dev/account/tokens"
);
}

const auth = {
accessToken: accessTokenFromEnv,
apiUrl: env.TRIGGER_API_URL ?? opts.defaultApiUrl ?? "https://api.trigger.dev",
Expand Down Expand Up @@ -292,6 +302,10 @@ export async function login(options?: LoginOptions): Promise<LoginResult> {
span.end();

if (options?.embedded) {
if (e instanceof NotPersonalAccessTokenError) {
throw e;
}

return {
ok: false as const,
error: e instanceof Error ? e.message : String(e),
Expand Down
6 changes: 6 additions & 0 deletions packages/cli-v3/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,12 @@ function validateConfig(config: TriggerConfig, warn = true) {
config.build.extensions.push(adaptResolveEnvVarsToSyncEnvVarsExtension(resolveEnvVarsFn));
}

if (!config.maxDuration) {
throw new Error(
`The "maxDuration" trigger.config option is now required, and must be at least 5 seconds.`
);
}
Comment on lines +292 to +296
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add validation for the minimum maxDuration value.

While the validation ensures maxDuration is present, it should also verify that the value is at least 5 seconds as mentioned in the error message and documentation.

 if (!config.maxDuration) {
   throw new Error(
     `The "maxDuration" trigger.config option is now required, and must be at least 5 seconds.`
   );
 }
+if (config.maxDuration < 5) {
+  throw new Error(
+    `The "maxDuration" trigger.config option must be at least 5 seconds, got ${config.maxDuration}.`
+  );
+}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (!config.maxDuration) {
throw new Error(
`The "maxDuration" trigger.config option is now required, and must be at least 5 seconds.`
);
}
if (!config.maxDuration) {
throw new Error(
`The "maxDuration" trigger.config option is now required, and must be at least 5 seconds.`
);
}
if (config.maxDuration < 5) {
throw new Error(
`The "maxDuration" trigger.config option must be at least 5 seconds, got ${config.maxDuration}.`
);
}


if (config.runtime && config.runtime === "bun") {
warn &&
prettyWarning(
Expand Down
12 changes: 12 additions & 0 deletions packages/cli-v3/src/utilities/isPersonalAccessToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const tokenPrefix = "tr_pat_";

export function isPersonalAccessToken(token: string) {
return token.startsWith(tokenPrefix);
}

export class NotPersonalAccessTokenError extends Error {
constructor(message: string) {
super(message);
this.name = "NotPersonalAccessTokenError";
}
}
6 changes: 4 additions & 2 deletions packages/cli-v3/templates/trigger.config.mjs.template
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ export default defineConfig({
project: "${projectRef}",
runtime: "${runtime}",
logLevel: "log",
// Set the maxDuration to 300 seconds for all tasks. See https://trigger.dev/docs/runs/max-duration
// maxDuration: 300,
// The max compute seconds a task is allowed to run. If the task run exceeds this duration, it will be stopped.
// You can override this on an individual task.
// See https://trigger.dev/docs/runs/max-duration
maxDuration: 3600,
retries: {
enabledInDev: true,
default: {
Expand Down
6 changes: 4 additions & 2 deletions packages/cli-v3/templates/trigger.config.ts.template
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ export default defineConfig({
project: "${projectRef}",
runtime: "${runtime}",
logLevel: "log",
// Set the maxDuration to 300 seconds for all tasks. See https://trigger.dev/docs/runs/max-duration
// maxDuration: 300,
// The max compute seconds a task is allowed to run. If the task run exceeds this duration, it will be stopped.
// You can override this on an individual task.
// See https://trigger.dev/docs/runs/max-duration
maxDuration: 3600,
retries: {
enabledInDev: true,
default: {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/v3/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export type TriggerConfig = {
*
* @see https://trigger.dev/docs/tasks/overview#maxduration-option
*/
maxDuration?: number;
maxDuration: number;

/**
* Enable console logging while running the dev CLI. This will print out logs from console.log, console.warn, and console.error. By default all logs are sent to the trigger.dev backend, and not logged to the console.
Expand Down
1 change: 1 addition & 0 deletions references/bun-catalog/trigger.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { defineConfig } from "@trigger.dev/sdk/v3";
export default defineConfig({
runtime: "bun",
project: "proj_uxbxncnbsyamyxeqtucu",
maxDuration: 3600,
machine: "small-2x",
retries: {
enabledInDev: true,
Expand Down
1 change: 1 addition & 0 deletions references/nextjs-realtime/trigger.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ export default defineConfig({
build: {
extensions: [rscExtension({ reactDomEnvironment: "worker" })],
},
maxDuration: 3600,
});
3 changes: 1 addition & 2 deletions references/v3-catalog/trigger.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ export default defineConfig({
machine: "medium-1x",
instrumentations: [new OpenAIInstrumentation()],
additionalFiles: ["wrangler/wrangler.toml"],
// Set the maxDuration to 300s for all tasks. See https://trigger.dev/docs/runs/max-duration
// maxDuration: 300,
maxDuration: 3600,
retries: {
enabledInDev: true,
default: {
Expand Down
Loading