timgit / pg-boss

Queueing jobs in Postgres from Node.js like a boss
MIT License
2.07k stars 157 forks source link

can not set schedule #376

Closed l3th2nh closed 1 year ago

l3th2nh commented 1 year ago

who can help me ? when i tried set a schedule by example :

await boss.schedule('notification-abc', '0 3 * * *', null, { tz: 'America/Chicago' })

i get a error

{ "code": "ERR_ASSERTION", "name": "AssertionError", "stack": "AssertionError [ERR_ASSERTION]: boss requires all jobs to have a queue name\n at Object.checkSendArgs (/srv/app/node_modules/pg-boss/src/attorney.js:52:3)\n at Manager.send (/srv/app/node_modules/pg-boss/src/manager.js:339:46)\n at Timekeeper.onSendIt (/srv/app/node_modules/pg-boss/src/timekeeper.js:199:24)\n at /srv/app/node_modules/pg-boss/src/timekeeper.js:56:164\n at pMap.concurrency (/srv/app/node_modules/pg-boss/src/manager.js:232:32)\n at /srv/app/node_modules/p-map/index.js:57:28\n at processTicksAndRejections (node:internal/process/task_queues:96:5)", "message": "boss requires all jobs to have a queue name", "expected": true, "operator": "==", "generatedMessage": false }

timgit commented 1 year ago

Please see the test suite on specific examples of scheduling

l3th2nh commented 1 year ago

yes @timgit that error even when i follow your instructions

const helper = require('./testHelper')

async function readme () { const PgBoss = require('../src') const boss = new PgBoss(helper.getConnectionString())

boss.on('error', console.error)

await boss.start()

const queue = 'some-queue'

await boss.schedule(queue, ' *')

console.log(created cronjob in queue ${queue})

await boss.work(queue, someAsyncJobHandler) }

async function someAsyncJobHandler (job) { console.log(running job ${job.id}) }

readme()

https://ibb.co/GTx1SZF

https://ibb.co/GTx1SZF

dimhold commented 3 months ago

Hi @timgit

Thank you for your awesome library.

I encountered the same issue while developing my Strapi plugin. It's not a pg-boss issue, but perhaps the quick research I've done might be useful to someone and save them some time. The issue can be reproduced using the example from the docs, as mentioned by @l3th2nh.

I am developing a custom Strapi plugin that needs to run jobs on a schedule. The problem occurs only with the strapi dependency and the following happens:

We read a job from the database: https://github.com/timgit/pg-boss/blame/9.0.3/src/manager.js#L497

result = await db.executeSql(preparedStatement, statementValues)

But the result we get has a string in the data field instead of an object. In the database, this JSONB field is stored correctly. result:

[
  {
    id: "a6124734-aa2a-4a20-84e3-1001f159cf1d",
    name: "__pgboss__send-it",
    // data is a STRING, BUT EXPECTED OBJECT:
    data: "{\"cron\": \"* * * * *\", \"data\": null, \"name\": \"some-queue\", \"options\": \"{}\", \"timezone\": \"UTC\", \"created_on\": \"2024-06-26T10:07:33.996Z\", \"updated_on\": \"2024-06-26T10:38:47.481Z\"}",
    expire_in_seconds: 900,
  },
]

// typeof result.rows[0].data -> 'string'

As a result, in onSendIt https://github.com/timgit/pg-boss/blob/9.0.3/src/timekeeper.js#L198 we cannot extract name, data, options:

  async onSendIt (job) {
    if (this.stopped) return
    const { name, data, options } = job.data   // name, data, and options are undefined because job.data is a string
    await this.manager.send(name, data, options)
  }

and we make an incorrect call: await this.manager.send(undefined, undefined, undefined);

This call, of course, does not pass the assertion: https://github.com/timgit/pg-boss/blob/9.0.3/src/attorney.js#L52 And the job fails with the message mentioned earlier: "message": "boss requires all jobs to have a queue name"

The issue of reading JSONB as a string is due to this strapi parser: https://github.com/strapi/strapi/blob/v4.25.1/packages/core/database/src/dialects/postgresql/index.ts#L27-L31

This topic is being discussed here: https://github.com/strapi/strapi/issues/18073 I also provided a workaround in that thread.