I building ecommerce application, i want to generate the PDF document after every order is created then send to customer via email,
How can I implement this?
Create two jobs, one for generating pdf and one for sending the email and put them together in a workflow. In an after
CollectionAfterOperationHookyou check for
createoperation and then add your workflow to the queue.
Can you give me an example??
check the documentation:
Then you can ask more specific questions
Thanks, I've another small task which I needed in my application,
Fetch The Latest Gold Price From External Service.
I've created the
./src/tasks/fetchGoldPrice.tsThis fetch new price from external API and update the Global 'goldRate' collection.
import { TaskHandler } from 'payload'
import axios from 'axios'
export const fetchGoldPriceHandler: TaskHandler<any> = async ({ input, job, req }) => {
try {
const { data } = await axios.get('https://www.goldapi.io/api/XAU/INR', {
headers: {
'x-access-token': process.env.GOLD_API_KEY!,
'Content-Type': 'application/json',
},
})
const { price_gram_24k, price_gram_22k, price_gram_18k } = data
await req.payload.updateGlobal({
slug: 'goldRate',
data: {
price_gram_24k,
price_gram_22k,
price_gram_18k,
updatedAt: new Date().toISOString(),
},
})
console.log('Gold price updated')
return {
output: {
success: true,
},
}
} catch (err) {
console.error('Error fetching gold price: ', err)
return {
output: {
success: false,
},
}
}
}Here is Payload Config:
tasks: [
{
slug: 'updateGoldPrice',
retries: 2,
inputSchema:[],
outputSchema: [
{
name: 'success',
type: 'text',
required: true
}
],
handler: fetchGoldPriceHandler
}
],I want this to rerun after every 30 minutes or hourly,,
I read the documentaion but didn't understand how to implement this
Can you tell me what to do further?
Payload does not have a direct way of doing recurring tasks. You could use onInit in payload-config to add the task to the queue initially and then inside the task function you could add the task to the queue again.
Just looking at the documentation again, this could maybe be used for recurring tasks:
hmm maybe not
There is a option in jobs.autoRun function (As per documentaiton)
I never tried this, I'll try and let you know
When you run your own server in production you can use the autoRun functionality.
Hi
here is my final jobs config
jobs: {
tasks: [
{
slug: 'fetchGoldPrice',
retries: 2,
inputSchema:[],
outputSchema: [
{
name: 'price_gram_24k',
type: 'number',
required: true
},
{
name: 'price_gram_22k',
type: 'number',
required: true
},
{
name: 'price_gram_18k',
type: 'number',
required: true
}
],
handler: fetchGoldPriceHandler
},
{
slug: 'updateGoldRate',
retries: 2,
inputSchema: [],
outputSchema: [
{
name: 'success',
type: 'text',
required: true
}
],
handler: updateGoldRateHandler
}
],
workflows: [
{
slug: 'updateGoldPrice',
inputSchema: [],
handler: async ({job, tasks}) => {
const data = await tasks.fetchGoldPrice('1');
const { price_gram_24k, price_gram_22k, price_gram_18k } = data
await tasks.updateGoldRate('2', {
input: {
price_gram_24k,
price_gram_22k,
price_gram_18k
}
})
}
} as WorkflowConfig<'updateGoldPrice'>
],
autoRun: [
{
cron: '0 * * * *',
limit: 10,
queue: 'hourly',
},
],
shouldAutoRun: async (payload) => {
const hourly = await payload.jobs.queue({
workflow: 'updateGoldPrice',
input: {},
});
return true
},
},No Jobs are running
Yes, you have to queue the job/workflow with
await payload.jobs.queue({
workflow: 'updateGoldPrice',
})You can do the initial queueing in payload-config
onInitmethod and then again at the end of your workflow. Just that when the workflow ends it will queue a new execution
Ah, just saw you use
shouldRun. It could work, but it may called more often then you think. I don't know. You are just missing the initial start, like I mentioned before
thanks,
understood, I'll make some change then get back to you
thanks bro,, you really saved my day
Star
Discord
online
Get dedicated engineering support directly from the Payload team.