mgcrea / prisma-queue

Minimalist postgresql job queue for Prisma
MIT License
46 stars 4 forks source link

Infinite Loop #4

Open nahtnam opened 3 months ago

nahtnam commented 3 months ago

Hey! My project uses prisma and this looked pretty convenient so I wanted to give it a shot.

I'm facing an issue where the worker is getting stuck in an infinite loop. As soon as I add a job, it prints out an infinite amount of:

2024-06-14T06:50:14.123Z prisma-queue processing job from queue named="email"...
2024-06-14T06:50:14.123Z prisma-queue dequeuing from queue named="email"...
2024-06-14T06:50:14.124Z prisma-queue no jobs found in queue named="email"
2024-06-14T06:50:14.174Z prisma-queue processing job from queue named="email"...
2024-06-14T06:50:14.174Z prisma-queue dequeuing from queue named="email"...
2024-06-14T06:50:14.175Z prisma-queue no jobs found in queue named="email"

I noticed that in the DB, the job is not being removed. NOTE: I'm not awaiting the .start()

Code:

export const emailQueue = createQueue<JobPayload, JobResult>(
  { name: 'email' },
  async (job, client) => {
    const { id, payload } = job;
    console.log(
      `Processing job#${id} with payload=${JSON.stringify(payload)})`,
    );
    // await someAsyncMethod();
    await job.progress(50);
    const status = 200;
    if (Math.random() > 0.5) {
      throw new Error(`Failed for some unknown reason`);
    }
    console.log(`Finished job#${id} with status=${status}`);
    return { status };
  },
);

void emailQueue.start();

My use case is I'm running Next.js in a container and just want this to run in the background. So I just put the void emailQueue.start() so that it does it in the background. This strategy seems to work with pgboss

mgcrea commented 3 months ago

When using pgboss do you await boss.work()? Looks like the process is killed and respawned very quickly and do not have time to actually process the job, you should await queue.start() when using prisma queue (which is basically the equivalent of boss.work).

nahtnam commented 3 months ago

No, I wrote the code in the same way with void in the top level

nahtnam commented 3 months ago

Got it to work!

async function init() {
  await emailQueue.start();
}

void init()

Do you foresee any problems with this?

EDIT: nvm, still seems to infinite loop