Heroku nodejs app gets 503 error when connecting to heroku-redis - node.js

I have a nodejs API app running on heroku and i get a service unavailable 503 error when this app tries to connect to heroku-redis or another redis server on redislabs.com (I tried both redis servers to see if it'd make a difference). I have a free heroku dev account being used for this nodejs API app and the heroku-redis add-on. Perhaps I need a static IP for the free heroku app to connect to heroku-redis? Not sure.
This is what i see when i look at my app's logs on heroku:
2022-02-05T18:30:24.003600+00:00 app[web.1]: > start
2022-02-05T18:30:24.003601+00:00 app[web.1]: > node app.js
2022-02-05T18:30:24.003601+00:00 app[web.1]:
2022-02-05T18:30:26.254765+00:00 heroku[web.1]: State changed from starting to up
2022-02-05T18:31:03.724181+00:00 app[web.1]: Starting RXNsetup app
2022-02-05T18:31:03.725096+00:00 app[web.1]: Start Redis connection
2022-02-05T18:31:33.716830+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=POST path="/run" host=rxnsetup.herokuapp.com request_id=c983d257-8773-4fff-a4d1-4b74341366b6 fwd="190.111.231.94" dyno=web.1 connect=0ms service=30176ms status=503 bytes=0 protocol=https
2022-02-05T18:30:00.000000+00:00 app[heroku-redis]: source=HEROKU_REDIS_CRIMSON addon=redis-symmetrical-62029 sample#active-connections=1 sample#load-avg-1m=1.105 sample#load-avg-5m=0.96 sample#load-avg-15m=0.6 sample#read-iops=0 sample#write-iops=0 sample#memory-total=15619136kB sample#memory-free=10625808kB sample#memory-cached=2390936kB sample#memory-redis=333744bytes sample#hit-rate=1 sample#evicted-keys=0
The first log statement above is in reference to another nodejs app that makes a POST HTTP request to the nodejs API app being run on heroku. I am able to connect to my redis server fine when i hit this same nodejs API app locally, but for some reason I get a 503 error when the same nodejs app runs in heroku. I have configured all of the same environment variables in heroku like i've done locally. I've been researching for awhile but am stuck with what could be causing this 503 error.
Update:
The root issue looked to be first that I had multiple instances of heroku-redis running, so I needed to promote one instance to be the primary one. Then another issue was that the REDIS_URL environment variable was being evaluated to undefined at runtime which was causing the 503 service errors I was getting.

I suspect you're looking at a stale connection string.
HEROKU_REDIS_<color> variables are normally only used if you have multiple deployments:
Heroku creates the REDIS_URL config var to store the location of the primary instance. In single-instance setups, your new instance is assigned a REDIS_URL. In cases where REDIS_URL exists, your instance is assigned a HEROKU_REDIS_<color> URL instead.
Normally, even if you have multiple instances, you would still connect to REDIS_URL, not HEROKU_REDIS_<color>. You can promote a HEROKU_REDIS_<color> URL to REDIS_URL by running
heroku redis:promote HEROKU_REDIS_<color>
Update your application to use REDIS_URL, review your addons to see if you have multiple Redis services provisioned, and decide which one(s) to keep. Then promote its variable to REDIS_URL as above.

Related

Node Express server works in Heroku staging pipeline but not on production pipeline (no such app error)

I developed an api with nodejs, express and run (npm start) with pm2 (link). I published the app on heroku in a pipeline, I use the staging and production pipeline. The staging app works fine, but the production app is very inconsistent, succeeding sometimes and failing most.
Below the api access log, in the second request there is an error 404 with "no such app" in the html body error message (it is not in the image log).
2021-11-22T12:52:37.266486+00:00 app[web.1]: Starting server in port 12080
2021-11-22T12:52:37.567394+00:00 heroku[router]: at=info method=POST path="/token" host={hidden}-production.herokuapp.com request_id={hidden} fwd="{hidden}" dyno=web.1ect=0ms service=692ms status=200 bytes=613 protocol=https
2021-11-22T12:53:12.688593+00:00 heroku[router]: at=info method=POST path="/token" host={hidden}-production.herokuapp.com request_id={hidden} fwd="{hidden}" dyno=web.1 connect=0ms service=35ms status=404 bytes=711 protocol=https
I believe it is not a route problem, it works sometimes and in staging it works fine. I'm using Heroku's promotion button (staging -> production).
Is there a problem with Heroku?
Are any different configurations required for the application in production?
The problem was in my app syncing with the Heroku routers.
Was fixed by the Heroku team itself.

How to make my app on Heroku stop timing out

I made a simple financial app consisting of a React frontend and Node/express backend that I want to deploy to Heroku. Since it's a small app I put everything (server as well as client files) in the root directory of my git repository. Thus the entry files for frontend (index.html), backend (index.js) and the Procfile required by Heroku are all in the root directory.
The server seems to be working fine, since I can successfully make requests from the locally installed frontend. However it appears that the server cannot serve the static files.
This is the log message from heroku:
2021-07-29T15:21:00.260957+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=GET path="/" host=xpensoft.herokuapp.com request_id=2a484f4d-30cf-49b9-920d-14b9e9be28fb fwd="31.151.16.151" dyno=web.1 connect=0ms service=30000ms status=503 bytes=0 protocol=https
I am serving the static files using this server code:
if (process.env.NODE_ENV === 'production') {
app.use(express.static(__dirname));
}
Anyone has any idea what could be causing the timeout?
EDIT:
Apart from the code above, which is supposed to serve up the static files (which is unfortunately doesn't) there is no specific code in the server file handling the '/' endpoint. There is only code for specific named endpoints.
In the React app I am redirecting the user using React Router:
<Route exact path='/'>
<Redirect to='/user' />
</Route>
However the React app is never loaded.
request timeout error comes when your app takes more than 30 seconds to send a response back to the client.
So there is some issue with your code which handles the '/' route.
if Your static files are facing issues to get served up, it's most probably the issues with .env file. So manually add the enviorment variables in Heroku Settings > Config Vars

Does the free tier of Heroku apps support file upload?

I am testing out a MEAN stack application. I have a Node.js backend which handles file upload (via fs/multer). However I'm receiving a 503 error when trying to hit it in production.
Locally everything is working fine. I am using Heroku's free tier; is that tier locked to not allow file upload?
The upload I'm trying currently (have tried several) is only 36kb (.png).
Went through my logs and found the 503-
2018-10-26T20:39:24.352297+00:00 heroku[router]: at=error code=H13 desc="Connection closed without response" method=POST path="/media/upload" host=www.example.co.uk request_id=5388711c-bc99-4e42-9dbd-7b645fbefb43 fwd="90.222.69.255" dyno=web.1 connect=1ms service=130ms status=503 bytes=0 protocol=http
2018-10-26T20:39:24.306975+00:00 app[web.1]: (node:20) [DEP0013] DeprecationWarning: Calling an asynchronous function without callback is deprecated.
2018-10-26T20:39:24.307037+00:00 app[web.1]: (node:20) [DEP0013] DeprecationWarning: Calling an asynchronous function without callback is deprecated.
2018-10-26T20:39:24.318818+00:00 app[web.1]: fs.js:113
2018-10-26T20:39:24.318822+00:00 app[web.1]: throw err; // Forgot a callback but don't know where? Use NODE_DEBUG=fs
2018-10-26T20:39:24.318824+00:00 app[web.1]: ^
2018-10-26T20:39:24.318826+00:00 app[web.1]:
2018-10-26T20:39:24.318828+00:00 app[web.1]: Error: ENOENT: no such file or directory, mkdir './uploads/18'
File uploads shouldn't generate HTTP 503 on their own. You should check your logs (via heroku logs) to get more information about what's failing. Having said that, even if you resolve this issue you'll probably need to make a fundamental change with the way you're handling file uploads.
All Heroku tiers have an ephemeral filesystem. Any changes made to the filesystem are lost whenever your dyno restarts, which happens frequently (at least once per day).
The official recommendation is to put uploaded files onto a third-party service like Amazon S3. The multer-s3 library should be able to help with that.

How do I deploy an Angular.js app?

This may be a stupid question, but I'm wondering what platform I should use to deploy my Angular.js app? I tried using Heroku but got a "no Cedar-supported app detected" error. Any thoughts?
UPDATE
I'm trying to serve this to heorku as a Node.js application, but still can't seem to get it working. There's a web-server.js file in the root directory of my application and I reference it in my Procfile here:
web: node web-server.js
And here's my package.json file, also in the root directory:
{
"name": "medicare-data",
"version": "0.0.1",
"dependencies": {
},
"engines": {
"node": "0.10.x",
"npm": "1.2.x"
},
"respository": "medicare-data",
"author": "sararob"
}
I'm getting a "slug compilation error" in my heroku logs. Thoughts on why this might not be working?
You can run an Angular app on heroku if it is served as a node.js application. Usually, when you start working with Angular seed, there is already a node webserver script in the project. I believe if there is a package.json file in the root, heroku will detect and run it as a node.js application. I'll come back and update this answer with more detail shortly.
UPDATE
From the Heroku docs (and I am no expert), it has this passage after the deploy section: https://devcenter.heroku.com/articles/nodejs#visit-your-application
which basically tells you to make sure you have one dyno running. You can do that either through the web app under the settings of your app or you can run this:
$ heroku ps:scale web=1
To see if the process is running, do
$ heroku ps
=== web: `node web.js`
web.1: up for 10s
I bet if you look at your logs you'll see something like this:
2013-08-26T12:51:18.257006+00:00 heroku[web.1]: Starting process with command `node scripts/web-server.js`
2013-08-26T12:51:19.109974+00:00 app[web.1]: Http Server running at http://localhost:8000/
2013-08-26T12:51:34.369092+00:00 heroku[router]: at=error code=H20 desc="App boot timeout" method=GET path=/ host=cmwidget.herokuapp.com fwd="77.101.66.230" dyno= connect= service= status=503 bytes=
2013-08-26T12:52:19.445974+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
It means that your Angular seed app is using it's own port (8000), and Heroku doesn't allow this. You need to relinquish the assignment of the port number to Heroku.
Easily fixed. Modify scripts/web-server.js, and change this:
this.server.listen(port);
...to this:
this.server.listen(process.env.PORT || port);
I changed this line
var DEFAULT_PORT = 8000;
into:
var DEFAULT_PORT = process.env.PORT || 8000;
..and then it worked.
So I believe the reason is that heroku doesn't like your app running on port 8000.

Weinre on heroku

I am trying to set up Weinre on heroku but I am having some issues.
I manage to build the app, simply by using a package.json with weinre npm dependency and a Procfile that looks like the following
web: node_modules/weinre/weinre --httpPort $PORT
At first everything seems to work, the client shows up the UI, the JS file loads fine, but the debug does not work and no client ever shows up at the server side.
While looking at the logs, I realized that I keep getting 404 errors for every request that contains /ws/target/* or /ws/client/*
2012-05-31T22:37:53+00:00 heroku[router]: GET xxxx.herokuapp.com/target/target-script.js dyno=web.1 queue=0 wait=0ms service=10ms status=200 bytes=190900
2012-05-31T22:37:36+00:00 heroku[router]: GET xxxx.herokuapp.com/ws/target/t-3 dyno=web.1 queue=0 wait=0ms service=3ms status=404 bytes=9
2012-05-31T22:36:39+00:00 heroku[router]: POST xxxx.herokuapp.com/ws/client/c-1 dyno=web.1 queue=0 wait=0ms service=40ms status=404 bytes=9
This setup works localy with no problem.
I also tried the Procilfe with --boundHost 0.0.0.0 and got the same error. when I use my heroku app domain/subdomain as the host I got weinre: error running server: Error: listen EADDRNOTAVAIL error
If needed, you can checkout the source at the github repo
https://github.com/felipesabino/weinre-heroku
EDIT:
It is possible that it is related to WebSockets not being supported on Heroku:
https://devcenter.heroku.com/articles/http-routing#websockets
The weirdest thing is that the same issue happens with PhoneGap's weinre debug page as well
http://debug.phonegap.com/
Does anybody know how to solve this?
I have weinre running on Heroku at the moment but I remember that when I was trying to get it working it was a real nightmare.
This was using the 2.0.0-pre-H0WVARLU-incubating of weinre so it may be a bit different but what I had to do was:
Modify weinre itself, I had to tell weinre to bind to all hosts, I modified the cli.coffee file and I set the boundHost to -all- as the default
I commended out the if remoteAddress check in channelManager.coffee since the address you come in from is remapped through the proxy and was never matching
Turn on verbose debugging, while you don't actually need this to run it made it a lot easier to trace where errors are happening (and why the requests weren't routing)
My Procfile ended up being very simple as I tried to use weinre as "native" as possible (note the repo contains only weinre, it's not a module like you're doing):
web: node weinre
I left any port changes to be handled by process.env.PORT, not passed in from the Procfile
Basically what I found was that weinre really isn't designed to work behind a proxy!
See this pull request https://github.com/apache/incubator-cordova-weinre/pull/10
Note that no other modifications are needed, Procfile can set the options (like it should) and Weinre is properly used as node module.
package.json (that contains the fix)
"dependencies" : {
"weinre": "https://github.com/downloads/AppGyver/incubator-cordova-weinre/apache-cordova-weinre-2.0.0-pre-H77LBWIW-incubating-bin.tar.gz"
},
Procfile
web: node_modules/weinre/weinre --httpPort $PORT --boundHost -all- --debug true --verbose true
Modify weinre itself, I had to tell weinre to bind to all hosts, I modified the cli.coffee file and I set the boundHost to -all- as the default
I commended out the if remoteAddress check in channelManager.coffee since the address you come in from is remapped through the proxy and was never matching
this two Hints helped a lot on appfog, too
Also you need to
rename weinre to app.js
require local coffeescript
assign cli to a variable and execute run direct
extend cli.coffee - > Port with process.env.PORT

Resources