I need to create a cron job

I need to create a cron job to update user info each hour, for example I need to increment credits of a user. How can I set the cron job?
4 Replies
kapa.ai
kapa.ai•4w ago
Hi @pollastrepiupiu. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Creating a Cron Job in Wasp You can create a recurring job in Wasp to increment user credits hourly using the job declaration with a schedule. Here's how to set it up: 1. Define the Job in your .wasp file
job updateUserCredits {
executor: PgBoss,
perform: {
fn: import { updateCredits } from "@src/workers/credits"
},
schedule: {
cron: "0 * * * *", // Run every hour at minute 0
args: {=json { "creditsToAdd": 1 } json=} // Optional arguments
},
entities: [User] // Include the entities you need access to
}
job updateUserCredits {
executor: PgBoss,
perform: {
fn: import { updateCredits } from "@src/workers/credits"
},
schedule: {
cron: "0 * * * *", // Run every hour at minute 0
args: {=json { "creditsToAdd": 1 } json=} // Optional arguments
},
entities: [User] // Include the entities you need access to
}
The cron expression 0 * * * * means "run at minute 0 of every hour". You can customize this using Crontab guru if you need a different schedule. 2. Implement the Worker Function Create the worker function that will be executed according to the schedule:
// src/workers/credits.js
export const updateCredits = async ({ creditsToAdd }, context) => {
// Update all users or apply specific criteria
const result = await context.entities.User.updateMany({
where: {
// Add any conditions here if needed
},
data: {
credits: {
increment: creditsToAdd
}
}
});

console.log(`Updated credits for ${result.count} users`);
return result;
}
// src/workers/credits.js
export const updateCredits = async ({ creditsToAdd }, context) => {
// Update all users or apply specific criteria
const result = await context.entities.User.updateMany({
where: {
// Add any conditions here if needed
},
data: {
credits: {
increment: creditsToAdd
}
}
});

console.log(`Updated credits for ${result.count} users`);
return result;
}
For TypeScript:
// src/workers/credits.ts
import { type UpdateUserCredits } from 'wasp/server/jobs'
import { type User } from 'wasp/entities'

type Input = { creditsToAdd: number }
type Output = { count: number }
export const updateCredits: UpdateUserCredits<Input, Output> = async ({ creditsToAdd }, context) => {
const result = await context.entities.User.updateMany({
where: {
// Add any conditions here if needed
},
data: {
credits: {
increment: creditsToAdd
}
}
});

console.log(`Updated credits for ${result.count} users`);
return result;
}
// src/workers/credits.ts
import { type UpdateUserCredits } from 'wasp/server/jobs'
import { type User } from 'wasp/entities'

type Input = { creditsToAdd: number }
type Output = { count: number }
export const updateCredits: UpdateUserCredits<Input, Output> = async ({ creditsToAdd }, context) => {
const result = await context.entities.User.updateMany({
where: {
// Add any conditions here if needed
},
data: {
credits: {
increment: creditsToAdd
}
}
});

console.log(`Updated credits for ${result.count} users`);
return result;
}
That's it! Wasp will automatically schedule and run this job every hour. The job will persist between server restarts and can be retried if it fails. Note: This requires PostgreSQL as your database since it uses the PgBoss executor. Make sure your app.db.system is set to PostgreSQL in your Wasp file. Recurring Jobs Documentation ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @pollastrepiupiu, 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?
pollastrepiupiu
pollastrepiupiuOP•4w ago
Will authomatically run If I work in local?
MEE6
MEE6•4w ago
Wohooo @pollastrepiupiu, you just became a Waspeteer level 2!
Carlos
Carlos•4w ago
Will authomatically run If I work in local?
Yep, when we run locally we do spin up a full DB with the necessary extensions, so everything will run smoothly 💫

Did you find this page helpful?