Flask application unable to start on Azure, connection in use - azure

I tried to deploy very simple Flask application:
import flask
app = flask.Flask(__name__)
#app.route('/', methods=['GET'])
def home():
return '<h1>Hello!</h1></p>'
app.run()
I followed some official tutorials, created App Service in Azure Portal, deployed app using Local Git solution. Application is deployed, but when I try to browse it, I get:
:( Application Error. If you are the application administrator, you can access the diagnostic resources.
My logs:
2022-09-22T14:24:17.032341362Z
2022-09-22T14:24:17.032431764Z _____
2022-09-22T14:24:17.032440964Z / _ \ __________ _________ ____
2022-09-22T14:24:17.032445564Z / /_\ \___ / | \_ __ \_/ __ \
2022-09-22T14:24:17.032449964Z / | \/ /| | /| | \/\ ___/
2022-09-22T14:24:17.032454364Z \____|__ /_____ \____/ |__| \___ >
2022-09-22T14:24:17.032458965Z \/ \/ \/
2022-09-22T14:24:17.032463265Z
2022-09-22T14:24:17.032467265Z A P P S E R V I C E O N L I N U X
2022-09-22T14:24:17.032471165Z
2022-09-22T14:24:17.032474965Z Documentation: http://aka.ms/webapp-linux
2022-09-22T14:24:17.032489065Z Python 3.9.7
2022-09-22T14:24:17.032493765Z Note: Any data outside '/home' is not persisted
2022-09-22T14:24:17.180055832Z Starting OpenBSD Secure Shell server: sshd.
2022-09-22T14:24:17.215008000Z App Command Line not configured, will attempt auto-detect
2022-09-22T14:24:17.433349825Z Starting periodic command scheduler: cron.
2022-09-22T14:24:17.442192344Z Launching oryx with: create-script -appPath /home/site/wwwroot -output /opt/startup/startup.sh -virtualEnvName antenv -defaultApp /opt/defaultsite
2022-09-22T14:24:17.556237878Z Found build manifest file at '/home/site/wwwroot/oryx-manifest.toml'. Deserializing it...
2022-09-22T14:24:17.562671538Z Output is compressed. Extracting it...
2022-09-22T14:24:17.564241977Z Build Operation ID: |OgP/jQgIHgU=.1da790b7_
2022-09-22T14:24:17.564259877Z Oryx Version: 0.2.20220825.1, Commit: 24032445dbf7bf6ef068688f1b123a7144453b7f, ReleaseTagName: 20220825.1
2022-09-22T14:24:17.565391205Z Extracting '/home/site/wwwroot/output.tar.gz' to directory '/tmp/8da9ca604221f20'...
2022-09-22T14:24:18.725265722Z App path is set to '/tmp/8da9ca604221f20'
2022-09-22T14:24:19.018530608Z Detected an app based on Flask
2022-09-22T14:24:19.405035911Z Generating `gunicorn` command for 'index:app'
2022-09-22T14:24:19.456995902Z Writing output script to '/opt/startup/startup.sh'
2022-09-22T14:24:19.689506779Z Using packages from virtual environment antenv located at /tmp/8da9ca604221f20/antenv.
2022-09-22T14:24:19.690332099Z Updated PYTHONPATH to ':/opt/startup/app_logs:/opt/startup/code_profiler:/tmp/8da9ca604221f20/antenv/lib/python3.9/site-packages'
2022-09-22T14:24:20.556171311Z [2022-09-22 14:24:20 +0000] [77] [INFO] Starting gunicorn 20.1.0
2022-09-22T14:24:20.595370385Z [2022-09-22 14:24:20 +0000] [77] [INFO] Listening at: http://0.0.0.0:8000 (77)
2022-09-22T14:24:20.601700442Z [2022-09-22 14:24:20 +0000] [77] [INFO] Using worker: sync
2022-09-22T14:24:20.610324257Z [2022-09-22 14:24:20 +0000] [80] [INFO] Booting worker with pid: 80
2022-09-22T14:24:21.265168226Z * Serving Flask app 'index'
2022-09-22T14:24:21.266054348Z * Debug mode: on
2022-09-22T14:24:21.300157595Z [31m[1mWARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.[0m
2022-09-22T14:24:21.300272498Z * Running on http://127.0.0.1:5000
2022-09-22T14:24:21.301268023Z [33mPress CTRL+C to quit[0m
2022-09-22T14:24:21.308547204Z * Restarting with stat
2022-09-22T14:24:21.760007120Z [2022-09-22 14:24:21 +0000] [81] [INFO] Starting gunicorn 20.1.0
2022-09-22T14:24:21.772788238Z [2022-09-22 14:24:21 +0000] [81] [ERROR] Connection in use: ('0.0.0.0', 8000)
2022-09-22T14:24:21.773676260Z [2022-09-22 14:24:21 +0000] [81] [ERROR] Retrying in 1 second.
2022-09-22T14:24:22.779581652Z [2022-09-22 14:24:22 +0000] [81] [ERROR] Connection in use: ('0.0.0.0', 8000)
2022-09-22T14:24:22.780698179Z [2022-09-22 14:24:22 +0000] [81] [ERROR] Retrying in 1 second.
2022-09-22T14:24:23.781856453Z [2022-09-22 14:24:23 +0000] [81] [ERROR] Connection in use: ('0.0.0.0', 8000)
2022-09-22T14:24:23.783124685Z [2022-09-22 14:24:23 +0000] [81] [ERROR] Retrying in 1 second.
2022-09-22T14:24:24.787329534Z [2022-09-22 14:24:24 +0000] [81] [ERROR] Connection in use: ('0.0.0.0', 8000)
2022-09-22T14:24:24.788615966Z [2022-09-22 14:24:24 +0000] [81] [ERROR] Retrying in 1 second.
2022-09-22T14:24:25.792686075Z [2022-09-22 14:24:25 +0000] [81] [ERROR] Connection in use: ('0.0.0.0', 8000)
2022-09-22T14:24:25.794038102Z [2022-09-22 14:24:25 +0000] [81] [ERROR] Retrying in 1 second.
2022-09-22T14:24:26.795751866Z [2022-09-22 14:24:26 +0000] [81] [ERROR] Can't connect to ('0.0.0.0', 8000)
2022-09-22T14:24:26.839176431Z [2022-09-22 14:24:26 +0000] [80] [INFO] Worker exiting (pid: 80)
2022-09-22T14:24:26.928820618Z [2022-09-22 14:24:26 +0000] [84] [INFO] Booting worker with pid: 84
It happens all the time, starting, problem with port, restarting, again and again. As I said, I just followed tutorials, didn't change any port or IP address...

I found a solution. My code was development version - in logs was a warning:
WARNING: This is a development server. Do not use it in a production
deployment. Use a production WSGI server instead
I changed code as was answered in Flask at first run: Do not use the development server in a production environment :
import flask
app = flask.Flask(__name__)
#app.route('/', methods=['GET'])
def home():
return '<h1>Hello!</h1></p>'
# debug app:
# app.run()
if __name__ == "__main__":
from waitress import serve
serve(app, host="0.0.0.0", port=8080)
Now application is deployed to Azure and working properly.

Related

Gunicorn as Service not recognizing Python dependencies

I am running a Flask app on a Ubuntu Linode Server using Gunicorn to serve the app and nginx as a reverse proxy.
I have one app running successfully, but I have a second app where I'm running in to the following problem.
What does work
when I run FLASK_APP=tester.py python3 -m flask run
This works, tester.py runs the app bound to 0.0.0.0 on port 5100 and I can access it from my browser.
when I run gunicorn --bind 0.0.0.0:5100 wsgi:app
This also works, as my wsgi.py imports my app object making it accessible for wsgi
Useful details
which python gives me /usr/bin/python
which pip gives /usr/bin/pip and which pip3 gives /usr/bin/pip3
which python3 gives me /usr/bin/python3
which gunicorn gives me /usr/bin/gunicorn
I am not using a virtual environment since boths app have near identical dependencies
What's not working
I created a service for forwarding traffic with nginx with:
/etc/systemd/system/testpad.service
[Unit]
# specifies metadata and dependencies
Description=Gunicorn instance to serve myproject
After=network.target
# tells the init system to only start this after the networking target has been reached
# We will give our regular user account ownership of the process since it owns all of the relevant files
[Service]
# Service specify the user and group under which our process will run.
User=www-data
# give group ownership to the www-data group so that Nginx can communicate easily with the Gunicorn processes.
Group=www-data
# We'll then map out the working directory and set the PATH environmental variable so that the init system knows where our the executabl>
WorkingDirectory=/home/xxx/xxx
# We'll then specify the commanded to start the service
ExecStart=/usr/bin/gunicorn --bind unix:testpad.sock --access-logfile /home/xxx/xxx/accesslog --error-logfile /home/xxx/xxx/errorlog -m 007 wsgi:app
# This will tell systemd what to link this service to if we enable it to start at boot. We want this service to start when the regular m>
[Install]
WantedBy=multi-user.target
when I start the service it runs for a second then I get this from sudo systemctl status testpad
testpad.service - Gunicorn instance to serve myproject
Loaded: loaded (/etc/systemd/system/testpad.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Mon 2021-09-06 15:47:59 UTC; 16min ago
Process: 655575 ExecStart=/usr/bin/gunicorn --bind unix:testpad.sock --access-logfile /home/xxx/xxx/acces>
Main PID: 655575 (code=exited, status=1/FAILURE)
Sep 06 15:47:54 microportalbeta systemd[1]: Started Gunicorn instance to serve myproject.
Sep 06 15:47:59 microportalbeta systemd[1]: testpad.service: Main process exited, code=exited, status=1/FAILURE
Sep 06 15:47:59 microportalbeta systemd[1]: testpad.service: Failed with result 'exit-code'.
so when I cat errorlog I get
[2021-09-06 15:21:31 +0000] [655222] [INFO] Worker exiting (pid: 655222)
[2021-09-06 15:21:31 +0000] [655223] [ERROR] Exception in worker process
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/gunicorn/arbiter.py", line 589, in spawn_worker
worker.init_process()
File "/usr/lib/python3/dist-packages/gunicorn/workers/base.py", line 134, in init_process
self.load_wsgi()
File "/usr/lib/python3/dist-packages/gunicorn/workers/base.py", line 146, in load_wsgi
self.wsgi = self.app.wsgi()
File "/usr/lib/python3/dist-packages/gunicorn/app/base.py", line 67, in wsgi
self.callable = self.load()
File "/usr/lib/python3/dist-packages/gunicorn/app/wsgiapp.py", line 58, in load
return self.load_wsgiapp()
File "/usr/lib/python3/dist-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
return util.import_app(self.app_uri)
File "/usr/lib/python3/dist-packages/gunicorn/util.py", line 384, in import_app
mod = importlib.import_module(module)
File "/usr/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 855, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "/home/xxx/xxx/wsgi.py", line 1, in <module>
from run import app
File "/home/xxx/xxx/run.py", line 1, in <module>
from genedflask import create_app, db
File "/home/xxx/xxx/xxx/__init__.py", line 5, in <module>
from flask_login import LoginManager
ModuleNotFoundError: No module named 'flask_login'
[2021-09-06 15:21:31 +0000] [655223] [INFO] Worker exiting (pid: 655223)
[2021-09-06 15:21:32 +0000] [655220] [WARNING] Worker with pid 655223 was terminated due to signal 15
[2021-09-06 15:21:32 +0000] [655220] [WARNING] Worker with pid 655222 was terminated due to signal 15
[2021-09-06 15:21:32 +0000] [655220] [INFO] Shutting down: Master
[2021-09-06 15:21:32 +0000] [655220] [INFO] Reason: Worker failed to boot.
[2021-09-06 15:23:34 +0000] [655249] [INFO] Starting gunicorn 20.1.0
[2021-09-06 15:23:34 +0000] [655249] [ERROR] Retrying in 1 second.
[2021-09-06 15:23:35 +0000] [655249] [ERROR] Retrying in 1 second.
[2021-09-06 15:23:36 +0000] [655249] [ERROR] Retrying in 1 second.
[2021-09-06 15:23:37 +0000] [655249] [ERROR] Retrying in 1 second.
[2021-09-06 15:23:38 +0000] [655249] [ERROR] Retrying in 1 second.
[2021-09-06 15:23:39 +0000] [655249] [ERROR] Can't connect to testpad.sock
[2021-09-06 15:28:44 +0000] [655315] [INFO] Starting gunicorn 20.1.0
[2021-09-06 15:28:44 +0000] [655315] [ERROR] Retrying in 1 second.
[2021-09-06 15:28:45 +0000] [655315] [ERROR] Retrying in 1 second.
[2021-09-06 15:28:46 +0000] [655315] [ERROR] Retrying in 1 second.
[2021-09-06 15:28:47 +0000] [655315] [ERROR] Retrying in 1 second.
[2021-09-06 15:28:48 +0000] [655315] [ERROR] Retrying in 1 second.
[2021-09-06 15:28:49 +0000] [655315] [ERROR] Can't connect to testpad.sock
[2021-09-06 15:31:09 +0000] [655348] [INFO] Starting gunicorn 20.1.0
[2021-09-06 15:31:09 +0000] [655348] [ERROR] Retrying in 1 second.
[2021-09-06 15:31:10 +0000] [655348] [ERROR] Retrying in 1 second.
[2021-09-06 15:31:11 +0000] [655348] [ERROR] Retrying in 1 second.
[2021-09-06 15:31:12 +0000] [655348] [ERROR] Retrying in 1 second.
[2021-09-06 15:31:13 +0000] [655348] [ERROR] Retrying in 1 second.
[2021-09-06 15:31:14 +0000] [655348] [ERROR] Can't connect to testpad.sock
[2021-09-06 15:33:34 +0000] [655387] [INFO] Starting gunicorn 20.1.0
[2021-09-06 15:33:34 +0000] [655387] [ERROR] Retrying in 1 second.
[2021-09-06 15:33:35 +0000] [655387] [ERROR] Retrying in 1 second.
[2021-09-06 15:33:36 +0000] [655387] [ERROR] Retrying in 1 second.
[2021-09-06 15:33:37 +0000] [655387] [ERROR] Retrying in 1 second.
[2021-09-06 15:33:38 +0000] [655387] [ERROR] Retrying in 1 second.
[2021-09-06 15:33:39 +0000] [655387] [ERROR] Can't connect to testpad.sock
[2021-09-06 15:47:54 +0000] [655575] [INFO] Starting gunicorn 20.1.0
[2021-09-06 15:47:54 +0000] [655575] [ERROR] Retrying in 1 second.
[2021-09-06 15:47:55 +0000] [655575] [ERROR] Retrying in 1 second.
[2021-09-06 15:47:56 +0000] [655575] [ERROR] Retrying in 1 second.
[2021-09-06 15:47:57 +0000] [655575] [ERROR] Retrying in 1 second.
[2021-09-06 15:47:58 +0000] [655575] [ERROR] Retrying in 1 second.
[2021-09-06 15:47:59 +0000] [655575] [ERROR] Can't connect to testpad.sock
Not sure why it is not finding flask-login as it is installed under pip and pip3 globally, and is visible when I run pip3 list or pip list.
I assume that once I overcome this hurdle then the testpad.sock file should auto create as it did with my previous app. Most people with this problem I found it was an issue with gunicorn looking at the global environment when people installed dependencies in a virtual environment, which I am not doing in this circumstance.
Any suggestions?
Update
I never did figure out why it could not find the libraries when ran as a service, I even set the PYTHONPATH variable to see if that would help.
What I did do is give up on running as a service and instead just ran the application as a background process on port 8000 and just directed nginx to forward traffic there, and that worked.

gunicorn killing workers repeatedly with no traffic

I have a Flask app running on top of gunicorn which works fine on centos8 and rhel8. I'm trying to bring the app up on an rhel8 derivative which is hardened for FIPS/military compliance. This new os constantly sees workers timing out with no REST traffic offered.
The keepalive files are not having their timestamps updated by the worker threads which leads the arbiter to kill them off. I've added enough debug code to know that much.
I've studied the code and see that gunicorn uses a notify mechanism which relies on updating the permissions on temporary files. I've been looking around for these files but can't seem to find them. I've looked in /tmp (which I believe to be the default) and even set --worker-tmp-dir /tmp/gunicorn with no luck. Both are empty.
Any advice on how to proceed here will be appreciated.
[root#host-172-23-14-219 ~]# ls -ltr /tmp/gunicorn/
total 0
[root#host-172-23-14-219 ~]# ls -latr /tmp/gunicorn/
total 0
These are the messages I see in the debug output. The repeat every 30 seconds which is the default value for timeout. If I change the timeout, the time before being killed increases.
[2021-01-15 16:33:21 +0300] [9120] [CRITICAL] WORKER TIMEOUT (pid:14483)
[2021-01-15 16:33:21 +0300] [9120] [CRITICAL] WORKER TIMEOUT (pid:14485)
[2021-01-15 16:33:21 +0300] [9120] [CRITICAL] WORKER TIMEOUT (pid:14488)
[2021-01-15 16:33:21 +0300] [14483] [INFO] Worker exiting (pid: 14483)
[2021-01-15 16:33:21 +0300] [14485] [INFO] Worker exiting (pid: 14485)
[2021-01-15 16:33:21 +0300] [14488] [INFO] Worker exiting (pid: 14488)
[2021-01-15 16:33:21 +0300] [14782] [INFO] Booting worker with pid: 14782
[2021-01-15 16:33:21 +0300] [9120] [DEBUG] 1 workers
[2021-01-15 16:33:21 +0300] [14783] [INFO] Booting worker with pid: 14783
[2021-01-15 16:33:21 +0300] [14785] [INFO] Booting worker with pid: 14785
[2021-01-15 16:33:21 +0300] [9120] [DEBUG] 3 workers
Complete trace running gunicorn from the command line.
[root#dbaas1 restapi]# /usr/bin/python3 /usr/local/bin/gunicorn --log-level debug --log-syslog --workers 3 --bind 127.0.0.1:5000 -m 007 wsgi:app
[2021-01-18 22:11:23 +0300] [2942060] [DEBUG] Current configuration:
config: None
bind: ['127.0.0.1:5000']
backlog: 2048
workers: 3
worker_class: sync
threads: 1
worker_connections: 1000
max_requests: 0
max_requests_jitter: 0
timeout: 30
graceful_timeout: 30
keepalive: 2
limit_request_line: 4094
limit_request_fields: 100
limit_request_field_size: 8190
reload: False
reload_engine: auto
reload_extra_files: []
spew: False
check_config: False
preload_app: False
sendfile: None
reuse_port: False
chdir: /opt/rbbn/dbaas/src/serviceDiscovery/restapi
daemon: False
raw_env: []
pidfile: None
worker_tmp_dir: None
user: 0
group: 0
umask: 7
initgroups: False
tmp_upload_dir: None
secure_scheme_headers: {'X-FORWARDED-PROTOCOL': 'ssl', 'X-FORWARDED-PROTO': 'https', 'X-FORWARDED-SSL': 'on'}
forwarded_allow_ips: ['127.0.0.1']
accesslog: None
disable_redirect_access_to_syslog: False
access_log_format: %(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s"
errorlog: -
loglevel: debug
capture_output: False
logger_class: gunicorn.glogging.Logger
logconfig: None
logconfig_dict: {}
syslog_addr: udp://localhost:514
syslog: True
syslog_prefix: None
syslog_facility: user
enable_stdio_inheritance: False
statsd_host: None
dogstatsd_tags:
statsd_prefix:
proc_name: None
default_proc_name: wsgi:app
pythonpath: None
paste: None
on_starting: <function OnStarting.on_starting at 0x7f13b046be18>
on_reload: <function OnReload.on_reload at 0x7f13b046bf28>
when_ready: <function WhenReady.when_ready at 0x7f13b04860d0>
pre_fork: <function Prefork.pre_fork at 0x7f13b04861e0>
post_fork: <function Postfork.post_fork at 0x7f13b04862f0>
post_worker_init: <function PostWorkerInit.post_worker_init at 0x7f13b0486400>
worker_int: <function WorkerInt.worker_int at 0x7f13b0486510>
worker_abort: <function WorkerAbort.worker_abort at 0x7f13b0486620>
pre_exec: <function PreExec.pre_exec at 0x7f13b0486730>
pre_request: <function PreRequest.pre_request at 0x7f13b0486840>
post_request: <function PostRequest.post_request at 0x7f13b04868c8>
child_exit: <function ChildExit.child_exit at 0x7f13b04869d8>
worker_exit: <function WorkerExit.worker_exit at 0x7f13b0486ae8>
nworkers_changed: <function NumWorkersChanged.nworkers_changed at 0x7f13b0486bf8>
on_exit: <function OnExit.on_exit at 0x7f13b0486d08>
proxy_protocol: False
proxy_allow_ips: ['127.0.0.1']
keyfile: None
certfile: None
ssl_version: 2
cert_reqs: 0
ca_certs: None
suppress_ragged_eofs: True
do_handshake_on_connect: False
ciphers: None
raw_paste_global_conf: []
strip_header_spaces: False
[2021-01-18 22:11:23 +0300] [2942060] [INFO] Starting gunicorn 20.0.4
[2021-01-18 22:11:23 +0300] [2942060] [DEBUG] Arbiter booted
[2021-01-18 22:11:23 +0300] [2942060] [INFO] Listening at: http://127.0.0.1:5000 (2942060)
[2021-01-18 22:11:23 +0300] [2942060] [INFO] Using worker: sync
[2021-01-18 22:11:23 +0300] [2942066] [INFO] Booting worker with pid: 2942066
[2021-01-18 22:11:23 +0300] [2942068] [INFO] Booting worker with pid: 2942068
[2021-01-18 22:11:23 +0300] [2942069] [INFO] Booting worker with pid: 2942069
[2021-01-18 22:11:23 +0300] [2942060] [DEBUG] 3 workers
[2021-01-18 22:11:53 +0300] [2942060] [CRITICAL] WORKER TIMEOUT (pid:2942066)
[2021-01-18 22:11:53 +0300] [2942060] [CRITICAL] WORKER TIMEOUT (pid:2942068)
[2021-01-18 22:11:53 +0300] [2942060] [CRITICAL] WORKER TIMEOUT (pid:2942069)
[2021-01-18 22:11:53 +0300] [2942066] [INFO] Worker exiting (pid: 2942066)
[2021-01-18 22:11:53 +0300] [2942068] [INFO] Worker exiting (pid: 2942068)
[2021-01-18 22:11:53 +0300] [2942069] [INFO] Worker exiting (pid: 2942069)
[2021-01-18 22:11:53 +0300] [2942357] [INFO] Booting worker with pid: 2942357
[2021-01-18 22:11:53 +0300] [2942060] [DEBUG] 1 workers
[2021-01-18 22:11:53 +0300] [2942359] [INFO] Booting worker with pid: 2942359
[2021-01-18 22:11:53 +0300] [2942360] [INFO] Booting worker with pid: 2942360
[2021-01-18 22:11:53 +0300] [2942060] [DEBUG] 3 workers
Running into a similar problem - everything works fine on Ubuntu 18.04, but fails consistently with similar errors / logging on Ubuntu 22.04.
Try increasing timeout higher than 30 in gunicorn config / settings.
Changing the timeout to 90 was sufficient for me to get things working again - my best guess is that the initial load-in of the Flask app takes longer on some machines and so it's hitting this limit.

Method not allowed, app running with no errors in heroku logs

I've deployed my flask app on heroku, and i'm getting "Method not allowed" in my browser at the url.
all my routes have the methods set. I'm not trying to do much other than access my pages (no forms or anything like that). I'm using gunicorn. Just got no idea where I'm going wrong.
here is my main file:
from mongo import get_spotify_music
import os
app = Flask(__name__)
music = list(get_spotify_music())
#app.route("/", methods=['GET'])
#app.route("/index", methods=['GET'])
def home():
return render_template('index.html', title='about')
is_live = os.environ.get("IS_LIVE", None)
url = '0.0.0.0' if is_live else '127.0.0.1'
if __name__ == "__main__":
port = int(os.environ.get("PORT", 5000))
app.run(host=url, port=port, debug=True)```
my Procfile looks like:
```web: gunicorn app:app```
heroku logs --tail looks like:
```2019-07-15T08:25:41.093957+00:00 app[api]: Deploy 325e5224 by user myemail#myemail.com
2019-07-15T08:25:45.035819+00:00 heroku[web.1]: Starting process with command `gunicorn app:app`
2019-07-15T08:25:47.166897+00:00 app[web.1]: [2019-07-15 08:25:47 +0000] [4] [INFO] Starting gunicorn 19.9.0
2019-07-15T08:25:47.167484+00:00 app[web.1]: [2019-07-15 08:25:47 +0000] [4] [INFO] Listening at: http://0.0.0.0:57502 (4)
2019-07-15T08:25:47.167616+00:00 app[web.1]: [2019-07-15 08:25:47 +0000] [4] [INFO] Using worker: sync
2019-07-15T08:25:47.172768+00:00 app[web.1]: [2019-07-15 08:25:47 +0000] [10] [INFO] Booting worker with pid: 10
2019-07-15T08:25:47.261871+00:00 app[web.1]: [2019-07-15 08:25:47 +0000] [11] [INFO] Booting worker with pid: 11
2019-07-15T08:25:47.852647+00:00 heroku[web.1]: State changed from starting to up
2019-07-15T08:25:50.000000+00:00 app[api]: Build succeeded
When I go to the link that's set, i just get a method not allowed, rather than my web page....
Not sure where I'm going wrong here...

serving flask app using gunicorn + nginx showing 404 [ec2]

I am trying to serve a simple API by following this digitalocean tutorial.
For testing I was earlier serving the API through gunicorn by doing a
$ gunicorn --bind 0.0.0.0:5000 trumporate.wsgi:app
And curling the API endpoint works inside the ec2 box
$ curl -X GET http://0.0.0.0:5000/api/v1/trump/rant/
{
"foo": "bar"
}
Now I shifted this gunicorn process to run at startup by making a systemd service
# /etc/systemd/system/trumporate.service
[Unit]
Description=Gunicorn instance for trumporate
After=network.target
[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/var/opt/trumporate
ExecStart=/usr/local/bin/gunicorn --workers 3 --bind unix:trumporate.sock -m 007 --access-logfile /var/log/trumporate/gunicorn-access.log --error-logfile /var/log/trumporate/gunicorn-error.log trumporate.wsgi:app
[Install]
WantedBy=multi-user.target
I have created the files
/var/log/trumporate/gunicorn-error.log
/var/log/trumporate/gunicorn-access.log
and changed the ownership and group to ubuntu
After enabling the service and rebooting, I checked the status
$ sudo systemctl status trumporate.service
● trumporate.service - Gunicorn instance for trumporate
Loaded: loaded (/etc/systemd/system/trumporate.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2017-05-03 06:30:26 UTC; 1h 2min ago
Main PID: 1122 (gunicorn)
Tasks: 4
Memory: 92.2M
CPU: 1.390s
CGroup: /system.slice/trumporate.service
├─1122 /usr/bin/python3 /usr/local/bin/gunicorn --workers 3 --bind unix:trumporate.sock -m 007 --access-logfile /var/log/trumporate/gunicorn-access.log --error-logfile /var/log/trumporate/gunic
├─1264 /usr/bin/python3 /usr/local/bin/gunicorn --workers 3 --bind unix:trumporate.sock -m 007 --access-logfile /var/log/trumporate/gunicorn-access.log --error-logfile /var/log/trumporate/gunic
├─1266 /usr/bin/python3 /usr/local/bin/gunicorn --workers 3 --bind unix:trumporate.sock -m 007 --access-logfile /var/log/trumporate/gunicorn-access.log --error-logfile /var/log/trumporate/gunic
└─1267 /usr/bin/python3 /usr/local/bin/gunicorn --workers 3 --bind unix:trumporate.sock -m 007 --access-logfile /var/log/trumporate/gunicorn-access.log --error-logfile /var/log/trumporate/gunic
May 03 06:30:26 ip-172-31-25-173 systemd[1]: Started Gunicorn instance for trumporate.
Following the DO tutorial, I tried configuring nginx to proxy incoming requests on port 80
$ cat /etc/nginx/sites-available/trumporate
server {
listen 80;
server_name private_ip_address;
location / {
include proxy_params;
proxy_pass http://unix:/var/opt/trumporate/trumporate.sock;
}
}
And then did a
$ ln -s /etc/nginx/sites-available/trumporate /etc/nginx/sites-enabled
$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Now if I try to do GET request to the API endpoint from outside the ec2 box
$ curl -X GET http://public_ip/api/v1/trump/rant
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.10.0 (Ubuntu)</center>
</body>
</html>
Same case when I try to do it from inside the ec2 container too
$ curl -X GET http://localhost:80/api/v1/trump/rant/
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.10.0 (Ubuntu)</center>
</body>
</html>
log files
# /var/log/nginx/access.log
dev_box_ip - - [03/May/2017:05:50:45 +0000] "GET /api/v1/trump/rant/ HTTP/1.1" 404 580 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36"
127.0.0.1 - - [03/May/2017:06:13:26 +0000] "GET /api/v1/trump/rant/ HTTP/1.1" 404 178 "-" "curl/7.47.0"
dev_box_ip - - [03/May/2017:07:42:42 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36"
/var/log/nginx$ cat error.log
/var/log/nginx$
/var/log/trumporate$ cat gunicorn-access.log
/var/log/trumporate$
$ cat gunicorn-error.log
[2017-05-03 06:28:41 +0000] [1884] [INFO] Starting gunicorn 19.7.1
[2017-05-03 06:28:41 +0000] [1884] [INFO] Listening at: unix:trumporate.sock (1884)
[2017-05-03 06:28:41 +0000] [1884] [INFO] Using worker: sync
[2017-05-03 06:28:41 +0000] [1889] [INFO] Booting worker with pid: 1889
[2017-05-03 06:28:41 +0000] [1890] [INFO] Booting worker with pid: 1890
[2017-05-03 06:28:41 +0000] [1891] [INFO] Booting worker with pid: 1891
[2017-05-03 06:29:48 +0000] [1884] [INFO] Handling signal: term
[2017-05-03 06:29:48 +0000] [1889] [INFO] Worker exiting (pid: 1889)
[2017-05-03 06:29:48 +0000] [1890] [INFO] Worker exiting (pid: 1890)
[2017-05-03 06:29:48 +0000] [1891] [INFO] Worker exiting (pid: 1891)
[2017-05-03 06:29:49 +0000] [1884] [INFO] Shutting down: Master
[2017-05-03 06:30:27 +0000] [1122] [INFO] Starting gunicorn 19.7.1
[2017-05-03 06:30:27 +0000] [1122] [INFO] Listening at: unix:trumporate.sock (1122)
[2017-05-03 06:30:27 +0000] [1122] [INFO] Using worker: sync
[2017-05-03 06:30:27 +0000] [1264] [INFO] Booting worker with pid: 1264
[2017-05-03 06:30:27 +0000] [1266] [INFO] Booting worker with pid: 1266
[2017-05-03 06:30:28 +0000] [1267] [INFO] Booting worker with pid: 1267
/var/log/trumporate$
EDIT
Relevant part of the flask app
#app.route('/api/v1/trump/rant/')
def return_rant():
foo = # logic
return jsonify(rant=foo)
Did you do nginx -s reload && systemctl restart nginx?
Another thing which you could try is to make the bindings on a http port instead of a socket:
--bind 127.0.0.1:6767 #in systemd config
and change nginx config as follows:
location / {
include proxy_params;
proxy_redirect off;
proxy_pass http://127.0.0.1:6767;
}
Also why do you have private_ip in nginx config?
server_name private_ip_address;
Change that to
server_name "_";
# OR
server_name PUBLIC_IP;
and remove all default configs from /etc/nginx/site-enabled
1) use of server_name private_ip_address;?
Nginx uses the server_name to check with the host header of the incoming request, and that isn't the private address. (You usually access using either a domain name or the public address in the URL bar)
2) I deleted /etc/nginx/site-enabled/default dir to make things to get to working.
If your server_name is not set correctly, nginx processes the request using the default file or the server block containing default_server. Thus I asked you to delete that file just in case there was an issue with your server name ^_^
Also, what difference would it make to the performance of the API if I am binding it to a port instead of the socket file as suggested by the blog post?
This would typically be premature optimization, any difference you get will purely be within a margin of error compared to the bottlenecks caused by flask/python and especially database connections. Though please take this with a grain of salt as I do not have any reliable source to quote this on.

Spring-boot slow to start

When I launch my jhipster app using "mvn spring-boot:run", it takes up to 60 seconds to start...
First part of my log is :
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building jhipster 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> spring-boot-maven-plugin:1.1.9.RELEASE:run (default-cli) # jhipster >>>
[INFO]
[INFO] --- maven-enforcer-plugin:1.3.1:enforce (enforce-versions) # jhipster ---
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) # jhipster ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 4 resources
[INFO] Copying 22 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) # jhipster ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) # jhipster ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 3 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) # jhipster ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] <<< spring-boot-maven-plugin:1.1.9.RELEASE:run (default-cli) # jhipster <<<
[INFO]
[INFO] --- spring-boot-maven-plugin:1.1.9.RELEASE:run (default-cli) # jhipster ---
[INFO] Attaching agents: []
Listening for transport dt_socket at address: 5005
--> Then it hangs for around 30 seconds before continuing :
[INFO] com.mycompany.myapp.Application - Starting Application on MacBook-Pro.local with PID 5130 (/Users/othomas/Developpement/jhipster-1.9.0/target/classes started by othomas in /Users/othomas/Developpement/jhipster-1.9.0)
[DEBUG] com.mycompany.myapp.Application - Running with Spring Boot v1.1.9.RELEASE, Spring v4.0.8.RELEASE
[DEBUG] org.jboss.logging - Logging Provider: org.jboss.logging.Log4jLoggerProvider
...
I remember having used older versions of jhipster generator (0.17 etc) et it started in 15-20 seconds.
Is it normal or is there a problem on my side ? Where to look for ?
Thanks,
O.
I've been suffering slow startup times myself and wondering what the cause was. I get all the console messages saying various things have started and then it hangs just before the final message to say the app has loaded.
Eventually I found I could use Java VisualVM as part of the JDK to see what was going on. If you have the jdk installed its jvisualvm.exe in the bin folder. Then when I select to debug as Application.java the tomcat process pops up and you can track what's going on.
I took a couple of thread dumps where it hangs and it always seemed to be where the swagger API docs are being generated. A bit more digging and this is configured in a class called MetricsConfiguration which is excluded if you run with a profile called "fast".
In eclipse I edited my debug configuration to include a program argument of:
--spring.profiles.active=dev,fast
This cuts down the startup time from 230 seconds to just 25!
I had a quick scan and fast seems to disable all sorts of things. It mainly looks like the stuff under the admin menu which you'll probably not need during development anyway. Personally I would prefer a fast bootup to being able to see the rest docs during development.
Swagger being such a hog made me wonder if it's such a good idea after all. Is it worth the cost? i then read this http://java.dzone.com/articles/swagger-great and I'm considering just removing it altogether. It's a nice idea but seems to add 33mb to the build + for me was causing really slow startup times.
For info I have around 16 entities. So not small but not excessively large either.
Make sure you aren't running the server in debug mode and have a breakpoint set. This reduced the startup time of one of my applications from 3 min to 22 sec.
This is weird.
Indeed, it should start in 5-15 seconds depending on your machine and specific setup.
But it should not hang for 30 seconds: the line you show is a bit new, it's because we launch the application in debug mode when you use the dev profile -> you can attach a debugger on it.
It looks like it's waiting for you to connect a debugger: I've never seen it myself, so maybe you have some specific JVM option for attaching a debugger at start up, with a timeout of 30 seconds?
Thanks for your feedback. I investigated and put more logs in the app (Application.java).
Actually the problem does not come from the debug mode, the application does not hang here.
The first big "pause" comes from the scanning of liquibase packages (addLiquibaseScanPackages(); in Application.java ) : 26 seconds !
My second pause is still related to Liquibase (log "Configuring Liquibase" ) : 20 seconds. During that time, if I put Liquibase log level to DEBUG, I see that a lock is set and then released but it happens very quickly.
I really don't understand. I am using h2 in-memory database, jdk 1.7.0_25 and Maven 3.0.5, running on MacBook Pro with SSD.
Here is my full log when I run with "mvn spring-boot:run".
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building jhipster 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> spring-boot-maven-plugin:1.1.9.RELEASE:run (default-cli) # jhipster >>>
[INFO]
[INFO] --- maven-enforcer-plugin:1.3.1:enforce (enforce-versions) # jhipster ---
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) # jhipster ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 4 resources
[INFO] Copying 22 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) # jhipster ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) # jhipster ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 3 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) # jhipster ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] <<< spring-boot-maven-plugin:1.1.9.RELEASE:run (default-cli) # jhipster <<<
[INFO]
[INFO] --- spring-boot-maven-plugin:1.1.9.RELEASE:run (default-cli) # jhipster ---
[INFO] Attaching agents: []
Listening for transport dt_socket at address: 5005
Wed Nov 26 16:32:23 CET 2014 Added log : Application is about to start
Wed Nov 26 16:32:28 CET 2014Added log : Application started, now we set banner to false
Wed Nov 26 16:32:28 CET 2014Added log : About to add Default profile
Wed Nov 26 16:32:28 CET 2014Added log : Default Profile added. Now we scan liquibase packages
Wed Nov 26 16:32:28 CET 2014Added log : Liquibase pakages scanned. Now we run the app
2014-11-26 16:32:54,564 [INFO] com.mycompany.myapp.Application - Starting Application on MacBook-Pro.local with PID 25452 (/Users/othomas/Developpement/jhipster-1.9.0/target/classes started by othomas in /Users/othomas/Developpement/jhipster-1.9.0)
2014-11-26 16:32:54,567 [DEBUG] com.mycompany.myapp.Application - Running with Spring Boot v1.1.9.RELEASE, Spring v4.0.8.RELEASE
2014-11-26 16:32:57,429 [DEBUG] org.jboss.logging - Logging Provider: org.jboss.logging.Log4jLoggerProvider
2014-11-26 16:32:57,559 [DEBUG] com.mycompany.myapp.config.AsyncConfiguration - Creating Async Task Executor
2014-11-26 16:32:58,305 [DEBUG] com.mycompany.myapp.config.MetricsConfiguration - Registering JVM gauges
2014-11-26 16:32:58,379 [INFO] com.mycompany.myapp.config.MetricsConfiguration - Initializing Metrics JMX reporting
2014-11-26 16:32:58,445 [DEBUG] com.mycompany.myapp.config.DatabaseConfiguration - Configuring Datasource
2014-11-26 16:32:59,353 [DEBUG] com.mycompany.myapp.config.DatabaseConfiguration - Configuring Liquibase
2014-11-26 16:33:19,489 [DEBUG] com.mycompany.myapp.config.CacheConfiguration - Starting Ehcache
2014-11-26 16:33:19,491 [DEBUG] com.mycompany.myapp.config.CacheConfiguration - Registering Ehcache Metrics gauges
2014-11-26 16:33:23,419 [DEBUG] com.mycompany.myapp.config.MailConfiguration - Configuring mail server
2014-11-26 16:33:24,559 [INFO] com.mycompany.myapp.config.WebConfigurer - Web application configuration, using profiles: [dev]
2014-11-26 16:33:24,560 [DEBUG] com.mycompany.myapp.config.WebConfigurer - Initializing Metrics registries
2014-11-26 16:33:24,564 [DEBUG] com.mycompany.myapp.config.WebConfigurer - Registering Metrics Filter
2014-11-26 16:33:24,565 [DEBUG] com.mycompany.myapp.config.WebConfigurer - Registering Metrics Servlet
2014-11-26 16:33:24,567 [DEBUG] com.mycompany.myapp.config.WebConfigurer - Registering GZip Filter
2014-11-26 16:33:24,569 [DEBUG] com.mycompany.myapp.config.WebConfigurer - Initialize H2 console
2014-11-26 16:33:24,570 [INFO] com.mycompany.myapp.config.WebConfigurer - Web application fully configured
2014-11-26 16:33:29,753 [INFO] com.mycompany.myapp.Application - Running with Spring profile(s) : [dev]
2014-11-26 16:33:30,012 [INFO] com.mycompany.myapp.config.ThymeleafConfiguration - loading non-reloadable mail messages resources
2014-11-26 16:33:30,896 [DEBUG] com.mycompany.myapp.aop.logging.LoggingAspect - Enter: com.mycompany.myapp.repository.CustomAuditEventRepository.auditEventRepository() with argument[s] = []
2014-11-26 16:33:30,905 [DEBUG] com.mycompany.myapp.aop.logging.LoggingAspect - Exit: com.mycompany.myapp.repository.CustomAuditEventRepository.auditEventRepository() with result = com.mycompany.myapp.repository.CustomAuditEventRepository$1#1edce963
2014-11-26 16:33:37,229 [INFO] com.mycompany.myapp.Application - Started Application in 68.311 seconds (JVM running for 73.972)
Wed Nov 26 16:33:37 CET 2014Added log : App is running
Thanks,
Olivier
It is advised to start the application with the debug points disabled unless you want to debug while starting up
you can just modify xmx like java -jar -Xmx1024m.
Because when Spring boot started, it loads lots of spring bean. You can add heap memory to improve it's performance.

Resources