timgit / pg-boss

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

Could not get fetch the jobs #67

Closed Rukeith closed 6 years ago

Rukeith commented 6 years ago

Here is my demo below. I use boss.fetch but that return null.

const PgBoss = require('pg-boss');
const boss = new PgBoss('postgres://postgres:mysecretpassword@localhost/demo');

const onError = error => console.error(error);

boss.on('error', onError);

boss.start()
  .then(ready)
  .catch(onError);

function ready() {
  let id;
  boss.publish('demo', { param1: 'parameter1' }, { startIn: 100 })
    .then(jobId => console.log(`created demo ${jobId}`))
    .then(() => boss.fetch('demo', 10))
    .then((jobs) => console.log('jobs =', jobs))
    .catch(onError);

  boss.subscribe('demo', someJobHandler)
    .then(() => console.log('subscribed to demo'))
    .catch(onError);
}

function someJobHandler(job) {
  console.log(`received ${job.name} ${job.id}`);
  console.log(`data: ${JSON.stringify(job.data)}`);

  job.done()
    .then(() => console.log(`demo ${job.id} completed`))
    .catch(onError);
}
timgit commented 6 years ago

Try removing your startIn option on publish. You’re delaying the job.

Rukeith commented 6 years ago

@timgit Couldn't I fetch the delay job?

timgit commented 6 years ago

the startIn option means, “do not make this job available to anyone until an interval of time has elapsed”. What is your use case?

Rukeith commented 6 years ago

@timgit My case is I will publish a job with some conditions. When the same condition of job publish again. I want to cancel the previous job which was setting the startIn to delayed.

timgit commented 6 years ago

Ok, well I'm not in a position to know what's best for this use case, but one thing you could try is calling cancel(id) to cancel the previous job. You could also take a look at the throttling options if that fits what you're trying to do.