Skip to content

Instantly share code, notes, and snippets.

@tunnckoCore
Created November 6, 2025 21:19
Show Gist options
  • Select an option

  • Save tunnckoCore/a0c68b24a28172d3c75169917581880a to your computer and use it in GitHub Desktop.

Select an option

Save tunnckoCore/a0c68b24a28172d3c75169917581880a to your computer and use it in GitHub Desktop.
civex queue pattern
// Enqueue an email
export const enqueueEmail = mutation({
args: { recipient: v.string(), body: v.string() },
handler: async (ctx, args) => {
await ctx.db.insert("emails", args);
}
});
// Process a batch of emails (e.g., the first 10)
export const processBatchOfEmails = mutation({
args: {},
handler: async (ctx) => {
const emails = await ctx.db.query("emails").take(10);
await ctx.scheduler.runAfter(0, internal.emails.sendEmails, { emails });
},
});
// Action to send emails and then delete them
export const sendEmails = internalAction({
args: {},
handler: async (ctx) => {
await ResendClient.sendBatch(emails);
await ctx.runMutation(internal.emails.deleteBatchOfEmails, {
emails: emails.map((email) => email._id)
});
},
});
// Mutation to delete a batch of emails
export const deleteBatchOfEmails = internalMutation({
args: { emails: v.array(v.id("emails")) },
handler: async (ctx) => {
await Promise.all(emails.map((email) => ctx.db.delete(email)));
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment