-
-
Notifications
You must be signed in to change notification settings - Fork 940
Closed
Labels
area/docsarea/serverIssues related to the Trigger.dev serverIssues related to the Trigger.dev serverenhancementNew feature or requestNew feature or request
Description
This kind of thing would be really useful in jobs:
await io.waitUntil("has used invite", {
condition: async () => {
const user = await prisma.user.findUniqueOrThrow({
where: {
id: payload.userId,
},
});
return user.invitationCodeId !== null;
},
});This would basically retry the condition function in an interval, and only continue once the condition function returned true.
The strategy for how frequent to call the condition function could be configurable:
- Set an interval frequency (minimum would be 60 seconds, max would be 1 day). Would also need some kind of timeout thing like "exit if the condition doesn't return true within 30 days"
- Set an exponential backoff strategy (similar to our task retries) with a maximum of 25 retries
This can be achieved using only subtasks and io.wait, something like:
type WaitUntilOptions = {
condition: () => Promise<boolean>;
checkInterval?: number;
};
function waitUntil(
key: string,
io: IO,
options: WaitUntilOptions
): Promise<void> {
return io.runTask(
key,
{
name: "Wait until",
},
async (task) => {
const result = await options.condition();
if (result) return;
//if more than 14 days has passed then throw
const startedAt = task.startedAt;
if (!startedAt) return;
const now = new Date();
const diff = now.getTime() - startedAt.getTime();
const days = diff / (1000 * 60 * 60 * 24);
if (days > 14) {
throw new Error(
`Waited for 14 days for condition to be true but it never was.`
);
}
await io.wait(
`${key}:${new Date().toISOString()}`,
options.checkInterval ? Math.max(options.checkInterval, 30) : 60 * 60
);
}
);
}But i'd prefer this not to produce a bunch of tasks that are only there for the interval.
Metadata
Metadata
Assignees
Labels
area/docsarea/serverIssues related to the Trigger.dev serverIssues related to the Trigger.dev serverenhancementNew feature or requestNew feature or request