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
Finn
Finn3y ago
any errors in your logs?
Birdzai
BirdzaiOP3y ago
[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.
Finn
Finn3y ago
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
Birdzai
BirdzaiOP3y ago
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.
Finn
Finn3y ago
does your app ever start? what does this page look like?
Birdzai
BirdzaiOP3y ago
Looks fine to me
Birdzai
BirdzaiOP3y ago
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
Finn
Finn3y ago
yea. it will be an error in your code though just as an fyi. railway has no concept or sleeping or waht not
Birdzai
BirdzaiOP3y ago
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.
Finn
Finn3y ago
👍
Rumon
Rumon3y ago
I know what's going on. Wait lemme share an SS from another community.
Finn
Finn3y ago
oo
Rumon
Rumon3y ago
Rumon
Rumon3y ago
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.
Finn
Finn3y ago
yea idk why the lib your using closes the connection to the db. strange.
Finn
Finn3y ago
ohh, very interesting
angelo
angelo3y ago
Yep, Strapi for some reason closes the connection to PG.
Birdzai
BirdzaiOP3y ago
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?
Rumon
Rumon3y ago
You'll get the exact same behavior if you use Easypanel. Not sure why this happens so inconsistently across various platforms.
Birdzai
BirdzaiOP3y ago
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:
✋ CAUTION

When using Docker, change the pool min value to 0 as Docker will kill any idle connections, making it impossible to keep any open connections to the database (see Tarn.js's pool (opens new window)settings used by Knex.js for more information).
✋ CAUTION

When using Docker, change the pool min value to 0 as Docker will kill any idle connections, making it impossible to keep any open connections to the database (see Tarn.js's pool (opens new window)settings used by Knex.js for more information).
So that should solve my issue.
angelo
angelo3y ago
This is fantastic and I will see if we can incorporate this into our starter info.
nebula
nebula3y ago
yo this might be a one and done fix for strapi on railway it’s been broken forever
Want results from more Discord servers?
Add your server