X Tutup
Skip to content

Added user defined function for updater's signalHandler#512

Merged
jh0ker merged 5 commits intopython-telegram-bot:masterfrom
d-qoi:UserSignalHandler
Mar 26, 2017
Merged

Added user defined function for updater's signalHandler#512
jh0ker merged 5 commits intopython-telegram-bot:masterfrom
d-qoi:UserSignalHandler

Conversation

@d-qoi
Copy link
Copy Markdown
Contributor

@d-qoi d-qoi commented Feb 4, 2017

There was no good way to add new handlers for signal without overloading the ones updater.idle() created.
While updater.idle() is very simple, I thing that it is better to add this rather than encouraging users to re-write those functions.

The use of this is for anything that should be closed, like database connections, or shut down cleanly.

If I have a database connection at db

def signalHandler(signum, frame):
    logger.info("Closing DB connections.")
    db.close()

I can insert it into the updater.

updater = Updater('authToken', user_sig_handler=signalHandler)

and assuming everything else is self as default, and logger is set to debug, we will get an output like this:

2017-02-04 20:17:10,935 - JobQueue - DEBUG - JobQueue thread stopped
2017-02-04 20:17:10,936 - telegram.ext.updater - DEBUG - Stopping Updater and Dispatcher...
2017-02-04 20:17:10,936 - telegram.ext.updater - DEBUG - Requesting Dispatcher to stop...
2017-02-04 20:17:11,878 - telegram.ext.dispatcher - DEBUG - orderly stopping
2017-02-04 20:17:11,878 - telegram.ext.dispatcher - DEBUG - Dispatcher thread stopped
2017-02-04 20:17:11,879 - telegram.ext.updater - DEBUG - dispatcher - ended
2017-02-04 20:17:11,947 - telegram.ext.dispatcher - DEBUG - Waiting for async thread 1/4 to end
2017-02-04 20:17:11,949 - telegram.ext.dispatcher - DEBUG - Closing run_async thread cbaf8766-b151-452f-a7f7-4fc982872ade_2/4
2017-02-04 20:17:11,949 - telegram.ext.dispatcher - DEBUG - Closing run_async thread cbaf8766-b151-452f-a7f7-4fc982872ade_3/4
2017-02-04 20:17:11,950 - telegram.ext.dispatcher - DEBUG - Closing run_async thread cbaf8766-b151-452f-a7f7-4fc982872ade_1/4
2017-02-04 20:17:11,950 - telegram.ext.dispatcher - DEBUG - Closing run_async thread cbaf8766-b151-452f-a7f7-4fc982872ade_0/4
2017-02-04 20:17:11,951 - telegram.ext.dispatcher - DEBUG - async thread 1/4 has ended
2017-02-04 20:17:11,952 - telegram.ext.dispatcher - DEBUG - Waiting for async thread 2/4 to end
2017-02-04 20:17:11,952 - telegram.ext.dispatcher - DEBUG - async thread 2/4 has ended
2017-02-04 20:17:11,952 - telegram.ext.dispatcher - DEBUG - Waiting for async thread 3/4 to end
2017-02-04 20:17:11,952 - telegram.ext.dispatcher - DEBUG - async thread 3/4 has ended
2017-02-04 20:17:11,953 - telegram.ext.dispatcher - DEBUG - Waiting for async thread 4/4 to end
2017-02-04 20:17:11,953 - telegram.ext.dispatcher - DEBUG - async thread 4/4 has ended
2017-02-04 20:17:11,953 - telegram.ext.updater - DEBUG - Waiting for dispatcher thread to end
2017-02-04 20:17:11,953 - telegram.ext.updater - DEBUG - dispatcher thread has ended
2017-02-04 20:17:11,954 - telegram.ext.updater - DEBUG - Waiting for updater thread to end
2017-02-04 20:17:14,332 - telegram.bot - DEBUG - No new updates found.
2017-02-04 20:17:14,332 - telegram.bot - DEBUG - []
2017-02-04 20:17:14,333 - telegram.bot - DEBUG - Exiting: getUpdates
2017-02-04 20:17:14,333 - telegram.ext.updater - DEBUG - updater - ended
2017-02-04 20:17:14,333 - telegram.ext.updater - DEBUG - updater thread has ended
2017-02-04 20:17:14,338 - __main__ - INFO - Closing DB connections.

Which ends with the log output in the user defined signal handler, meaning the code has run.

@d-qoi
Copy link
Copy Markdown
Contributor Author

d-qoi commented Feb 4, 2017

There are 3 tests that fail with make test but those failed when I initially pulled from upstream master.

I am currently writing a test for this, I will update this pull request once it is written. If you would like me to squash the commits, I am happy to.

bot (Optional[Bot]): A pre-initialized bot instance. If a pre-initizlied bot is used, it is
the user's responsibility to create it using a `Request` instance with a large enough
connection pool.
user_sig_handler (Optional[function]: A function that takes ``signum, frame`` as positional arguments.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're missing a closing paranthesis here

@jh0ker
Copy link
Copy Markdown
Member

jh0ker commented Feb 13, 2017

I left one minor request, everything else seems good to me

@jh0ker
Copy link
Copy Markdown
Member

jh0ker commented Feb 27, 2017

Python 2.7 tests fail at test_updater.py:767 because nonlocal is unavailable there. Also, yapf and flake8 precommit hooks fail (see Python 3.5 build on Travis). If you fix those two issues, we're good to merge :)

@jh0ker jh0ker merged commit 5b14b13 into python-telegram-bot:master Mar 26, 2017
@github-actions github-actions bot locked and limited conversation to collaborators Aug 24, 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

Successfully merging this pull request may close these issues.

2 participants

X Tutup