Is there a way to test run a job with a command in wasp?

I'd like to test a script that I wrote but as it only runs once a day it's not practical. I could try and use it in a different file but wanted to check if there was an easy way first
3 Replies
kapa.ai
kapa.ai•6d ago
Hi @CamBlackwood. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- I'm sorry, but based on the provided knowledge sources, there isn't a specific Wasp CLI command to test run a job. However, you can submit work to be done in your Operations (https://wasp-lang.dev/docs/data-model/operations/overview) or setupFn (https://wasp-lang.dev/docs/project/server-config#setup-function) (or any other NodeJS code) after successfully defining the job. Here's an example of how to define a job:
// In your .wasp file
job mySpecialJob {
executor: PgBoss,
perform: {
fn: import { foo } from "@src/workers/bar"
},
entities: [Task],
}

// In src/workers/bar.js
export const foo = async ({ name }, context) => {
console.log(`Hello ${name}!`)
const tasks = await context.entities.Task.findMany({})
return { tasks }
}
// In your .wasp file
job mySpecialJob {
executor: PgBoss,
perform: {
fn: import { foo } from "@src/workers/bar"
},
entities: [Task],
}

// In src/workers/bar.js
export const foo = async ({ name }, context) => {
console.log(`Hello ${name}!`)
const tasks = await context.entities.Task.findMany({})
return { tasks }
}
The worker function must be an async function. The function's return value represents the Job's result. The worker function accepts two arguments: args (The data passed into the job when it's submitted) and context: { entities } (The context object containing entities you put in the Job declaration) source (https://wasp-lang.dev/docs/advanced/jobs#job-definition-and-usage). For more detailed information, you might want to check the official Wasp documentation or their GitHub repository. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @CamBlackwood, 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?
Vinny (@Wasp)
Vinny (@Wasp)•6d ago
yeah @CamBlackwood you could import the job into an operation or api endpoint and run it whenver that endpoint is hit
CamBlackwood
CamBlackwood•6d ago
@Vinny (@Wasp) thanks, ended up doing the API approach was super simple 🙂
Want results from more Discord servers?
Add your server