queue

How can I send a message to a queue ( for background processing) in a Wasp application?
2 Replies
kapa.ai
kapa.ai4mo ago
Hi @Karma Andrew. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To send a message to a queue for background processing in a Wasp application, you should use Wasp's Jobs feature. Here’s how you can do it: 1. Declare a Job in your .wasp file
Define a job and specify the function that should be executed in the background. For example:
job mySpecialJob {
executor: PgBoss,
perform: {
fn: import { foo } from "@src/workers/bar"
},
entities: [Task],
}

job mySpecialJob {
executor: PgBoss,
perform: {
fn: import { foo } from "@src/workers/bar"
},
entities: [Task],
}

This sets up a job called mySpecialJob that will use the foo function from your worker file for processing. The executor PgBoss is currently the only supported job executor and uses PostgreSQL as the backend queue system. The entities field allows you to specify which entities are available in the job context, if needed for database access Declaring Jobs. 2. Implement the Worker Function
In your worker file (e.g., src/workers/bar.js), implement the function that will process the job:
export const foo = async ({ name }, context) => {
console.log(`Hello ${name}!`)
// Do background processing here, e.g., database operations
}

export const foo = async ({ name }, context) => {
console.log(`Hello ${name}!`)
// Do background processing here, e.g., database operations
}

The function receives the job arguments and a context object with access to your entities Job Definition and Usage. 3. Submit a Job to the Queue
In your server-side code (e.g., an Action or another server function), import the job and submit it:
import { mySpecialJob } from 'wasp/server/jobs'

// Submit immediately
const submittedJob = await mySpecialJob.submit({ name: "Johnny" })
// Or, to delay execution (e.g., 10 seconds later)
await mySpecialJob.delay(10).submit({ name: "Johnny" })

import { mySpecialJob } from 'wasp/server/jobs'

// Submit immediately
const submittedJob = await mySpecialJob.submit({ name: "Johnny" })
// Or, to delay execution (e.g., 10 seconds later)
await mySpecialJob.delay(10).submit({ name: "Johnny" })

The .submit() method sends the job to the queue for background processing. You can also use .delay() to schedule the job for future execution JavaScript API. Summary:
- Declare a job in your .wasp file. - Implement the worker function. - Use .submit() (and optionally .delay()) to send a message to the queue for background processing. Wasp jobs are persistent, can be retried, delayed, or scheduled, and are suitable for typical background processing tasks in web applications Recurring Jobs. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @Karma Andrew, 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?
toticozeni
toticozeni4mo ago
We don't support queues as a wasp feature, rather you need to implement it yourself like in any other project. You can do it in postgres or use something separate (redis, rabbitmq...).

Did you find this page helpful?