Python Twitter Bot w/ Heroku Error: R10 Boot Timeout - python

I have developed a simple python twitter bot which periodically executes various functions using the following libraries:
TwitterFollowBot==2.0.2
schedule==0.3.2
The application works fine when I execute it on my computer, and I wanted to migrate it to Heroku so it could run independently. Upon executing it on Heroku it works as it should for 60 seconds before timing out:
Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
After researching this, I found out that Heroku dynamically switches ports and my application must continuously specify which port it should run on. From another thread I read that a possible solution required me to alter my Procfile, so I appended the PORT variable to the end:
Procfile: web: python app.py $PORT
This had no effect so I tried it again with ${PORT},
And I also tried switching web: with bot: (which stopped my application from executing properly)
I found other solutions to this issue which worked for node, or python applications using Django, Flask, etc... However, I was unable to find a solution for just a simple .py application. Is this even possible? Or should I create my app with Flask and attempt one of the other fixes?

If it doesn't provide any web content then you don't need to run a web process - call it something else like bot and then do:
heroku ps:scale web=0
heroku ps:scale bot=1
and you won't get any more R10s.

Related

How to run Dash App in background/offline [duplicate]

This question already has answers here:
Are a WSGI server and HTTP server required to serve a Flask app?
(3 answers)
Closed 1 year ago.
I am new to dash app development and created a app which is running in the linux server. It is available to all users in our intranet only when I trigger and online. Once I logoff it is not accessible. How to schedule the app to run continuously even when I am offline. Any responses would be appreciated.
currently it is serving with below command
python app.py
I cant deploy in Heroku due to security restrictions. Docker also unavailable. Any other option would be appreciated.
Regards,
Sudheer
From this description, I guess that you
SSH into the server
Run python app.py
At this point, the app is available
Log off from the SSH connection (e.g. exit command)
At this point, the app is not available any more
If so, this is because the command you run during the SSH session will be terminated when you log off from the session.
There are several ways to keep your app running after you log off. For example,
Run nohub python app.py &.
Run tmux and run python app.py in the tmux window. Then Ctrl+b+d to close the tmux window.
In either way, the app will keep running after you log off from the SSH connection. If my guess about your situation is wrong, please elaborate on your situation more in detail. For example, tell us the series of command you run and what happens with them.
In particular, the term "offline" is not clear in this context. If you are completely offline and not connected even to the intranet, then there is no chance that you can use the app running on the server.

How do I tell Jenkins to run a Python Flask application, then Selenium tests, then close the Flask application?

I have a Flask application that spins up my web application to a specific port (e.g., 8080). It launches by running python run.py. I also have a Python file that runs Selenium end-to-end tests for the web app.
This is part of a GitHub repo, and I have it hooked up so that whenever I push/commit to the GitHub repo, it initiates a Jenkins test. Jenkins is using my machine as the agent to run the commands. I have a series of commands in a Pipeline on Jenkins to execute.
Ideally, the order of commands should be:
Spin up the Flask web application
Run the Selenium tests
Terminate the Flask web application
Now, my issue is this: I can use Jenkins to clone the repo to the local workspace on my computer (the agent), and then run the Python command to spin up my web app. I can use Jenkins to run a task in parallel to do the Selenium tests on the port used by the web app.
BUT I cannot figure out how to end the Flask web app once the Selenium tests are done. If I don't end the run.py execution, it is stuck on an infinite loop, endlessly serving up the application to the port. I can have Jenkins automatically abort after some specified amount of time, but that flags the entire process as a failure.
Does anyone have a good idea of how to end the Flask hosting process once the Selenium tests are done? Or, if I am doing this in a block-headed way (I admit I am speaking out of copious amounts of ignorance and inexperience), is there a better way I should be handling this?
I'm not aware of an official way to cause the development server to exit under Selenium control, but the following route works with Flask 1.1.2
from flask import request
...
if settings.DEBUG:
#app.route('/quit')
def quit():
shutdown_hook = request.environ.get('werkzeug.server.shutdown')
if shutdown_hook is not None:
shutdown_hook()
return "Bye"
return "No shutdown hook"

ENOTFOUND: getaddrinfo ENOTFOUND rendezvous.runtime.heroku.com after heroku run python manage.py migrate

Has anyone got this error:
ENOTFOUND: getaddrinfo ENOTFOUND rendezvous.runtime.heroku.com
rendezvous.runtime.heroku.com:5000
while running
heroku run python manage.py migrate ?
Any thoughts on how this can be fixed?
The error is because the user (or the CLI) cannot connect to the heroku servers, as is your case being behind a proxy. The fast and easy solution would be to go over to your Heroku Dashboard, and open up a Heroku Console there, then simply run your code from there.
Hope this helps!
I had a similar experience with a Rails app on Heroku.
Running heroku console --app AppName in my local terminal I got: ENOTFOUND: getaddrinfo ENOTFOUND rendezvous.runtime.heroku.com. This happened when I was trying to access our production app, but I also got the same response when trying to access the console on any of our Heroku development servers.
I've been working on this app for a year and this is the first time this has happened.
I went to the relevant app on the Heroku dashboard, clicked the Run console option, but then didn't actually run anything. I then tried heroku console --app AppName on my local terminal again and it worked as it should. The time between first getting the error and then it working was around 10 minutes. I'm not sure if interacting with the Heroku dashboard did anything, or if it was just that something was wrong at the time and it got sorted out when I tried a few minutes later.

How to Make uWSGI die when it encounters an error?

I have my Python app running through uWSGI. Rarely, the app will encounter an error which makes it not be able to load. At that point, if I send requests to uWSGI, I get the error no python application found, check your startup logs for errors. What I would like to happen in this situation is for uWSGI to just die so that the program managing it (Supervisor, in my case) can restart it. Is there a setting or something I can use to force this?
More info about my setup:
Python 2.7 app being run through uWSGI in a docker container. The docker container is managed by Supervisor, and if it dies, Supervisor will restart it, which is what I want to happen.
After an hour of searching, I finally found a way to do this. Just pass the --need-app argument when starting uWSGI, or add need-app = true in your .ini file, if you run things that way. No idea why this is off by default (in what situation would you ever want uWSGI to keep running when your app has died?) but so it goes.

How to debug Django app running on Heroku using a remote pdb connection?

To debug a bug I'm seeing on Heroku but not on my local machine, I'm trying to do step-through debugging.
The typical import pdb; pdb.set_trace() approach doesn't work with Heroku since you don't have access to a console connected to your app, but apparently you can use rpdb, a "remote" version of pdb.
So I've installed rpdb, added import rpdb; rpdb.set_trace() at the appropriate spot. When I make a request that hits the rpdb line, the app hangs as expected and I see the following in my heroku log:
pdb is running on 3d0c9fdd-c18a-4cc2-8466-da6671a72cbc:4444
Ok, so how to connect to the pdb that is running? I've tried heroku run nc 3d0c9fdd-c18a-4cc2-8466-da6671a72cbc 4444 to try to connect to the named host from within heroku's system, but that just immediately exits with status 1 and no error message.
So my specific question is: how do I now connect to this remote pdb?
The general related question is: is this even the right way for this sort of interactive debugging of an app running on Heroku? Is there a better way?
NOTE RE CELERY: Note, I've now also tried a similar approach with Celery, to no avail. The default host celery's rdb (remote pdb wrapper) uses is localhost, which you can't get to when it's Heroku. I've tried using the CELERY_RDB_HOST environment variable to the domain of the website that is being hosted on Heroku, but that gives a "Cannot assign requested address" error. So it's the same basic issue -- how to connect to the remote pdb instance that's running on Heroku?
In answer to your second question, I do it differently depending on the type of error (browser-side, backend, or view). For backend and view testing (unittests), will something like this work for you?
$ heroku run --app=your-app "python manage.py shell --settings=settings.production"
Then debug-away within ipython:
>>> %run -d script_to_run_unittests.py
Even if you aren't running a django app you could just run the debugger as a command line option to ipython so that any python errors will drop you to the debugger:
$ heroku run --app=your-app "ipython --pdb"
Front-end testing is a whole different ballgame where you should look into tools like selenium. I think there's also a "salad" test suite module that makes front end tests easier to write. Writing a test that breaks is the first step in debugging (or so I'm told ;).
If the bug looks simple, you can always do the old "print and run" with something like
import logging
logger = logging.getLogger(__file__)
logger.warn('here be bugs')`
and review your log files with getsentry.com or an equivalent monitoring tool or just:
heroku logs --tail

Categories

Resources