From f0e2809d0ab3fcff058a38c6f480ad927338f5cc Mon Sep 17 00:00:00 2001 From: James Ritchie Date: Thu, 26 Oct 2023 13:56:17 +0100 Subject: [PATCH 01/10] Card hover state is now grey --- apps/webapp/app/components/run/RunCard.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/webapp/app/components/run/RunCard.tsx b/apps/webapp/app/components/run/RunCard.tsx index 1731622c21..e3bc1e998c 100644 --- a/apps/webapp/app/components/run/RunCard.tsx +++ b/apps/webapp/app/components/run/RunCard.tsx @@ -36,7 +36,7 @@ export function RunPanel({ ? "border-slate-850" : "border-slate-900", onClick && "cursor-pointer", - onClick && !selected && "hover:border-green-500/30", + onClick && !selected && "hover:border-slate-500/30", className )} onClick={() => onClick && onClick()} From 2178697fc73eccef88ef1b9f399d01380043f4e2 Mon Sep 17 00:00:00 2001 From: James Ritchie Date: Thu, 26 Oct 2023 14:00:26 +0100 Subject: [PATCH 02/10] =?UTF-8?q?Added=20a=20callout=20warning=20if=20you?= =?UTF-8?q?=20don=E2=80=99t=20have=20any=20tasks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/webapp/app/components/run/RunOverview.tsx | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/apps/webapp/app/components/run/RunOverview.tsx b/apps/webapp/app/components/run/RunOverview.tsx index f9086d3c66..7d8dbe4219 100644 --- a/apps/webapp/app/components/run/RunOverview.tsx +++ b/apps/webapp/app/components/run/RunOverview.tsx @@ -159,6 +159,14 @@ export function RunOverview({ run, trigger, showRerun, paths }: RunOverviewProps
+ + This Run completed but it did not use any Tasks – this can cause unpredictable + results. Read the docs to view the solution. + Trigger Date: Thu, 26 Oct 2023 14:06:22 +0100 Subject: [PATCH 03/10] Conditionally show the no-task warning at the top of the run --- apps/webapp/app/components/run/RunOverview.tsx | 18 ++++++++++-------- .../app/presenters/RunPresenter.server.ts | 1 + 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/apps/webapp/app/components/run/RunOverview.tsx b/apps/webapp/app/components/run/RunOverview.tsx index 7d8dbe4219..16c8afa9cb 100644 --- a/apps/webapp/app/components/run/RunOverview.tsx +++ b/apps/webapp/app/components/run/RunOverview.tsx @@ -159,14 +159,16 @@ export function RunOverview({ run, trigger, showRerun, paths }: RunOverviewProps
- - This Run completed but it did not use any Tasks – this can cause unpredictable - results. Read the docs to view the solution. - + {(run.tasks.length === 0 || run.tasks.every((t) => t.noop)) && ( + + This Run completed but it did not use any Tasks – this can cause unpredictable + results. Read the docs to view the solution. + + )} Trigger Date: Thu, 26 Oct 2023 14:06:38 +0100 Subject: [PATCH 04/10] New job to test the no-task warning message --- references/job-catalog/src/events.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/references/job-catalog/src/events.ts b/references/job-catalog/src/events.ts index 8e4cb660bb..77bcb9dd53 100644 --- a/references/job-catalog/src/events.ts +++ b/references/job-catalog/src/events.ts @@ -77,4 +77,26 @@ client.defineJob({ }, }); +client.defineJob({ + id: "no-real-task", + name: "No real Task", + version: "0.0.1", + trigger: eventTrigger({ + name: "no.real.task", + schema: z.object({ + userId: z.string(), + }), + }), + run: async (payload, io, ctx) => { + await io.logger.info("Hello World", { ctx, payload }); + await io.wait("Wait 1 sec", 1); + //this is a real task + // await io.runTask("task-example-1", async () => { + // return { + // message: "Hello World", + // }; + // }); + }, +}); + createExpressServer(client); From f8b431e45adb62715ee1b1c402156671cda419ad Mon Sep 17 00:00:00 2001 From: James Ritchie Date: Thu, 26 Oct 2023 15:23:28 +0100 Subject: [PATCH 05/10] Created a new example Job for the CLI that uses Tasks --- references/job-catalog/package.json | 3 +- references/job-catalog/src/cli-example.ts | 54 +++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 references/job-catalog/src/cli-example.ts diff --git a/references/job-catalog/package.json b/references/job-catalog/package.json index 0059ee0e29..6295d21fa1 100644 --- a/references/job-catalog/package.json +++ b/references/job-catalog/package.json @@ -28,6 +28,7 @@ "replicate": "nodemon --watch src/replicate.ts -r tsconfig-paths/register -r dotenv/config src/replicate.ts", "misconfigured": "nodemon --watch src/misconfigured.ts -r tsconfig-paths/register -r dotenv/config src/misconfigured.ts", "auto-yield": "nodemon --watch src/auto-yield.ts -r tsconfig-paths/register -r dotenv/config src/auto-yield.ts", + "cli-example": "nodemon --watch src/cli-example.ts -r tsconfig-paths/register -r dotenv/config src/cli-example.ts", "dev:trigger": "trigger-cli dev --port 8080" }, "dependencies": { @@ -62,4 +63,4 @@ "ts-node": "^10.9.1", "tsconfig-paths": "^3.14.1" } -} \ No newline at end of file +} diff --git a/references/job-catalog/src/cli-example.ts b/references/job-catalog/src/cli-example.ts new file mode 100644 index 0000000000..b1eff1c2a1 --- /dev/null +++ b/references/job-catalog/src/cli-example.ts @@ -0,0 +1,54 @@ +import { createExpressServer } from "@trigger.dev/express"; +import { TriggerClient, eventTrigger } from "@trigger.dev/sdk"; + +export const client = new TriggerClient({ + id: "job-catalog", + apiKey: process.env["TRIGGER_API_KEY"], + apiUrl: process.env["TRIGGER_API_URL"], + verbose: false, + ioLogLocalEnabled: true, +}); + +// Your first job +// This Job will be triggered by an event, log a joke to the console, and then wait 5 seconds before logging the punchline. +client.defineJob({ + // This is the unique identifier for your Job, it must be unique across all Jobs in your project. + id: "example-job", + name: "Example Job: a joke with a delay", + version: "0.0.1", + // This is triggered by an event using eventTrigger. You can also trigger Jobs with webhooks, on schedules, and more: https://trigger.dev/docs/documentation/concepts/triggers/introduction + trigger: eventTrigger({ + name: "example.event", + }), + run: async (payload, io, ctx) => { + // Use a Task to generate a random number. Using a Tasks means it only runs once. + const result = await io.runTask("generate-random-number", async () => { + return { + num: Math.floor(Math.random() * 10000), + }; + }); + + // Use the random number in a joke and log it to the console. + await io.logger.info(`Why was the number ${result.num} afraid of the number 7?`); + + // Wait for 5 seconds, the second parameter is the number of seconds to wait, you can add delays of up to a year. + await io.wait("Wait 5 seconds for the punchline...", 5); + + // Use a Task to display the answer. Tasks are important to use in all Jobs as they allow your Runs to resume again after e.g. a serverless function timeout. Learn more about Tasks in the docs: https://trigger.dev/docs/documentation/concepts/tasks + await io.runTask( + "task-example", + async () => { + return { + foo: "bar", + }; + }, + { name: `Answer: Because 7,8,9! And ${result.num} was next 🤦` } + ); + await io.logger.info( + "✨ Congratulations, You just ran your first successful Trigger.dev Job! ✨" + ); + // To learn how to write much more complex (and probably funnier) Jobs, check out our docs: https://trigger.dev/docs/documentation/guides/create-a-job + }, +}); + +createExpressServer(client); From 17cbe37b8ae6afa82d8139585b8365fd9919d031 Mon Sep 17 00:00:00 2001 From: James Ritchie Date: Thu, 26 Oct 2023 16:00:22 +0100 Subject: [PATCH 06/10] Updated the Next.js cli joke job --- .../cli/src/templates/nextjs/exampleJob.js | 33 ++++++++++++++----- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/packages/cli/src/templates/nextjs/exampleJob.js b/packages/cli/src/templates/nextjs/exampleJob.js index ff0fbdc800..9cd5516002 100644 --- a/packages/cli/src/templates/nextjs/exampleJob.js +++ b/packages/cli/src/templates/nextjs/exampleJob.js @@ -2,9 +2,9 @@ import { eventTrigger } from "@trigger.dev/sdk"; import { client } from "${jobsPathPrefix}trigger"; // Your first job -// This Job will be triggered by an event, log a joke to the console, and then wait 5 seconds before logging the punchline +// This Job will be triggered by an event, log a joke to the console, and then wait 5 seconds before logging the punchline. client.defineJob({ - // This is the unique identifier for your Job, it must be unique across all Jobs in your project + // This is the unique identifier for your Job, it must be unique across all Jobs in your project. id: "example-job", name: "Example Job: a joke with a delay", version: "0.0.1", @@ -13,15 +13,32 @@ client.defineJob({ name: "example.event", }), run: async (payload, io, ctx) => { - // This logs a message to the console - await io.logger.info("🧪 Example Job: a joke with a delay"); - await io.logger.info("How do you comfort a JavaScript bug?"); - // This waits for 5 seconds, the second parameter is the number of seconds to wait, you can add delays of up to a year + // Use a Task to generate a random number. Using a Tasks means it only runs once. + const result = await io.runTask("generate-random-number", async () => { + return { + num: Math.floor(Math.random() * 10000), + }; + }); + + // Use the random number in a joke and log it to the console. + await io.logger.info(`Why was the number ${result.num} afraid of the number 7?`); + + // Wait for 5 seconds, the second parameter is the number of seconds to wait, you can add delays of up to a year. await io.wait("Wait 5 seconds for the punchline...", 5); - await io.logger.info("You console it! 🤦"); + + // Use a Task to display the answer. Tasks are important to use in all Jobs as they allow your Runs to resume again after e.g. a serverless function timeout. Learn more about Tasks in the docs: https://trigger.dev/docs/documentation/concepts/tasks + await io.runTask( + "task-example", + async () => { + return { + foo: "bar", + }; + }, + { name: `Answer: Because 7,8,9! And ${result.num} was next 🤦` } + ); await io.logger.info( "✨ Congratulations, You just ran your first successful Trigger.dev Job! ✨" ); // To learn how to write much more complex (and probably funnier) Jobs, check out our docs: https://trigger.dev/docs/documentation/guides/create-a-job }, -}); +}); \ No newline at end of file From 323a4b58f8ac93003afe34161bb7d2810fa7f84c Mon Sep 17 00:00:00 2001 From: James Ritchie Date: Thu, 26 Oct 2023 16:34:00 +0100 Subject: [PATCH 07/10] Updated the Remix app cli job example --- .../cli/src/templates/remix/exampleJob.js | 33 ++++++++++++++----- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/packages/cli/src/templates/remix/exampleJob.js b/packages/cli/src/templates/remix/exampleJob.js index affba011fd..3892a931f8 100644 --- a/packages/cli/src/templates/remix/exampleJob.js +++ b/packages/cli/src/templates/remix/exampleJob.js @@ -2,9 +2,9 @@ import { eventTrigger } from "@trigger.dev/sdk"; import { client } from "${jobsPathPrefix}trigger.server"; // Your first job -// This Job will be triggered by an event, log a joke to the console, and then wait 5 seconds before logging the punchline -export const job = client.defineJob({ - // This is the unique identifier for your Job, it must be unique across all Jobs in your project +// This Job will be triggered by an event, log a joke to the console, and then wait 5 seconds before logging the punchline. +client.defineJob({ + // This is the unique identifier for your Job, it must be unique across all Jobs in your project. id: "example-job", name: "Example Job: a joke with a delay", version: "0.0.1", @@ -13,12 +13,29 @@ export const job = client.defineJob({ name: "example.event", }), run: async (payload, io, ctx) => { - // This logs a message to the console - await io.logger.info("🧪 Example Job: a joke with a delay"); - await io.logger.info("How do you comfort a JavaScript bug?"); - // This waits for 5 seconds, the second parameter is the number of seconds to wait, you can add delays of up to a year + // Use a Task to generate a random number. Using a Tasks means it only runs once. + const result = await io.runTask("generate-random-number", async () => { + return { + num: Math.floor(Math.random() * 10000), + }; + }); + + // Use the random number in a joke and log it to the console. + await io.logger.info(`Why was the number ${result.num} afraid of the number 7?`); + + // Wait for 5 seconds, the second parameter is the number of seconds to wait, you can add delays of up to a year. await io.wait("Wait 5 seconds for the punchline...", 5); - await io.logger.info("You console it! 🤦"); + + // Use a Task to display the answer. Tasks are important to use in all Jobs as they allow your Runs to resume again after e.g. a serverless function timeout. Learn more about Tasks in the docs: https://trigger.dev/docs/documentation/concepts/tasks + await io.runTask( + "task-example", + async () => { + return { + foo: "bar", + }; + }, + { name: `Answer: Because 7,8,9! And ${result.num} was next 🤦` } + ); await io.logger.info( "✨ Congratulations, You just ran your first successful Trigger.dev Job! ✨" ); From 3d03b89798a73c6190c588899d3f3d0238e998e0 Mon Sep 17 00:00:00 2001 From: James Ritchie Date: Thu, 26 Oct 2023 16:34:07 +0100 Subject: [PATCH 08/10] Updated the Astro app cli job example --- .../cli/src/templates/astro/exampleJob.js | 31 ++++++++++++++----- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/packages/cli/src/templates/astro/exampleJob.js b/packages/cli/src/templates/astro/exampleJob.js index ff0fbdc800..77cacbf5d3 100644 --- a/packages/cli/src/templates/astro/exampleJob.js +++ b/packages/cli/src/templates/astro/exampleJob.js @@ -2,9 +2,9 @@ import { eventTrigger } from "@trigger.dev/sdk"; import { client } from "${jobsPathPrefix}trigger"; // Your first job -// This Job will be triggered by an event, log a joke to the console, and then wait 5 seconds before logging the punchline +// This Job will be triggered by an event, log a joke to the console, and then wait 5 seconds before logging the punchline. client.defineJob({ - // This is the unique identifier for your Job, it must be unique across all Jobs in your project + // This is the unique identifier for your Job, it must be unique across all Jobs in your project. id: "example-job", name: "Example Job: a joke with a delay", version: "0.0.1", @@ -13,12 +13,29 @@ client.defineJob({ name: "example.event", }), run: async (payload, io, ctx) => { - // This logs a message to the console - await io.logger.info("🧪 Example Job: a joke with a delay"); - await io.logger.info("How do you comfort a JavaScript bug?"); - // This waits for 5 seconds, the second parameter is the number of seconds to wait, you can add delays of up to a year + // Use a Task to generate a random number. Using a Tasks means it only runs once. + const result = await io.runTask("generate-random-number", async () => { + return { + num: Math.floor(Math.random() * 10000), + }; + }); + + // Use the random number in a joke and log it to the console. + await io.logger.info(`Why was the number ${result.num} afraid of the number 7?`); + + // Wait for 5 seconds, the second parameter is the number of seconds to wait, you can add delays of up to a year. await io.wait("Wait 5 seconds for the punchline...", 5); - await io.logger.info("You console it! 🤦"); + + // Use a Task to display the answer. Tasks are important to use in all Jobs as they allow your Runs to resume again after e.g. a serverless function timeout. Learn more about Tasks in the docs: https://trigger.dev/docs/documentation/concepts/tasks + await io.runTask( + "task-example", + async () => { + return { + foo: "bar", + }; + }, + { name: `Answer: Because 7,8,9! And ${result.num} was next 🤦` } + ); await io.logger.info( "✨ Congratulations, You just ran your first successful Trigger.dev Job! ✨" ); From f6159a6498751950d7a96ada501d675ab844741e Mon Sep 17 00:00:00 2001 From: James Ritchie Date: Thu, 26 Oct 2023 17:05:01 +0100 Subject: [PATCH 09/10] Made sure the example job works in remix --- packages/cli/src/templates/remix/exampleJob.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cli/src/templates/remix/exampleJob.js b/packages/cli/src/templates/remix/exampleJob.js index 3892a931f8..4e4f95cd58 100644 --- a/packages/cli/src/templates/remix/exampleJob.js +++ b/packages/cli/src/templates/remix/exampleJob.js @@ -3,7 +3,7 @@ import { client } from "${jobsPathPrefix}trigger.server"; // Your first job // This Job will be triggered by an event, log a joke to the console, and then wait 5 seconds before logging the punchline. -client.defineJob({ +export const job = client.defineJob({ // This is the unique identifier for your Job, it must be unique across all Jobs in your project. id: "example-job", name: "Example Job: a joke with a delay", From 01044a168effefe9d6f4a130e885f454ab4d6569 Mon Sep 17 00:00:00 2001 From: James Ritchie Date: Thu, 26 Oct 2023 18:01:29 +0100 Subject: [PATCH 10/10] Added changeset --- .changeset/pretty-adults-admire.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/pretty-adults-admire.md diff --git a/.changeset/pretty-adults-admire.md b/.changeset/pretty-adults-admire.md new file mode 100644 index 0000000000..9283d53b2b --- /dev/null +++ b/.changeset/pretty-adults-admire.md @@ -0,0 +1,5 @@ +--- +"@trigger.dev/cli": patch +--- + +Added new example job which uses Tasks