-
Notifications
You must be signed in to change notification settings - Fork 5.7k
A new way to restart the bot under v20 #3718
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
The solution I'm implementing in my v20 bot is fairly primitive but it works well.
and this is the bot itself,
EDIT, or maybe an afterthought: |
My solution would be to just recommend running the bot as a systemd service in production. It can be set up so as to restart the service every time it fails. I kind of see it as a general problem and a general solution, unrelated to the specifics of PTB, and the solution by @Trifase would also work with any script, not just our bot. |
@lemontree210 auto-restarting the bot if it fails is one thing. Restarting it from within the code is more complictade though … @Trifase can you paste the output of running In fact, I also know why it's not working: Raising I guess some research is in order on how |
How about using something like psystemd to restart the application that is running as a systemd service? |
Does that allow you to trigger a graceful shutdown from within a callback and then restart the program? |
|
Sure!
Unless I messed up setting up the MWE, that's all I do. It's working for me on raspbian or whatever it's called the raspberry OS nowadays, but it's not working on windows. This is the debug log (formatted, sorry) of the successfully shutdown after the
The original repo in which I'm using the code I'm providing is public, but really undocumented: this is the file that relaunch the bot and this is the restart function - repo is a bit noisy but that's the gist of it |
I use this:
It's not optimal, definitely don't do this. On a simple bot it is good enough though. |
Finally got around to have a closer look at my findings from #3718 (comment). By having a look at the source of python-telegram-bot/telegram/ext/_application.py Lines 935 to 940 in 59105b2
and then cross-checking with the python docs
this becomes a bit of a case of RTFM 😄 . I proposed a method of stopping the application from within callbacks in #3804. Once that is possible, restarting the script after it has finished can be done by generic (i.e. non-ptb-specific) means like systemd or |
I still don't get why it works for me. |
v20.5 introduced |
In v13, we had a simple code snippet we could use to restart the bot (https://github.com/python-telegram-bot/v13.x-wiki/wiki/Code-snippets#simple-way-of-restarting-the-bot), which restarted the whole script passing in any arguments. However simply adapting that to v20 this doesn't work since:
stop
,shutdown
,app.updater.shutdown
) all need to be awaited instead of simply calledIf anyone has figured out a simple way to restart the whole script (e.g. by sending a command to a bot), feel free to propose your solution below!
The text was updated successfully, but these errors were encountered: