Why does the FastAPI startup event run twice in HEROKU? - heroku

When I deployed a FastAPI app to heroku
the startup event is fired twice.
Is it my problem?
The code below is not my original app, but a temporary app I made for testing.
And the Heroku was also made fresh for testing.
# Heroku logs
2022-10-18T08:37:32 app[api]: Build started by user junah.dev+1#gmail.com
2022-10-18T08:37:49 app[api]: Deploy 298098b0 by user junah.dev+1#gmail.com
2022-10-18T08:37:49 app[api]: Release v6 created by user junah.dev+1#gmail.com
2022-10-18T08:37:52 heroku[web.1]: State changed from down to starting
2022-10-18T08:37:54 heroku[web.1]: Starting process with command `uvicorn app.main:app --host=0.0.0.0 --port=${PORT:-5000}`
2022-10-18T08:37:55 app[api]: Build succeeded
2022-10-18T08:37:55.490285+00:00 app[web.1]: INFO: Uvicorn running on http://0.0.0.0:51121 (Press CTRL+C to quit)
2022-10-18T08:37:55 app[web.1]: INFO: Started parent process [4]
2022-10-18T08:37:55 app[web.1]: INFO: Started server process [11]
2022-10-18T08:37:55 app[web.1]: INFO: Waiting for application startup.
2022-10-18T08:37:55 app[web.1]: INFO: Application startup complete.
2022-10-18T08:37:55 app[web.1]: INFO: Started server process [10]
2022-10-18T08:37:55 app[web.1]: INFO: Waiting for application startup.
2022-10-18T08:37:55 app[web.1]: INFO: Application startup complete.
2022-10-18T08:37:56 heroku[web.1]: State changed from starting to up
# main.py
from fastapi import Depends, FastAPI
import uvicorn
app = FastAPI()
#app.get("/")
async def root():
return {"message": "Hello World"}
# Procfile
web: uvicorn app.main:app --host=0.0.0.0 --port=${PORT:-5000}

Uvicorn operates by running worker processes.
If you don't give it a number of processes to use via the --workers argument, it defaults to the value of the WEB_CONCURRENCY environment variable, falling back to 1 if neither is set:
--workers INTEGER Number of worker processes. Defaults to the
$WEB_CONCURRENCY environment variable if
available, or 1. Not valid with --reload.
Heroku sets a default value for the WEB_CONCURRENCY environment variable:
The WEB_CONCURRENCY environment variable is automatically set by Heroku, based on the processes’ Dyno size. This feature is intended to be a sane starting point for your application. We recommend knowing the memory requirements of your processes and setting this configuration variable accordingly.
Heroku has probably set this value to 2 based on the size of the dynos you are running. This would cause two workers to run in parallel, which is what your logs are showing:
A parent process: Started parent process [4]
And two worker processes:
Started server process [11]
Started server process [10]
You can change the value of WEB_CONCURRENCY if you wish.
If your code is malfunctioning with multiple workers that indicates a problem with your code. In that case, instead of setting this to 1, I suggest you identify why it is misbehaving and fix the problem. One common cause of this is use of global variables.

Related

State Changed from up to Crashed ( Discord App Won't Start ) , and yes i use a start script and everything is right

2022-05-27T00:41:10.751190+00:00 heroku[worker.1]: State changed from up to crashed
2022-05-27T00:41:10.761682+00:00 heroku[worker.1]: State changed from crashed to starting
2022-05-27T00:41:12.597341+00:00 heroku[worker.1]: Starting process with command : node .
2022-05-27T00:41:13.215607+00:00 heroku[worker.1]: State changed from starting to up
2022-05-27T00:41:13.579455+00:00 heroku[worker.1]: Process exited with status 0
2022-05-27T00:41:13.649880+00:00 heroku[worker.1]: State changed from up to crashed
2022-05-27T00:41:45.366307+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2022-05-27T00:41:45.457887+00:00 heroku[web.1]: Stopping process with SIGKILL
2022-05-27T00:41:45.474285+00:00 app[web.1]: Error waiting for process to terminate: No child processes
2022-05-27T00:41:45.610972+00:00 heroku[web.1]: Process exited with status 22
For python,
Simply, use gunicorn to easen the burden.
Within the project directory, with the virtual environment activated, install gunicorn as follows:
pip install gunicorn
If you're using pipenv you can try:
pipenv install gunicorn
Update the requirements.txt file to include the new installed gunicorn module by running:
pip freeze > requirements.txt
Update the Procfile as follows:
web: gunicorn your_django_project_name.wsgi --log-file -
N.B:
There should be space between the web: and gunicorn as well as between --log-file and the - next to it.
Lastly, add, commit and push the changes
For js devs,
Make sure the Procfile looks like:
web: npm start
THEN
try this solution here

How to run a node app with no listening port in Heroku

I have a node app that does a simple web scraping in real time.
Since it has no back-end feature, it doesn't listen any port and never use Express.
When I deploy this app, Heroku automatically runs some commands to build and run my app.
This is my package.json file.
"scripts": {
"develop": "nodemon --exec babel-node src",
"test": "mocha --require babel-core/register",
"build": "rimraf lib && babel src -d lib",
"start": "node lib",
"lint": "eslint src"
}
I checked the Heroku logs to see if the app is running properly but I found errors like below.
...
2020-12-27T07:24:48.000000+00:00 app[api]: Build succeeded
2020-12-27T07:25:46.564866+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2020-12-27T07:25:46.589773+00:00 heroku[web.1]: Stopping process with SIGKILL
2020-12-27T07:25:46.680704+00:00 heroku[web.1]: Process exited with status 137
2020-12-27T07:25:46.732427+00:00 heroku[web.1]: State changed from starting to crashed
2020-12-27T07:25:46.735872+00:00 heroku[web.1]: State changed from crashed to starting
2020-12-27T07:25:51.844095+00:00 heroku[web.1]: Starting process with command `npm start`
2020-12-27T07:25:54.728760+00:00 app[web.1]:
2020-12-27T07:25:54.728776+00:00 app[web.1]: > my-node-app#0.1.0 start /app
2020-12-27T07:25:54.728777+00:00 app[web.1]: > node lib
2020-12-27T07:25:54.728777+00:00 app[web.1]:
2020-12-27T07:26:52.296540+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2020-12-27T07:26:52.315003+00:00 heroku[web.1]: Stopping process with SIGKILL
2020-12-27T07:26:52.410290+00:00 heroku[web.1]: Process exited with status 137
2020-12-27T07:26:52.468187+00:00 heroku[web.1]: State changed from starting to crashed
It seems building was successful but has failed when running it.
In my opinion, Heroku tried to bind PORT but my app never listened any port, so these errors had occurred.
How can I run my app successfully with no error on Heroku in this case?
If your node.js project is a web server, you cannot remove the port. No port, no web server. Trying to make a web server without a port is like trying to make a locomotive with no railroad.
You can use the default port however. When users give browsers URLs without ports, they automatically apply the default port.
Heroku also provide the default port so you don't have to specify the port
Just deploy your app and use the url provided by the heroku to call node js application

Why is Heroku not binding to port of Java HTTP server?

I have implemented a basic HTTP server in java which I am trying to deploy to Heroku. The server behaves as expected when run on localhost, but on Heroku I get the following error in my logs:
2018-12-18T09:03:28.323430+00:00 heroku[web.1]: State changed from crashed to starting
2018-12-18T09:03:32.152747+00:00 heroku[web.1]: Starting process with command `java -jar build/http-server.jar`
2018-12-18T09:03:34.496251+00:00 app[web.1]: Setting JAVA_TOOL_OPTIONS defaults based on dyno size. Custom settings will override them.
2018-12-18T09:03:34.517683+00:00 app[web.1]: Picked up JAVA_TOOL_OPTIONS: -XX:+UseContainerSupport -Xmx300m -Xss512k -XX:CICompilerCount=2 -Dfile.encoding=UTF-8
2018-12-18T09:05:02.493578+00:00 heroku[web.1]: State changed from starting to crashed
2018-12-18T09:05:02.344933+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 90 seconds of launch
2018-12-18T09:05:02.344933+00:00 heroku[web.1]: Stopping process with SIGKILL
2018-12-18T09:05:02.475449+00:00 heroku[web.1]: Process exited with status 137
I have set the PORT environment variable to 5000, which is what my server is currently binding to (yes, it's hard coded for now). If I change the server port to 80, for example, I can see in my logs that the server is unable to bind to that port, so I'm happy that it is binding to the correct port. My understanding of the docs is that I should just be able to set the port number that I need using the PORT environment variable.
Why, then, is Heroku unable to bind to this port? Is there some other action I should be taking, i.e. proxying from 443 to 5000?
I have set the PORT environment variable to 5000, which is what my server is currently binding to (yes, it's hard coded for now).
You've got this backwards.
The PORT environment variable is set by Heroku and your code must bind to that value. You can't hard-code the port, or choose it yourself. You must take it from the environment.

Heroku App Errors, Nothing in Heroku Logs

I'm getting "We're sorry, but something went wrong."
heroku logs turns up nothing useful from what I can see...
2014-10-06T20:48:04.846089+00:00 heroku[web.1]: State changed from up to starting
2014-10-06T20:48:06.519972+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2014-10-06T20:48:07.928530+00:00 app[web.1]: => Booting Thin
2014-10-06T20:48:07.928582+00:00 app[web.1]: => Rails 4.1.5 application starting in production on http://0.0.0.0:58305
2014-10-06T20:48:07.928584+00:00 app[web.1]: => Run `rails server -h` for more startup options
2014-10-06T20:48:07.928586+00:00 app[web.1]: => Notice: server is listening on all interfaces (0.0.0.0). Consider using 127.0.0.1 (--binding option)
2014-10-06T20:48:07.928587+00:00 app[web.1]: => Ctrl-C to shutdown server
2014-10-06T20:48:07.928589+00:00 app[web.1]: Thin web server (v1.6.2 codename Doc Brown)
2014-10-06T20:48:07.928590+00:00 app[web.1]: Maximum connections set to 1024
2014-10-06T20:48:07.928592+00:00 app[web.1]: Listening on 0.0.0.0:58305, CTRL+C to stop
2014-10-06T20:48:07.928593+00:00 app[web.1]: Exiting
2014-10-06T20:48:07.812908+00:00 heroku[web.1]: Starting process with command `bin/rails server -p 49372 -e production`
2014-10-06T20:48:08.659411+00:00 heroku[web.1]: Process exited with status 0
2014-10-06T20:48:11.589427+00:00 heroku[web.1]: State changed from starting to up
Things work fine locally.
Looks like the following got rid of the issue (i thought db migration would happen automatically)
heroku rake db:migrate

ruby app crashes on heroku: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch

I am trying to load a simple ruby app on heroku that periodically runs a background task using resque, which checks an email account. It works fine locally with foreman, but keeps crashing on heroku.
I think maybe the Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch comes from my while loop, which is perpetual and therefore longer than 60 seconds? Should I be using some different kind of process to launch smsnotify.rb?
Any help would be extremely appreciated!
My heroku logs:
2013-04-06T20:49:15+00:00 heroku[slugc]: Slug compilation finished
2013-04-06T20:49:18+00:00 heroku[web.1]: Starting process with command `bundle exec ruby smsnotify.rb -p 9129`
2013-04-06T20:49:21+00:00 app[web.1]: /app/vendor/bundle/ruby/1.9.1/gems/activesupport-3.0.0/lib/active_support/multibyte.rb:26: warning: already initialized constant VALID_CHARACTER
2013-04-06T20:50:19+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2013-04-06T20:50:19+00:00 heroku[web.1]: Stopping process with SIGKILL
2013-04-06T20:50:20+00:00 heroku[web.1]: Process exited with status 137
2013-04-06T20:50:20+00:00 heroku[web.1]: State changed from starting to crashed
My Procfile:
web: bundle exec ruby smsnotify.rb -p $PORT
resque: env TERM_CHILD=1 bundle exec rake jobs:work
smsnotify.rb:
require "./email_checker"
require 'hirefire'
require 'resque'
Resque.redis = 'redis://redistogo:removed:removed/'
HireFire::Initializer.initialize!
while true do
Resque.enqueue(EmailChecker)
sleep 30
puts "Starting Email Job"
end
You aren't starting up a process that is binding to a port, so it is complaining.
You just need to change your Procfile to say 'worker' instead of 'web'
If you have a web frontend to this, then you will need a worker and a web

Resources