W
Wasp-lang3mo ago
Mrr

scheduling feature

In my wasp project I want to add a feature where user can schedule when they want emails to come (eg: daily or weekly once every Monday or monthly) so each user can schedule when they want. How can I implement this feature??
14 Replies
kapa.ai
kapa.ai3mo ago
Hi @Mrr. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To implement a feature where users can schedule when they want emails to come, you can use Wasp's support for background jobs. This allows you to delay jobs until a future time or have a recurring schedule. Here's a general approach you can take: 1. Create a Job for Sending Emails: You can create a job that sends an email using Wasp's emailSender API. This job will take the email details (like recipient, subject, and content) as arguments.
job sendEmailJob {
perform: { fn: import { sendEmail } from "@src/workers/sendEmail" },
// other configurations...
}
job sendEmailJob {
perform: { fn: import { sendEmail } from "@src/workers/sendEmail" },
// other configurations...
}
In the sendEmail function, you can use the emailSender API to send the email. 2. Schedule the Job: You can schedule the job to run at a future time using the delay method. The delay method takes a number of seconds, a Date, or an ISO date string.
await sendEmailJob
.delay(<time-you-want-email-to-be-sent>)
.submit({ address: "[email protected]" })
await sendEmailJob
.delay(<time-you-want-email-to-be-sent>)
.submit({ address: "[email protected]" })
3. Recurring Jobs: If you want the emails to be sent on a recurring basis (like daily or weekly), you can add a schedule to your job declaration. The schedule takes a cron expression that specifies when the job should be run.
job mySpecialJob {
executor: PgBoss,
perform: {
fn: import { foo } from "@src/workers/bar"
},
schedule: {
cron: "0 * * * *",
args: {=json { "job": "args" } json=} // optional
}
}
job mySpecialJob {
executor: PgBoss,
perform: {
fn: import { foo } from "@src/workers/bar"
},
schedule: {
cron: "0 * * * *",
args: {=json { "job": "args" } json=} // optional
}
}
In this example, the job is scheduled to run every hour. Please note that currently, Wasp's emailSender API does not support the send_at header for scheduling emails via SendGrid. There is an open issue for this on GitHub, and it will be looked into when working on the email sender again. For more information, you can refer to the Wasp documentation on Jobs (https://wasp-lang.dev/docs/advanced/jobs) and the Wasp documentation on the emailSender API (https://wasp-lang.dev/docs/advanced/email#javascript-api). Sources: - Discord support thread: Schedule email with sendgrid (https://discord.com/channels/686873244791210014/1179895919471247490) - Github Issue (open): [Email sender] Allow specifying provider specific options, like templateId for Sendgrid (https://github.com/wasp-lang/wasp/issues/1553) - Wasp Docs: Advanced > Recurring Jobs (https://wasp-lang.dev/docs/advanced/jobs) ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @Mrr, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
Mrr
MrrOP3mo ago
Anyone?
miho
miho3mo ago
@sodic one for you
Filip
Filip3mo ago
Hey @Mrr, This is more of a business logic/general devleopment question than a Wasp-specific one. That said, Wasp does offer some mechanisms to help with that. Here's what I'd do: 1. Ask users through the web app to specify their preference (daily, weekly, monthly) and save it to the database. 2. Set up three jobs, each scheduled to execute on the desired time periods - a monthly job, a weekly job (every Monday). 3. In each of those jobs, query the database and send the emails to appropriate users. Here's the documentation for jobs: https://wasp-lang.dev/docs/advanced/jobs Let me know if that works out!
Mrr
MrrOP3mo ago
Cool will try this!!
Filip
Filip3mo ago
Hey @Mrr, how's it going? Any success here?
Mrr
MrrOP3mo ago
@sodic I haven't tried it but will definitely update you by this week.
Filip
Filip3mo ago
Good luck 😄
VS
VS3mo ago
@sodic Hey Sodic, jumping on here - is there a way wasp could implement the cancelling job without having to call the pgboss library? If that's inconvenient, no worries I can call the library and implement it. Also thanks also for integrating websockets - wasp has so much!
Filip
Filip3mo ago
Hey @Adam! Yes, you can do it: https://wasp-lang.dev/docs/advanced/jobs#tracking Oh, did you know about this and meant something more high-level?
VS
VS3mo ago
Ah, yeah I meant like mySpecialJob .cancel(<jobid>) But no worries, I'll just call Pgboss and do it that way! I thought it would be a little neat add if you could .submit and .cancel!
miho
miho3mo ago
That's a good suggestion! I've added your comment to this issue: https://github.com/wasp-lang/wasp/issues/2229
GitHub
Expose PgBoss instance as part of public API · Issue #2229 · wasp-l...
From a Discord question: at the moment, I'm also using the id and the state of the current job to be able to cancel the job: let bosscancel = await boss.cancel(jobId); Can you tell me how I can...
Mrr
MrrOP3mo ago
@sodic Heyy!! I have done it and it's working. Like user can choose daily, weekly, monthly and I made 3 jobs for it to send Exact thing I followed Now I'll try to implement where user can choose which day in a week they want and next time too😁
Filip
Filip2mo ago
nice, great work!
Want results from more Discord servers?
Add your server