Skip to content

Queues of jobs cant be started. #12658

Closed
@AgrrFoss

Description

@AgrrFoss

Documentation Issue

I used template from docs, for start queue of jobs, but my task not work

https://payloadcms.com/docs/jobs-queue/tasks
https://payloadcms.com/docs/jobs-queue/queues

Additional Details

I use this code in payload.config.ts:

import { postgresAdapter } from '@payloadcms/db-postgres'
import { payloadCloudPlugin } from '@payloadcms/payload-cloud'
import { lexicalEditor } from '@payloadcms/richtext-lexical'
import path from 'path'
import { buildConfig, TaskConfig } from 'payload'
import { fileURLToPath } from 'url'
import sharp from 'sharp'

import { Users } from './collections/Users'
import { Media } from './collections/Media'

const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)

export default buildConfig({
  admin: {
    user: Users.slug,
    importMap: {
      baseDir: path.resolve(dirname),
    },
  },
  collections: [Users, Media],
  editor: lexicalEditor(),
  secret: process.env.PAYLOAD_SECRET || '',
  typescript: {
    outputFile: path.resolve(dirname, 'payload-types.ts'),
  },
  db: postgresAdapter({
    pool: {
      connectionString: process.env.DATABASE_URI || '',
    },
  }),
  jobs: {
    tasks: [
      {
        retries: {
          shouldRestore: false,
        },
        slug: 'testTask',
        handler: async ({ input, job, req }) => {
          console.log('Test task working')
          return {
            output: {},
          }
        },
      }
    ],
    autoRun: [
      {
        cron: '* * * * *', // every hour at minute 0
        limit: 100, // limit jobs to process each run
        queue: 'default', // name of the queue
      },
      // add as many cron jobs as you want
    ],
    shouldAutoRun: async (payload) => {
      console.log('Test autorun')
      return true
    },
  },
  sharp,
  plugins: [
    payloadCloudPlugin(),
    // storage-adapter-placeholder
  ],
})

I see every minute in console:
'Test autorun'
'Test autorun'
'Test autorun'
...
But I expect:
Test autorun
Test task working
Test autorun
Test task working

If add in config:

  onInit: async (payload)=> {
    await payload.jobs.queue({
      task: 'testTask',
      input: {}
    })
  },

I have result in console:
Test autorun
[10:42:00] INFO: Running 1 jobs.
new: 1
retrying: 0
Test task working
POST /api/users/login 200 in 99ms
GET /admin 200 in 206ms
Test autorun

Why doesn't test task add in jobs queue and how can I do this?

My repo with problem: https://github.com/AgrrFoss/test-jobs

Metadata

Metadata

Assignees

Labels

documentationImprovements or additions to documentation

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions