X Tutup
Skip to content

Commit 5b14b13

Browse files
d-qoijh0ker
authored andcommitted
Added user defined function for updater's signalHandler (python-telegram-bot#512)
* Added user defined function for updater's signalHandler * Added test_userSignal to test_updater * Added test_userSignal to test_updater Fixing paren
1 parent 5897aff commit 5b14b13

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ The following wonderful people contributed directly or indirectly to this projec
1414
- `Anton Tagunov <https://github.com/anton-tagunov>`_
1515
- `Balduro <https://github.com/Balduro>`_
1616
- `bimmlerd <https://github.com/bimmlerd>`_
17+
- `d-qoi <https://github.com/d-qoi>`
1718
- `daimajia <https://github.com/daimajia>`_
1819
- `Eli Gao <https://github.com/eligao>`_
1920
- `ErgoZ Riftbit Vaper <https://github.com/ergoz>`_

telegram/ext/updater.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ class Updater(object):
6262
bot (Optional[Bot]): A pre-initialized bot instance. If a pre-initizlied bot is used, it is
6363
the user's responsibility to create it using a `Request` instance with a large enough
6464
connection pool.
65+
user_sig_handler (Optional[function]): Takes ``signum, frame`` as positional arguments.
66+
This will be called when a signal is received, defaults are (SIGINT, SIGTERM, SIGABRT)
67+
setable with Updater.idle(stop_signals=(signals))
6568
request_kwargs (Optional[dict]): Keyword args to control the creation of a request object
6669
(ignored if `bot` argument is used).
6770
@@ -71,7 +74,14 @@ class Updater(object):
7174
"""
7275
_request = None
7376

74-
def __init__(self, token=None, base_url=None, workers=4, bot=None, request_kwargs=None):
77+
def __init__(self,
78+
token=None,
79+
base_url=None,
80+
workers=4,
81+
bot=None,
82+
user_sig_handler=None,
83+
request_kwargs=None):
84+
7585
if (token is None) and (bot is None):
7686
raise ValueError('`token` or `bot` must be passed')
7787
if (token is not None) and (bot is not None):
@@ -92,6 +102,7 @@ def __init__(self, token=None, base_url=None, workers=4, bot=None, request_kwarg
92102
request_kwargs['con_pool_size'] = workers + 4
93103
self._request = Request(**request_kwargs)
94104
self.bot = Bot(token, base_url, request=self._request)
105+
self.user_sig_handler = user_sig_handler
95106
self.update_queue = Queue()
96107
self.job_queue = JobQueue(self.bot)
97108
self.__exception_event = Event()
@@ -426,6 +437,8 @@ def signal_handler(self, signum, frame):
426437
self.is_idle = False
427438
if self.running:
428439
self.stop()
440+
if self.user_sig_handler:
441+
self.user_sig_handler(signum, frame)
429442
else:
430443
self.logger.warning('Exiting immediately!')
431444
import os

tests/test_updater.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,23 @@ def test_idle(self):
790790
sleep(1)
791791
self.assertFalse(self.updater.running)
792792

793+
def test_userSignal(self):
794+
self._setup_updater('Test7', messages=0)
795+
796+
tempVar = {'a': 0}
797+
798+
def userSignalInc(signum, frame):
799+
tempVar['a'] = 1
800+
801+
self.updater.user_sig_handler = userSignalInc
802+
self.updater.start_polling(poll_interval=0.01)
803+
Thread(target=self.signalsender).start()
804+
self.updater.idle()
805+
# If we get this far, idle() ran through
806+
sleep(1)
807+
self.assertFalse(self.updater.running)
808+
self.assertTrue(tempVar['a'] != 0)
809+
793810
def test_createBot(self):
794811
self.updater = Updater('123:abcd')
795812
self.assertIsNotNone(self.updater.bot)

0 commit comments

Comments
 (0)
X Tutup