Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling Promote on Job does not check if Queue is Ready #1231

Closed
chocof opened this issue Mar 1, 2019 · 4 comments
Closed

Calling Promote on Job does not check if Queue is Ready #1231

chocof opened this issue Mar 1, 2019 · 4 comments
Labels

Comments

@chocof
Copy link

chocof commented Mar 1, 2019

Description

When we create delayed jobs in a queue from a producer application and try to promote them from a consumer application then we get an error.

Scenario

The first script produces some delayed jobs

const Queue = require('bull');
const testQueue = new Queue('testqueue', 'redis://127.0.0.1:6379');

testQueue.process((job, done) => {
  console.log('Executing Job', job.data);
  done();
});

const init = async () => {
  await testQueue.add({ test: 'test1' }, {
    delay: 1000000,
    attempts: 2,
    timeout: 10,
    removeOnComplete: true,
    removeOnFail: true
  });
};

init();

This script reads and promotes them to be executed

const Queue = require('bull');
const testQueue = new Queue('testqueue', 'redis://127.0.0.1:6379');

testQueue.process((job, done) => {
  console.log('Executing Job', job.data);
  done()
});

const init = async () => {
  // assume that there is another place where we initialise
  // some delayed jobs and there is at least one of them in
  // the results of getDelayed
  const jobs = await testQueue.getDelayed();
  for (let i = 0; i < jobs.length; i++) await jobs[i].promote();
}
init();

Result

(node:13481) UnhandledPromiseRejectionWarning: TypeError: queue.client.promote is not a function
    at Object.promote (/Users/ftsokos/mainProj/bull/lib/scripts.js:342:25)
    at Job.promote (/Users/ftsokos/mainProj/bull/lib/job.js:281:20)
    at init (/Users/ftsokos/mainProj/code_samples/bull.sample.js:11:17)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
(node:13481) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:13481) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Cause

After looking into the code I believe that the problem here is that the queue has not yet
been properly initialised when we are trying to access the promote function.

To fix this issue I added the following code to job.promote function lib/job.js:280

Job.prototype.promote = function() {
  const queue = this.queue;
  const jobId = this.id;
  return this.queue.isReady().then(() =>
    scripts.promote(queue, jobId).then(result => {
      if (result === -1) {
        throw new Error('Job ' + jobId + ' is not in a delayed state');
      }
    })
  );
};

If you want me to, I can upload a PR to fix the issue.
However if this is the expected behaviour or if I am doing something wrong then please
let me know :)

Bull version

3.7.0

Additional information

The issue happens only when the consumer side is just initialised (due to the queue not being initialised).

@AnshulMalik
Copy link

Do we have a PR for this?

@chocof
Copy link
Author

chocof commented Jan 19, 2020

hello, I just created a pr for this #1619 :)

@tylercollier
Copy link

Until a new version is cut, I fixed this for my code by doing:

const job = // ...get job
await job.queue.isReady();
await job.promote();

jtassin pushed a commit to jtassin/bull that referenced this issue Jul 3, 2020
@stale
Copy link

stale bot commented Jul 12, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants