Skip to content

Commit

Permalink
cmt
Browse files Browse the repository at this point in the history
  • Loading branch information
JUNIORCO committed Sep 8, 2024
1 parent 8668cdf commit 9196cfa
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
12 changes: 11 additions & 1 deletion app/trigger/first-run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: <explanation>
onFailure: async (payload, error: any) => {
Expand Down Expand Up @@ -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,
Expand All @@ -148,6 +157,7 @@ export const firstRun = task({
schedulePayload,
stripeCustomerId: currentPyng.stripeCustomerId,
clerkUserId,
runId: run.id,
};
},
});
27 changes: 25 additions & 2 deletions app/trigger/pyng.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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");
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -153,5 +171,10 @@ export const pyngTask = schedules.task({
if (emailResponse.error) {
throw new Error(emailResponse.error.message);
}

return {
skip: false,
runId: run.id,
};
},
});

0 comments on commit 9196cfa

Please sign in to comment.