M Zeeshan
M Zeeshan3mo ago

Schedule function

hello there I need to execute a specific task every minute on creation (api.some_task.create), but only for a duration of 10 minutes. Could someone please guide me on how to schedule this function?
16 Replies
Convex Bot
Convex Bot3mo ago
Thanks for posting in <#1088161997662724167>. Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets. - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.) - Use search.convex.dev to search Docs, Stack, and Discord all at once. - Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI. - Avoid tagging staff unless specifically instructed. Thank you!
ballingt
ballingt3mo ago
Sounds like you want to use a scheduled function https://docs.convex.dev/scheduling/scheduled-functions that reschedules itself 9 times.
Hmza
Hmza3mo ago
npm
@convex-dev/crons
Convex component for scheduling periodic jobs.. Latest version: 0.1.1, last published: 14 days ago. Start using @convex-dev/crons in your project by running npm i @convex-dev/crons. There are no other projects in the npm registry using @convex-dev/crons.
ballingt
ballingt3mo ago
oooo or that!
Hmza
Hmza3mo ago
Sory lol
ballingt
ballingt3mo ago
The crons approach is cool too
Hmza
Hmza3mo ago
Yeah indy suggested this the other day
ballingt
ballingt3mo ago
This version si the component, I wouldn't recommend a component yet for a production app @M Zeeshan without the caveat that these are still in beta, but you're welcome to try it! I'd probably write a mutation (or action? what does it need to do?) like
export const aSpecificTask = mutation((ctx, args) => {
console.log('accomplish the task');
if (args.reschedule > 0) {
ctx.scheduler.runAfter(10000, internal.thisFile.aSpecificTask, {reschedule: args.reschedule - 1});
}
})
export const aSpecificTask = mutation((ctx, args) => {
console.log('accomplish the task');
if (args.reschedule > 0) {
ctx.scheduler.runAfter(10000, internal.thisFile.aSpecificTask, {reschedule: args.reschedule - 1});
}
})
M Zeeshan
M ZeeshanOP3mo ago
@ballingt @Hmza at the moment i am executing a cron job for every minute but i know this is not appropriate solution, actually i am practicing web push notification where user gets push notification every minute, after 10 minutes i unsubscribe it auto. in dry code step, i realized why not schedual a function when i am creating subscription instead of executing cron job every minute. here is the working push notifications crons.ts
crons.interval(
'push notification',
{ minutes: 1 }, // every minute
internal.actions.sendNotification,
);
crons.interval(
'push notification',
{ minutes: 1 }, // every minute
internal.actions.sendNotification,
);
ballingt
ballingt3mo ago
@M Zeeshan does the scheduler.runAfter solution above make sense?
M Zeeshan
M ZeeshanOP3mo ago
it works... but i dont want to call cron job every minute why do i call cron if nobody interact
ballingt
ballingt3mo ago
@M Zeeshan I'm recommending not calling a cron job every minute instead, scheduling a function you can either schedule a function that will reschedule itself 9 times or schedule the function 10 times up front
M Zeeshan
M ZeeshanOP3mo ago
self repetation ? how to?
M Zeeshan
M ZeeshanOP3mo ago
i c... this one this makes sense and i will use it i have one more question as queries runs immediately how to call query function inside an event e.g. onClick
ballingt
ballingt3mo ago
Convex React | Convex Developer Hub
Convex React is the client library enabling your React application to interact

Did you find this page helpful?