From 9196cfa1c7f3e54feef483b9a4ff58def4bbd171 Mon Sep 17 00:00:00 2001 From: "Sami @JUNIORCO" Date: Sun, 8 Sep 2024 17:15:39 -0400 Subject: [PATCH] cmt --- app/trigger/first-run.ts | 12 +++++++++++- app/trigger/pyng.ts | 27 +++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/app/trigger/first-run.ts b/app/trigger/first-run.ts index d00e9cf..e22a9bc 100644 --- a/app/trigger/first-run.ts +++ b/app/trigger/first-run.ts @@ -50,6 +50,15 @@ export const firstRun = task({ }); console.log("Billing meter event created", meterEvent); } + + await prisma.run.update({ + where: { + id: output.runId, + }, + data: { + status: RunStatus.Success, + }, + }); }, // biome-ignore lint/suspicious/noExplicitAny: onFailure: async (payload, error: any) => { @@ -133,7 +142,7 @@ export const firstRun = task({ const markdown = await scrape(currentPyng.url); // create run - await prisma.run.create({ + const run = await prisma.run.create({ data: { pyngId, scrape: markdown, @@ -148,6 +157,7 @@ export const firstRun = task({ schedulePayload, stripeCustomerId: currentPyng.stripeCustomerId, clerkUserId, + runId: run.id, }; }, }); diff --git a/app/trigger/pyng.ts b/app/trigger/pyng.ts index 2911265..e48895a 100644 --- a/app/trigger/pyng.ts +++ b/app/trigger/pyng.ts @@ -1,3 +1,4 @@ +import { RunStatus } from "@prisma/client"; import { schedules } from "@trigger.dev/sdk/v3"; import OpenAI from "openai"; import { zodResponseFormat } from "openai/helpers/zod"; @@ -12,7 +13,11 @@ import stripe from "./stripe"; export const pyngTask = schedules.task({ id: "pyng", - onSuccess: async (payload) => { + onSuccess: async (payload, output: { skip: boolean; runId: string }) => { + if (output.skip) { + return; + } + const pyngId = payload.externalId; if (!pyngId) { throw new Error("externalId (which is the pyngId) is required"); @@ -49,6 +54,15 @@ export const pyngTask = schedules.task({ }); console.log("Billing meter event created", meterEvent); } + + await prisma.run.update({ + where: { + id: output.runId, + }, + data: { + status: RunStatus.Success, + }, + }); }, run: async (payload) => { const pyngId = payload.externalId; @@ -116,6 +130,7 @@ export const pyngTask = schedules.task({ // save to db const run = await prisma.run.create({ data: { + status: RunStatus.Pending, scrape: markdown, reasoning: `Reasoning:\n${message?.reasoning}\n\nReflection:\n${message?.reflection}`, sentEmail: message?.shouldSendEmail || false, @@ -128,7 +143,10 @@ export const pyngTask = schedules.task({ if (!message?.shouldSendEmail) { console.log("Condition not met, done."); - return; + return { + skip: true, + runId: run.id, + }; } // if condition is met, send email @@ -153,5 +171,10 @@ export const pyngTask = schedules.task({ if (emailResponse.error) { throw new Error(emailResponse.error.message); } + + return { + skip: false, + runId: run.id, + }; }, });