Server Error after period of Inactivity
After a period of inactivity, a request to my node.js app always returns a server error. Then all subsequent activity works fine until after the next period of inactivity, which results in the same behavior. It's almost like the app has spun down and it needs that first request to spin up again (a la Heroku) but I thought this didn't happen on this platform. Can anyone shed any light on this for me?
23 Replies
any errors in your logs?
[2022-09-19 18:24:49.577] debug: ⛔️ Server wasn't able to start properly.
[2022-09-19 18:24:49.578] error: select "t0"."id" from "admin_users" as "t0" limit $1 - Connection terminated unexpectedly
Error: select "t0"."id" from "admin_users" as "t0" limit $1 - Connection terminated unexpectedly
at Connection.<anonymous> (/app/node_modules/pg/lib/client.js:132:73)
at Object.onceWrapper (node:events:627:28)
at Connection.emit (node:events:513:28)
at Connection.emit (node:domain:489:12)
at Socket.<anonymous> (/app/node_modules/pg/lib/connection.js:57:12)
at Socket.emit (node:events:525:35)
at Socket.emit (node:domain:489:12)
at TCP.<anonymous> (node:net:757:14)
error Command failed with exit code 1.
yea that will be somthing wrong with your app that kills your webserver but not the proccess ig.
oh wait it says it wasnt able to start properly?
either way
The same git deployment works fine on Heroku and Digital Ocean (I'm evaluating deployment platforms). It's only a problem here on Railway
And works fine immediately on the second request.
does your app ever start? what does this page look like?
Looks fine to me
The app is running fine now, I just get that server error the first time i hit it after inactivity. Doing a browser refresh and everything is then fine
yea. it will be an error in your code though just as an fyi. railway has no concept or sleeping or waht not
it's an open source headless CMS project (Strapi). It's been working fine on other PaaS offerings. I doubt it's a coding error in light of that.
Thanks anyway for commenting.
👍
I know what's going on. Wait lemme share an SS from another community.
oo
And this specially happens with PostgreSQL.
There are 2 solutions:
1. Setup some kind of monitoring tool which would send an HTTP request to your app every minute. This would essentially prevent the app from closing the connection with the database. (This is what I picked for my app hosted using Easypanel on DigitalOcean)
2. Use
pgbouncer
.yea idk why the lib your using closes the connection to the db. strange.
My best guess is it doesn't keep the connection alive. Ref: https://github.com/brianc/node-postgres/blob/3e53d06cd891797469ebdd2f8a669183ba6224f6/packages/pg/lib/client.js#L51
ohh, very interesting
Yep, Strapi for some reason closes the connection to PG.
Thanks everyone....this may indeed be the reason for the server error, but I'm still confused as to why the same github deploy works on other PaaS platforms (Heroku and Digital Ocean). Shouldn't I also be seeing the same behavior there as well?
Hmmm...never heard of Easypanel before....interesting. I'm using something similar (Dokku) on Digital Ocean...no problems with Postgres connections over there.
OK, I think I have a solution, which I'm posting here if anyone should encounter the same issue re Strapi. Turns out that for Docker-based deployments I have to change a DB connection pool setting. The Strapi docs state the following:
So that should solve my issue.
This is fantastic and I will see if we can incorporate this into our starter info.
yo this might be a one and done fix for strapi on railway
it’s been broken forever