timgit / pg-boss

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

indefinite promises #288

Closed basaran closed 2 years ago

basaran commented 2 years ago

Hello again :)

A line of code is said to be worth a thousand words, so could you please watch the movie below and let me know if you think this might be an issue with pg-boss:

// in an unknown Class extends EventEmitter. constructor...
        this.resolve;
        this.reject;
        this.errors = 0;
        this.swear = new Promise((resolve, reject) => {
            this.resolve = resolve;
            this.reject = reject;
            setInterval(() => {}, 10000);
        });

        this.swear.resolve = this.resolve;
        this.swear.reject = this.reject;

        this.on("stop", (x) => {
            console.log("stop received");
            this.swear.reject();
        });

       return this;
// in an unknown subscription handler

async function longTask() {
    const task = new Task();
    task.boot("/");
    await task.swear;
}
// back in the unknown Class extending eventEmitter boot fn...
async boot() {
  try {
     const apiResults = await fetchSomeApi();

     if (this.errors >= 10) return;
   } catch (e) {
    this.emit("stop", e);
 }
}

I'm basically creating an indefinite promise, for pg-boss to hold on to ,and rejecting it later if need be. Hope this makes sense. The dumb setInterval is there to keep the promise open, otherwise node I suppose claims it or something.

basaran commented 2 years ago

It appears to work fine, just asking if you think this might blow up in my face later which happens more often than it should :)

timgit commented 2 years ago

pg-boss will simply wait on the promise. There are no more rules. :)

basaran commented 2 years ago

arigato gosaymasu, closing again.