breejs / bree

Bree is a Node.js and JavaScript job task scheduler with worker threads, cron, Date, and human syntax. Built for @ladjs, @forwardemail, @spamscanner, @cabinjs.
https://jobscheduler.net
MIT License
2.96k stars 80 forks source link

[fix] Can't query database (postgresql) in worker #191

Closed pencilcheck closed 2 years ago

pencilcheck commented 2 years ago

Describe the bug

Node.js version: v14.18.3

OS version: Mac OSX 12.0.1

Description: Worker can't query database connection, it seems to get stuck and never move to the next line.

I uses "pg" package from node, and connects to the database and try to run .query like some basic select statement.

Somehow the code always get stuck at the query statement.

A separate side note: If I use pooling from "pg" to connect, the CPU of the node running bree worker will goes up to 100%, not sure why.

Actual behavior

ℹ  info      Worker for job "geocoding" online
⚠  warning   Job "geocoding" is already running
⚠  warning   Job "geocoding" is already running
⚠  warning   Job "geocoding" is already running
⚠  warning   Job "geocoding" is already running
⚠  warning   Job "geocoding" is already running

Expected behavior

ℹ  info      Worker for job "geocoding" online
checkpoint 2
checkpoint 3
checkpoint 4

Code to reproduce

https://gist.github.com/pencilcheck/758e36120f6a106328698379682c828a

Checklist

titanism commented 2 years ago

First, you should not be including a require call inside nested code. Require calls should be top-level imports or requires.

This code const { Client } = require("pg").native; should be moved to the top of the file.

Secondly, you did not read the pg documentation at https://node-postgres.com/features/connecting.

You need to include inside your code await client.connect().

Please read the documentation for the package you are using before filing an issue in a completely different dependency.

This code would not even work if you included it as a simple app and ran it with node app.js (without any of the work thread stuff).

pencilcheck commented 2 years ago

Ah, the real reason is because of the pg version I used is not compatible with the node version 14 https://node-postgres.com/#version-compatibility