pyTelegramBotAPI: bot stops running after about 1 hour on Heroku - python

I can't find a solution. I'm hosting a telegram bot in python with the library pyTelegramBotAPI on Heroku, my code works but the script stop running after 1 or 2 hours from a new deployment or a new execution through the console (or some minutes, i don't know the exact timing). Where is the problem? Thanks for the help.
Example of logs on Heroku:
2020-07-15T10:47:40.500384+00:00 app[api]: Starting process with command `python randobot.py` by user ________#gmail.com
2020-07-15T10:47:44.398310+00:00 heroku[run.1085]: State changed from starting to up
2020-07-15T10:47:44.455749+00:00 heroku[run.1085]: Awaiting client
2020-07-15T10:47:44.773283+00:00 heroku[run.1085]: Starting process with command `python\ randobot.py`
2020-07-15T10:47:50.709806+00:00 heroku[run.1085]: Client connection closed. Sending SIGHUP to all processes
2020-07-15T10:47:51.233462+00:00 heroku[run.1085]: Process exited with status 129
2020-07-15T10:47:51.270855+00:00 heroku[run.1085]: State changed from up to complete
Procfile.:
web: python randobot.py
Code randobot.py:
import telebot
import time
import json
bot_token = "____________________________"
bot = telebot.TeleBot(token=bot_token)
...
... Some functions ...
...
while True:
try:
bot.polling(none_stop=True)
except Exception as e:
logger.error(e)
time.sleep(15)
Commands used for the deploy on Heroku with git:
git add .
git commit -m "..."
git push heroku master
Command used the first time on Heroku to run the bot (after the login ecc.):
heroku ps:scale web=1

It works, with (command on the console):
heroku ps:scale worker=1

Related

Trouble deploying Django application with Celery to Elastic Beanstalk

I have a working django application deployed to Elastic Beanstalk. I am trying to add some asynchronous commands to it so am adding Celery. Currently I am running container commands through python.config in my .ebextensions.
I have added the command:
06startworker:
command: "source /var/app/venv/*/bin/activate && celery -A project worker --loglevel INFO -B &"
to my python.config. When I add this command and try to deploy my elasticbeanstalk instance timesout and deployment fails.
I have confirmed that connection to my redis server is working and my application can connect to it. Checking my logs in my cfn-init.log I see:
Command 01wsgipass succeeded
Test failed with code 1
...
Command 06startworker succeeded
So I think that when adding the 06startworker command it is somehow interfering with my 01wsgipass command which runs fine when I dont have the start worker command.
For reference my wsgi command is:
01wsgipass: command: 'echo "WSGIPassAuthorization On" >> ../wsgi.conf'
I'm at a bit of a loss on how to troubleshoot this from here. I'm finding the logs that I am getting are not very helpful.

I am hosting a Discord Bot on Heroku it just gives me out error 14 ps webscale doesnt work either

I am hosting a Discord.py Bot on Heroku.
I am getting error code H 14
My procfile is below:
worker: python "Main Script.py"
My requirements file contains:
discord
I tried running the following command:
heroku ps:scale web=1
but it just says couldn't find the web process type.
There are a few issues here:
Your Procfile must be called Procfile exactly. You spell it all lowercase in your quesiton, but it needs a capital P.
Your Procfile only defines a worker process. I'm not sure why you are trying to scale up a web process. Try
heroku ps:scale worker=1
instead.
Or, if your Main Script.py is supposed to listen for HTTP requests, define it as a web process in your `Procfile:
web: python "Main Script.py"
In this case, make sure you're binding to the port Heroku assigns you via the PORT environment variable.
I suggest renaming your file as well, e.g. to main.py, but that shouldn't cause anything to break.

My discord.py bot hosted on heroku does not work

I hosted my discord.py bot on heroku, Build Succeeded, and Deployed.
The code works on my VSC but not on heroku.
requirements.txt :
discord.py==1.3.4
PyNaCl==1.3.0
pandas
dnspython==1.16.0
async-timeout==3.0.1
Procfile :
worker: python bot.py
I don't know what is the problem.
Can please anyone tell me what to revise or what's wrong?
I appreciate your help.
I did :
$ heroku logs --tail
and I got :
2020-07-30T03:30:00.853064+00:00 app[worker.1]: File "bot.py", line 3, in <module>
2020-07-30T03:30:00.853284+00:00 app[worker.1]: import requests
2020-07-30T03:30:00.853316+00:00 app[worker.1]: ModuleNotFoundError: No module named 'requests'
2020-07-30T03:30:00.964329+00:00 heroku[worker.1]: Process exited with status 1
2020-07-30T03:30:01.011352+00:00 heroku[worker.1]: State changed from up to crashed
Sorry for the fool question.
To fix your issue it sounds like you need to add the requests module to your requirements.txt. To obtain this you can run the following command in your console:
pip show requests
Make a note of the version number and append the following to your requirements.txt file:
requests==x.y.z
Where x.y.z is replaced with the version you obtained from using pip show requests.
The general process of hosting a bot on Heroku:
If you want to host your bot on Heroku the steps I take are:
requirements.txt
You can obtain this by running the following command in your console:
pip freeze > requirements.txt
This will write all the required Python modules into the file for you with their respective versions. (This should also find and add your missing "requests" module)
Procfile
Inside this file, you will only need:
worker: python bot.py
"python bot.py" is what will be run on Heroku to start your bot. Although you have done this fine.
Heroku Resources
On the Heroku website access the dashboard for your app. Then select the "Resources" tab.
Next, there should be the contents of your Procfile. Click on the pen on the right hand side, enable the switch to allow Heroku to start the worker.
If you have any issues with hosting your bot, please reply to my suggestion and I'll be happy to help.

Heroku dynos crashing

I readed a lot of questions here in stackoverflow, but I cannot solve my problem.
I have a simples python script (Only a print('Hello World')) and I'm sending it to Heroku, my files are:
Procfile: (I believe the problem is here)
web: python index.py
heroku logs --tail
2020-05-06T16:06:38.000000+00:00 app[api]: Build started by user <myemail>
2020-05-06T16:07:00.948412+00:00 heroku[web.1]: State changed from crashed to starting
2020-05-06T16:07:00.572714+00:00 app[api]: Release v11 created by user <myemail>
2020-05-06T16:07:00.572714+00:00 app[api]: Deploy 4053de7f by user <myemail>
2020-05-06T16:07:07.099124+00:00 heroku[web.1]: State changed from starting to crashed
2020-05-06T16:07:07.103524+00:00 heroku[web.1]: State changed from crashed to starting
2020-05-06T16:07:07.002972+00:00 app[web.1]: Hello World
2020-05-06T16:07:08.000000+00:00 app[api]: Build succeeded
2020-05-06T16:07:13.797877+00:00 heroku[web.1]: State changed from starting to crashed
2020-05-06T16:07:13.619779+00:00 app[web.1]: Hello World
heroku ts
Free dyno hours quota remaining this month: 550h 0m (100%)
For more information on dyno sleeping and how to upgrade, see:
https://devcenter.heroku.com/articles/dyno-sleeping
=== web (Free): python index.py (1)
web.1: crashed 2020/05/06 13:07:13 -0300 (~ 4m ago)
How we can see, the "Hello World" in log shows that the script runs when I send it to server. But after it, the dyno crashes
in case of Web dyno the application must bind to the port provided by Heroku and defined in the $PORT env variable, where it will receive the incoming requests.
If you need to HTTP traffic try to create a worker node
For example to create a simple Flask app which can receive requests:
from flask import Flask
import os
import logging
try:
app = Flask(__name__)
except Exception as e:
logging.exception("Error at startup")
#app.route('/test')
def test():
logging.info('/test')
return "test Ok"
if __name__ == '__main__':
app.run(debug=False, port=int(os.environ.get("PORT", 5000)), host='0.0.0.0')
Port is set using $PORT on Heroku while locally defaults to 5000

'heroku run python manage.py migrate' fails with an error 'Error R13 (Attach error) -> Failed to attach to process'

i have tried 'heroku run python manage.py migrate' and get " Timeout awaiting dyno, see https://devcenter.heroku.com/articles/one-off-dynos#timeout-awaiting-process" error.
heroku logs
2015-05-06T05:42:05.881706+00:00 heroku[api]: Starting process with command `manage.py db:migrate` by navajyothms1989#gmail.com
2015-05-06T05:42:22.537124+00:00 heroku[run.9766]: Awaiting client
2015-05-06T05:42:22.789430+00:00 heroku[run.9766]: State changed from starting to up
2015-05-06T05:42:52.552578+00:00 heroku[run.9766]: Error R13 (Attach error) -> Failed to attach to process
2015-05-06T05:42:53.293185+00:00 heroku[run.9766]: Process exited with status 128
2015-05-06T05:42:53.307222+00:00 heroku[run.9766]: State changed from up to complete
What should I do to solve this problem?
Same thing happened to me. But it was solved by connecting to my mobile internet. My ISP was blocking most of the ports it seems and heroku run command takes random ports.

Categories