timgit / pg-boss

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

Should expire_in_seconds set the state to expired? #443

Closed Eomm closed 3 months ago

Eomm commented 7 months ago

I was wondering if the expire_in_seconds should set a job's state to expired instead of error here:

https://github.com/timgit/pg-boss/blob/f1c1636caf9518ec9cd3fe0d0e2844ee98179e68/src/manager.js#L225-L227

I was able to get an exipired state only when the job is active and I stop and restart pg-boss. Is this expected?

Example:

'use strict';

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

buildConsumer();

async function buildConsumer () {
  const boss = new PgBoss({
    user: 'postgres',
    password: 'postgres',
    schema: 'postgres',
  });

  await boss.start();

  const jobId = await boss.send('queueName', { body: 'foo' }, { expireInSeconds: 2 });
  console.log('šŸ“£ Inserted job');

  await boss.work(
    'queueName',
    {
      teamSize: 1,
      newJobCheckInterval: 100,
    },
    async () => {
      console.log('šŸŒ Started SLOW job');
      await new Promise((resolve) => setTimeout(resolve, 5_000));
      console.log('Done job');

      const job = await boss.getJobById(jobId);
      console.log({ job }); // ā—ļø state: 'failed', message: 'handler execution exceeded 2000ms'
    },
  );
  console.log('šŸ“£ Start worker');

  process.on('SIGINT', async () => {
    console.log('šŸ“£ Stopping');
    await boss.stop();
    console.log('šŸ“£ Stopped');
    process.exit();
  });
}
timgit commented 4 months ago

The expired state will be obsolete in v10 to make this more consistent