pg-boss icon indicating copy to clipboard operation
pg-boss copied to clipboard

Stopping pg-boss gracefully does not wait until stop timeouts

Open StarpTech opened this issue 2 years ago • 9 comments

Hi, it looks like pg-boss does not respect graceful nor timeout property. I have a simple worker that needs at least 5 seconds to finish. On SIGTERM I clean up all my server resources but boss.stop does not block until the max timeout of 30s is over.

await fastify.pgboss.send(queue, { some: 'data' });

await fastify.pgboss.work(queue, async function someAsyncJobHandler(job) {
  console.log(`job ${job.id} received with data:`);

  // promise wait
  await new Promise((resolve) => setTimeout(resolve, 5000));

  console.log(job);
});

 const stopOptions: PgBoss.StopOptions = {
     timeout: 30_000,
     graceful: true,
	 destroy: false
};
await boss.stop(stopOptions);

StarpTech avatar Aug 27 '23 17:08 StarpTech

My fault. I still had a timeout in the shutdown phase. The bug is still valid.

StarpTech avatar Aug 27 '23 18:08 StarpTech

I was able to workaround this by

await boss.stop(stopOptions);

await new Promise((resolve) => {
  boss.once('stopped', () => resolve(null));
});

fastify.log.info('PgBoss shutdown complete');

I think it would be more intuitive if this is the default behavior of stop(). Would you accept a PR?

StarpTech avatar Aug 27 '23 19:08 StarpTech

PRs are always welcome. I think it should be opt-in via as a new option on stop(). Thanks!

timgit avatar Aug 30 '23 20:08 timgit

What's the reason for passing it onto the next cycle here? + why is a delay of 1000ms required?

StarpTech avatar Aug 30 '23 20:08 StarpTech

@timgit friendly ping.

StarpTech avatar Sep 06 '23 18:09 StarpTech

Hold off on the PR. v10 will include this

timgit avatar Sep 06 '23 19:09 timgit

@timgit thank you. Any eta?

StarpTech avatar Sep 06 '23 19:09 StarpTech

Interested in the beta?

timgit avatar Sep 06 '23 19:09 timgit

Sure

StarpTech avatar Sep 06 '23 20:09 StarpTech