timgit / pg-boss

Queueing jobs in Node.js using PostgreSQL like a boss
MIT License
1.73k stars 144 forks source link

bug: swallowed error when connection is closed #410

Open Eomm opened 12 months ago

Eomm commented 12 months ago

pg-boss version: 9.0.3

When a job is slow, the timeout parameter does not trigger any error/event nor the job.

Give the following snippet, it runs correctly with exit code 0 and no errors. I would expect an error event as notification that a job did not completed successfully.

It seems that the error is ignored here:

https://github.com/timgit/pg-boss/blob/64df1e471fdd44469702467ff09420c7396ba685/src/manager.js#L239

'use strict';

const pg = require('pg');
const PgBoss = require('pg-boss');

(async function () {
  try {
    await buildConsumer();
  } catch (error) {
    console.log({ globalErr: error.message });
  }
})();

async function buildConsumer () {

  const pool = new pg.Pool({
    user: 'postgres',
    password: 'postgres',
  });

  const boss = new PgBoss({
    schema: 'postgres',
    db: {
      executeSql: async (sql, params) => {
        console.log(`🐞 Executing SQL: ${sql.substring(0, 90).replaceAll('\n', ' ')}`)
        return await pool.query(sql, params)
      },
    }
  });

  boss.on('error', (error) => {
    console.log({ onError: error.message }); // ❌ NO ERROR
  });

  await boss.start();

  await boss.send('queueName', { body: 'body ' + Math.random() });
  console.log('📣 Inserted job');

  await boss.work('queueName',
    {
      teamSize: 1,
      newJobCheckInterval: 100,
    },
    async () => {
      console.log('🚀 Started job');
      await new Promise(resolve => setTimeout(resolve, 10000));
      console.log('🚀 Done job');
    }
  );
  console.log('📣 Start worker');

  await new Promise(resolve => setTimeout(resolve, 1000));

  console.log('📣 Stopping')
  await boss.stop({
    graceful: true,
    timeout: 2000, // ❌ NO TIMEOUT ERROR
  });
  console.log('📣 Stopped');

  await pool.end()
  console.log('📣 Pool closed');
}

What do you think?

timgit commented 11 months ago

The semantics of "error" is reserved for something unexpected happening during maintenance or cron handling, not a promise that wasn't cancelled before shutdown. If pg-boss were to detect and signal on a promise it had to give up on, I guess a new event could be created for that.