【发布时间】:2021-06-13 05:16:09
【问题描述】:
每隔一段时间,Postgres 就会出错(底部的错误消息)。我不能强迫它崩溃,所以我无法重现它以找到错误的来源。我也在免费层,所以没有 Postgres 日志。
我正在 Heroku 测功机上使用 Bolt.js 运行 Slack 应用程序。
在 app.js 中我连接到数据库
const pg_client = new Client({
connectionString: process.env.DATABASE_URL,
ssl: {
rejectUnauthorized: false,
},
});
pg_client.connect();
然后我有一个通用查询函数,它接收客户端、查询和值
//Generic query function
let res;
try {
await pg_client.query("BEGIN");
try {
res = await pg_client.query(q, v);
await pg_client.query("COMMIT");
} catch (err) {
await pg_client.query("ROLLBACK");
throw err;
}
} finally {
//client.release()
}
return res;
}
module.exports = { query };
我刚刚意识到如果查询失败,我的 throw err; 行是否会导致 Postgres 崩溃?我应该只记录那个错误吗?
我是否需要在每次查询时释放我的客户端并重新启动它?我需要设置池或其他东西吗? Heroku dynos 每 24 小时重启一次,所以我的连接不会持续数周。
任何能指引我正确方向的帮助都会令人惊叹。谢谢!
错误信息是
2021-03-15T21:04:15.932529+00:00 app[web.1]: events.js:292
2021-03-15T21:04:15.932548+00:00 app[web.1]: throw er; // Unhandled 'error' event
2021-03-15T21:04:15.932549+00:00 app[web.1]: ^
2021-03-15T21:04:15.932549+00:00 app[web.1]:
2021-03-15T21:04:15.932552+00:00 app[web.1]: error: terminating connection due to administrator command
2021-03-15T21:04:15.932554+00:00 app[web.1]: at Parser.parseErrorMessage (/app/node_modules/pg-protocol/dist/parser.js:278:15)
2021-03-15T21:04:15.932554+00:00 app[web.1]: at Parser.handlePacket (/app/node_modules/pg-protocol/dist/parser.js:126:29)
2021-03-15T21:04:15.932555+00:00 app[web.1]: at Parser.parse (/app/node_modules/pg-protocol/dist/parser.js:39:38)
2021-03-15T21:04:15.932556+00:00 app[web.1]: at TLSSocket.<anonymous> (/app/node_modules/pg-protocol/dist/index.js:10:42)
2021-03-15T21:04:15.932556+00:00 app[web.1]: at TLSSocket.emit (events.js:315:20)
2021-03-15T21:04:15.932557+00:00 app[web.1]: at addChunk (internal/streams/readable.js:309:12)
2021-03-15T21:04:15.932557+00:00 app[web.1]: at readableAddChunk (internal/streams/readable.js:284:9)
2021-03-15T21:04:15.932558+00:00 app[web.1]: at TLSSocket.Readable.push (internal/streams/readable.js:223:10)
2021-03-15T21:04:15.932558+00:00 app[web.1]: at TLSWrap.onStreamRead (internal/stream_base_commons.js:188:23)
2021-03-15T21:04:15.932559+00:00 app[web.1]: Emitted 'error' event on Client instance at:
2021-03-15T21:04:15.932559+00:00 app[web.1]: at Client._handleErrorEvent (/app/node_modules/pg/lib/client.js:319:10)
2021-03-15T21:04:15.932559+00:00 app[web.1]: at Client._handleErrorMessage (/app/node_modules/pg/lib/client.js:330:12)
2021-03-15T21:04:15.932560+00:00 app[web.1]: at Connection.emit (events.js:315:20)
2021-03-15T21:04:15.932560+00:00 app[web.1]: at /app/node_modules/pg/lib/connection.js:115:12
2021-03-15T21:04:15.932561+00:00 app[web.1]: at Parser.parse (/app/node_modules/pg-protocol/dist/parser.js:40:17)
2021-03-15T21:04:15.932561+00:00 app[web.1]: at TLSSocket.<anonymous> (/app/node_modules/pg-protocol/dist/index.js:10:42)
2021-03-15T21:04:15.932562+00:00 app[web.1]: [... lines matching original stack trace ...]
2021-03-15T21:04:15.932562+00:00 app[web.1]: at TLSSocket.Readable.push (internal/streams/readable.js:223:10) {
2021-03-15T21:04:15.932563+00:00 app[web.1]: length: 116,
2021-03-15T21:04:15.932563+00:00 app[web.1]: severity: 'FATAL',
2021-03-15T21:04:15.932564+00:00 app[web.1]: code: '57P01',
2021-03-15T21:04:15.932564+00:00 app[web.1]: detail: undefined,
2021-03-15T21:04:15.932565+00:00 app[web.1]: hint: undefined,
2021-03-15T21:04:15.932565+00:00 app[web.1]: position: undefined,
2021-03-15T21:04:15.932566+00:00 app[web.1]: internalPosition: undefined,
2021-03-15T21:04:15.932566+00:00 app[web.1]: internalQuery: undefined,
2021-03-15T21:04:15.932566+00:00 app[web.1]: where: undefined,
2021-03-15T21:04:15.932567+00:00 app[web.1]: schema: undefined,
2021-03-15T21:04:15.932567+00:00 app[web.1]: table: undefined,
2021-03-15T21:04:15.932567+00:00 app[web.1]: column: undefined,
2021-03-15T21:04:15.932568+00:00 app[web.1]: dataType: undefined,
2021-03-15T21:04:15.932568+00:00 app[web.1]: constraint: undefined,
2021-03-15T21:04:15.932569+00:00 app[web.1]: file: 'postgres.c',
2021-03-15T21:04:15.932569+00:00 app[web.1]: line: '3023',
2021-03-15T21:04:15.932570+00:00 app[web.1]: routine: 'ProcessInterrupts'
2021-03-15T21:04:15.932570+00:00 app[web.1]: }
2021-03-15T21:04:16.359776+00:00 heroku[web.1]: Process exited with status 1
2021-03-15T21:04:16.449742+00:00 heroku[web.1]: State changed from up to crashed
2021-03-15T21:04:16.501439+00:00 heroku[web.1]: State changed from crashed to starting
2021-03-15T21:04:20.383409+00:00 heroku[web.1]: Starting process with command `node app.js`
2021-03-15T21:04:23.522205+00:00 app[web.1]: ⚡️ Bolt app is running!
2021-03-15T21:04:24.872264+00:00 heroku[web.1]: State changed from starting to up
2021-03-15T21:06:30.856589+00:00 app[web.1]: (node:4) UnhandledPromiseRejectionWarning: Error: connect ETIMEDOUT 10.152.19.239:5432
2021-03-15T21:06:30.856610+00:00 app[web.1]: at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)
2021-03-15T21:06:30.856611+00:00 app[web.1]: (Use `node --trace-warnings ...` to show where the warning was created)
2021-03-15T21:06:30.857096+00:00 app[web.1]: (node:4) UnhandledPromiseRejectionWarning
【问题讨论】:
标签: node.js postgresql heroku