pulsecron / pulse

The modern MongoDB-powered job scheduler library for Node.js
https://pulsecron.com
MIT License
90 stars 4 forks source link

Promise pulse.stop() never resolve in some cases #16

Closed tiberioalunni closed 3 months ago

tiberioalunni commented 3 months ago

I'm facing a problem calling pulse.stop(), here an examle:

pulse.stop().then(() => {
   console.log('Pulse stopped');
});

The problem is that in case of success of this._collection.updateMany resolve() is not called. Another problem that may exists is that in case of fail of this._collection.updateMany both reject(error) and resolve() are called.

See https://github.com/pulsecron/pulse/blob/v1.1.9/src/pulse/stop.ts

const _unlockJobs = async (): Promise<void> => {
    return new Promise((resolve, reject) => {
      debug('Pulse._unlockJobs()');
      const jobIds = this._lockedJobs.map((job) => job.attrs._id);

      if (jobIds.length === 0) {
        debug('no jobs to unlock');
        resolve();
      }

      debug('about to unlock jobs with ids: %O', jobIds);
      this._collection.updateMany({ _id: { $in: jobIds } }, { $set: { lockedAt: null } }).catch((error) => {
        if (error) {
          reject(error);
        }

        this._lockedJobs = [];
        resolve();
      });
    });
  };

I'm not sure 100% if the following solution is the best one, but I think it will work

this._collection.updateMany({ _id: { $in: jobIds } }, { $set: { lockedAt: null } })
.then() {
  this._lockedJobs = [];
  resolve();
}
.catch((error) => {
  reject(error);
});
code-xhyun commented 3 months ago

Thank you for the issue! I will solve these and give you an answer as soon as possible.

code-xhyun commented 3 months ago

@tiberioalunni This was fixed in v1.1.10 🚀

tiberioalunni commented 3 months ago

Nice! Thank you!