#Job does not execute immediately after creation

5 messages · Page 1 of 1 (latest)

low delta
#

I'm having an issue where my job does not execute immediately after being created. I have already run npx payload jobs:run beforehand, but nothing happens when I create a new job. The only way to get it to execute is by running npx payload jobs:run again.

Is there a way to ensure that jobs execute automatically as soon as they are created, without manually running the command again? Any insights would be appreciated.

// when create job
const createdJob = await payload.jobs.queue({
      task: 'instagramCrawler',
      input: {},
    })

// in handler
export const instagramCrawler = {
  slug: 'instagramCrawler',

  handler: async ({ req }) => {
    try {
      console.log('instagramCrawler running')
      
      // ...

      return {
        output: posts,
      }
    } catch (error: any) {
      console.error(error)
      throw error
    }
  },
} as TaskConfig<'instagramCrawler'>
feral tulipBOT
low delta
low delta
#

solved

import { getPayload } from 'payload'
import { config } from '@/index'

async function background() {
  console.log(`[Auto run all jobs] Start`)
  const payload = await getPayload({ config })
  while (true) {
    const results = await payload.jobs.run()
    console.log(results)
    await new Promise((resolve) => setTimeout(resolve, 30000))
  }
}

background()
obsidian oxide
#

where is the background() method execute?