timgit / pg-boss

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

.work() seems to return and not run infinitely #465

Closed aryamannaik closed 2 months ago

aryamannaik commented 3 months ago

I'm trying to follow the README example in typescript. I have pg-boss 9.0.3 installed via yarn add pg-boss.

My goal is to simply queue a job, and then process this via work. My expectation is that once I call work it will process jobs infinitely.

I'm running the following script:

async function doSomethingAsyncWithThis(data: any): Promise<void> {
  // Implement your function here
  await new Promise((resolve) => {
    resolve(data);
  });
}

async function someAsyncJobHandler(job: PgBoss.Job<any>): Promise<void> {
  console.log(`job ${job.id} received with data:`);
  console.log(JSON.stringify(job.data));

  await doSomethingAsyncWithThis(job.data);
}

async function readme(): Promise<void> {
  const boss: PgBoss = new PgBoss({
    host: "localhost",
    port: 5432,
    user: "postgres",
    password: "postgres",
    database: "postgres",
  });

  boss.on("error", (error: Error) => console.error(error));

  await boss.start();

  const queue: string = "some-queue";

  const jobId = await boss.send(queue, { param1: "foo" });

  console.log(`created job in queue ${queue}: ${jobId}`);

  await boss.work(queue, someAsyncJobHandler);
}

await readme();

I'm also able to verify in my local PG that this job is sent in the job table.

However, my script returns immediately, and I don't see any of the output from the someAsyncJobHandler function.

Am I misunderstanding how the work function is supposed to work?

Adds a new polling worker for a queue and executes the provided callback function when jobs are found. Multiple workers can be added if needed.

My read from this was that the worker would poll infinitely.

My higher level goal here is to use the work function in my worker service threads that will process jobs send to pg-boss via send from the application code that handles user requests. Is my understanding here incorrect, or is this a bug?

timgit commented 3 months ago

You will need to ensure your node process does not exit, in a similar way you would keep a HTTP listener running for an API

aryamannaik commented 2 months ago

Thanks @timgit. Would it be worth updating the README to have a complete example here?

Another option would be to provide an API that runs work and does not exit. See graphile-worker's implementation here for some inspo!