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

bug: swallowed error when connection is closed #410

Open
Eomm opened this issue Jul 12, 2023 · 1 comment
Open

bug: swallowed error when connection is closed #410

Eomm opened this issue Jul 12, 2023 · 1 comment

Comments

@Eomm
Copy link

Eomm commented Jul 12, 2023

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:

).catch(() => {}) // allow promises & non-promises to live together in harmony

'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
Copy link
Owner

timgit commented Jul 16, 2023

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.

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

No branches or pull requests

2 participants