W
Wasp-lang2mo ago
Sven

Could I execute an action or multiple once per day on the server side?

Could I execute an action or multiple once per day on the server side for each user. It loads data from an api for that specific user, i want to know if I can make it such that this is done every day for each user.
4 Replies
kapa.ai
kapa.ai2mo ago
Hi @Sven. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Yes, you can execute an action or multiple actions once per day on the server side for each user using Wasp's support for background jobs. Specifically, you can use recurring jobs, which can be scheduled to run at specific intervals, such as once per day. Here is an example of how you can declare a recurring job in Wasp:
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 foo function is automatically scheduled and invoked every hour. You can adjust the cron expression to make it run once per day instead. The args field can be used to pass arguments to the job, such as user-specific data. The job will be executed by PgBoss, which is a job executor that handles scheduling, monitoring, and execution. It requires your app.db.system to be PostgreSQL and it runs alongside your other server-side code. You can submit a job like this:
import { mySpecialJob } from 'wasp/server/jobs'

const submittedJob = await mySpecialJob.submit({ job: "Johnny" })

// Or, if you'd prefer it to execute in the future, just add a .delay().
// It takes a number of seconds, Date, or ISO date string.
await mySpecialJob
.delay(10)
.submit({ name: "Johnny" })
import { mySpecialJob } from 'wasp/server/jobs'

const submittedJob = await mySpecialJob.submit({ job: "Johnny" })

// Or, if you'd prefer it to execute in the future, just add a .delay().
// It takes a number of seconds, Date, or ISO date string.
await mySpecialJob
.delay(10)
.submit({ name: "Johnny" })
In your case, you would need to create a job for each user and schedule it to run once per day. The job would call a function that loads data from an API for that specific user. Please note that you need to handle the logic of creating and scheduling a job for each user in your application code. For more information, you can refer to the Wasp documentation on jobs (https://wasp-lang.dev/docs/advanced/jobs). Sources: - Wasp Docs: Recurring Jobs (https://wasp-lang.dev/docs/advanced/jobs#recurring-jobs) - Wasp Docs: Job Definition and Usage (https://wasp-lang.dev/docs/advanced/jobs#job-definition-and-usage) - Wasp Docs: Declaring Jobs (https://wasp-lang.dev/docs/advanced/jobs#declaring-jobs) ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @Sven, 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?
miho
miho2mo ago
Could I execute an action or multiple once per day on the server side for each user.
Yes, you'd create a job which then takes either all of the users (if there isn't a lot of users) or takes a chunk of unprocessed users (you keep some state in the DB to know which users you processed and when) and does something for those users. This job can run each minute - because you keep track of which users you processed that day, it won't process users twice, so there is no harm in running it all the time.
Sven
SvenOP2mo ago
Ah really nice What if it is some kind of “heavy” job. Which gets information from an api and processes ut through multiple requests
miho
miho2mo ago
You'll need to come up with your own business logic that works for you 🙂 As I said:
you keep some state in the DB to know which users you processed and when
You do this to avoid repeating expensive work.
Want results from more Discord servers?
Add your server