Skip to content
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

Question: How to shutdown properly? #801

Closed
Endogen opened this issue Aug 15, 2017 · 12 comments
Closed

Question: How to shutdown properly? #801

Endogen opened this issue Aug 15, 2017 · 12 comments

Comments

@Endogen
Copy link

Endogen commented Aug 15, 2017

I really fear to bring up a question that was answered 1000 times now but since i searched the issues and didn't find anything related, examples didn't include anything relevant and i didn't get a satisfying answer in the telegram group, i have to ask:

How to shutdown the bot without executing another script with
os.execl(os.dirname(__file__) + '/shutdown.py', '--')?

Is this really the only way to shutdown? Seems weird (in my case) to create an empty script that is just there to server the purpose to shutdown. I don't want to dump anything on exit. I would really just like to shutdown cleanly and easily. I can't imagine that this is such a huge problem.

EDIT: Well, not even the provided example works. I didn't test it before because it's not an option for me

@jsmnbom
Copy link
Member

jsmnbom commented Aug 16, 2017

How are you running your bot? Webhooks? Polling and updater.idle?

@Endogen
Copy link
Author

Endogen commented Aug 16, 2017

I'm using Polling and updater.idle. Just for reference, here is the implementation

@jsmnbom
Copy link
Member

jsmnbom commented Aug 16, 2017

What platform are you on? You didn't really provide the required info in the issue template.

@Endogen
Copy link
Author

Endogen commented Aug 16, 2017

Sorry for not providing the info. I didnt see it as an issue but rather as an question. Im on an Linux root server

@jsmnbom
Copy link
Member

jsmnbom commented Aug 16, 2017

Have you tried something like

updater.stop()
os.exit()

?

@Endogen
Copy link
Author

Endogen commented Aug 16, 2017

Yes. Doesnt work

@Endogen
Copy link
Author

Endogen commented Aug 16, 2017

Something like

os.system('kill %d' % os.getpid())

does also not work

@deepserket
Copy link

hi, speaking in the group gave me the solution,
you just need to make a function like

def shutdown():
    updater.stop()
    updater.is_idle = False

and call it in a new thread in the function that is called when the shutdown command is sent, like:

def stop(bot, update):
    threading.Thread(target=shutdown).start()

updater.dispatcher.add_handler(CommandHandler('stop', stop))

@Endogen
Copy link
Author

Endogen commented Aug 21, 2017

Thank you! I can confirm that it works perfectly. This really helps me a lot. I would argue that this needs to be part of the documentation / wiki

@Endogen Endogen closed this as completed Aug 21, 2017
@drmaquino
Copy link

hi, speaking in the group gave me the solution,
you just need to make a function like

def shutdown():
    updater.stop()
    updater.is_idle = False

and call it in a new thread in the function that is called when the shutdown command is sent, like:

def stop(bot, update):
    threading.Thread(target=shutdown).start()

updater.dispatcher.add_handler(CommandHandler('stop', stop))

just to add to your correct answer, remember to import threading on the module:
( just add "import threading" at the top! )

@michaelkuzmin
Copy link

I just spent so much time trying to replicate this. so for people like me I am going to provide the full code here:

from telegram.ext import Updater, InlineQueryHandler, CommandHandler
import requests
import re
import settings
import threading

def shutdown():
    updater.stop()
    updater.is_idle = False
def stop(bot, update):
    threading.Thread(target=shutdown).start()
def main():
    dp = updater.dispatcher
    dp.add_handler(CommandHandler('stop', stop))
    updater.bot.send_message(chat_id=settings.my_id, text="Here is a message from the bot")
    updater.start_polling()
    print("Bot is waiting for user input")
    updater.idle()
    print("Last row is reached")

updater = Updater(settings.AUTH_TOKEN)
if __name__ == '__main__':
    main()

where settings.py has AUTH_TOKEN='XXXXXXX:YYYYYYYYYYYYYYYYYYYYYYYYYYYYY'

@robert-cody
Copy link

Calling updater.stop() and then setting updater.is_idle to False is most incorrect way to stop a bot because it will lose all persistence, see telegram.ext.updater.Updater.signal_handler method.

The correct way would be to send usual fully supported by library signal:
os.kill(os.getpid(), signal.SIGINT)
You can call it in any callback without creating of extra thread.

@github-actions github-actions bot locked and limited conversation to collaborators Aug 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants