2015-11-05 13:52:33 +01:00
|
|
|
#!/usr/bin/env python
|
2015-12-21 21:18:53 +01:00
|
|
|
#
|
|
|
|
# A library that provides a Python interface to the Telegram Bot API
|
2017-05-14 12:18:29 +02:00
|
|
|
# Copyright (C) 2015-2017
|
2016-01-05 14:12:03 +01:00
|
|
|
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
|
2015-12-21 21:18:53 +01:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Lesser Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser Public License
|
|
|
|
# along with this program. If not, see [http://www.gnu.org/licenses/].
|
2016-01-13 17:09:35 +01:00
|
|
|
"""This module contains the Dispatcher class."""
|
2015-11-05 13:52:33 +01:00
|
|
|
|
2015-11-15 17:36:38 +01:00
|
|
|
import logging
|
2016-09-06 15:38:07 +02:00
|
|
|
import weakref
|
2015-11-11 13:33:03 +01:00
|
|
|
from functools import wraps
|
2016-09-06 15:38:07 +02:00
|
|
|
from threading import Thread, Lock, Event, current_thread, BoundedSemaphore
|
2016-01-06 15:35:55 +01:00
|
|
|
from time import sleep
|
2016-09-06 15:38:07 +02:00
|
|
|
from uuid import uuid4
|
2016-10-25 19:28:34 +02:00
|
|
|
from collections import defaultdict
|
2016-09-06 15:38:07 +02:00
|
|
|
|
2016-05-30 03:16:33 +02:00
|
|
|
from queue import Queue, Empty
|
2015-11-05 13:52:33 +01:00
|
|
|
|
2016-06-01 20:21:24 +02:00
|
|
|
from future.builtins import range
|
2016-04-16 19:25:38 +02:00
|
|
|
|
2016-08-20 22:01:07 +02:00
|
|
|
from telegram import TelegramError
|
2016-04-14 23:57:40 +02:00
|
|
|
from telegram.ext.handler import Handler
|
2016-07-15 01:30:54 +02:00
|
|
|
from telegram.utils.promise import Promise
|
2015-11-15 17:36:38 +01:00
|
|
|
|
2016-08-20 22:01:07 +02:00
|
|
|
logging.getLogger(__name__).addHandler(logging.NullHandler())
|
2016-02-09 22:08:27 +01:00
|
|
|
""":type: set[Thread]"""
|
2016-04-14 23:57:40 +02:00
|
|
|
DEFAULT_GROUP = 0
|
2015-11-06 00:24:01 +01:00
|
|
|
|
2015-11-11 13:33:03 +01:00
|
|
|
|
|
|
|
def run_async(func):
|
2016-09-06 15:38:07 +02:00
|
|
|
"""Function decorator that will run the function in a new thread.
|
|
|
|
|
|
|
|
Using this decorator is only possible when only a single Dispatcher exist in the system.
|
2015-11-11 13:33:03 +01:00
|
|
|
|
|
|
|
Args:
|
|
|
|
func (function): The function to run in the thread.
|
2016-09-06 15:38:07 +02:00
|
|
|
async_queue (Queue): The queue of the functions to be executed asynchronously.
|
2015-11-11 13:33:03 +01:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
function:
|
|
|
|
|
2016-09-06 15:38:07 +02:00
|
|
|
"""
|
2016-02-06 11:38:56 +01:00
|
|
|
|
2015-11-11 13:33:03 +01:00
|
|
|
@wraps(func)
|
2016-06-01 20:11:00 +02:00
|
|
|
def async_func(*args, **kwargs):
|
2016-09-06 15:38:07 +02:00
|
|
|
return Dispatcher.get_instance().run_async(func, *args, **kwargs)
|
2015-11-11 13:33:03 +01:00
|
|
|
|
|
|
|
return async_func
|
|
|
|
|
|
|
|
|
2016-04-15 16:20:37 +02:00
|
|
|
class Dispatcher(object):
|
2015-11-05 13:52:33 +01:00
|
|
|
"""
|
2015-11-22 14:47:38 +01:00
|
|
|
This class dispatches all kinds of updates to its registered handlers.
|
2015-12-21 20:25:31 +01:00
|
|
|
|
2015-11-05 13:52:33 +01:00
|
|
|
Args:
|
2015-11-16 20:43:35 +01:00
|
|
|
bot (telegram.Bot): The bot object that should be passed to the
|
2015-12-21 21:40:41 +01:00
|
|
|
handlers
|
2016-04-18 18:13:54 +02:00
|
|
|
update_queue (Queue): The synchronized queue that will contain the
|
|
|
|
updates.
|
2016-05-26 14:39:11 +02:00
|
|
|
job_queue (Optional[telegram.ext.JobQueue]): The ``JobQueue`` instance to pass onto handler
|
|
|
|
callbacks
|
|
|
|
workers (Optional[int]): Number of maximum concurrent worker threads for the ``@run_async``
|
|
|
|
decorator
|
2016-09-06 15:38:07 +02:00
|
|
|
|
2015-11-05 13:52:33 +01:00
|
|
|
"""
|
2016-09-06 15:38:07 +02:00
|
|
|
__singleton_lock = Lock()
|
|
|
|
__singleton_semaphore = BoundedSemaphore()
|
|
|
|
__singleton = None
|
|
|
|
logger = logging.getLogger(__name__)
|
2016-05-15 03:46:40 +02:00
|
|
|
|
2016-05-28 14:21:39 +02:00
|
|
|
def __init__(self, bot, update_queue, workers=4, exception_event=None, job_queue=None):
|
2015-11-05 13:52:33 +01:00
|
|
|
self.bot = bot
|
|
|
|
self.update_queue = update_queue
|
2016-05-26 14:39:11 +02:00
|
|
|
self.job_queue = job_queue
|
2016-09-27 09:21:35 +02:00
|
|
|
self.workers = workers
|
2016-04-14 23:57:40 +02:00
|
|
|
|
2016-10-25 19:28:34 +02:00
|
|
|
self.user_data = defaultdict(dict)
|
|
|
|
""":type: dict[int, dict]"""
|
|
|
|
self.chat_data = defaultdict(dict)
|
|
|
|
""":type: dict[int, dict]"""
|
|
|
|
|
2016-04-14 23:57:40 +02:00
|
|
|
self.handlers = {}
|
2016-04-25 09:18:26 +02:00
|
|
|
""":type: dict[int, list[Handler]"""
|
|
|
|
self.groups = []
|
|
|
|
""":type: list[int]"""
|
2015-11-05 13:52:33 +01:00
|
|
|
self.error_handlers = []
|
2016-04-14 23:57:40 +02:00
|
|
|
|
2015-11-15 17:36:38 +01:00
|
|
|
self.running = False
|
2016-02-06 09:10:08 +01:00
|
|
|
self.__stop_event = Event()
|
2016-02-06 11:38:56 +01:00
|
|
|
self.__exception_event = exception_event or Event()
|
2016-09-06 15:38:07 +02:00
|
|
|
self.__async_queue = Queue()
|
|
|
|
self.__async_threads = set()
|
|
|
|
|
|
|
|
# For backward compatibility, we allow a "singleton" mode for the dispatcher. When there's
|
|
|
|
# only one instance of Dispatcher, it will be possible to use the `run_async` decorator.
|
|
|
|
with self.__singleton_lock:
|
|
|
|
if self.__singleton_semaphore.acquire(blocking=0):
|
|
|
|
self._set_singleton(self)
|
2016-06-19 23:46:34 +02:00
|
|
|
else:
|
2016-09-06 15:38:07 +02:00
|
|
|
self._set_singleton(None)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def _reset_singleton(cls):
|
|
|
|
# NOTE: This method was added mainly for test_updater benefit and specifically pypy. Never
|
|
|
|
# call it in production code.
|
|
|
|
cls.__singleton_semaphore.release()
|
|
|
|
|
|
|
|
def _init_async_threads(self, base_name, workers):
|
|
|
|
base_name = '{}_'.format(base_name) if base_name else ''
|
|
|
|
|
|
|
|
for i in range(workers):
|
|
|
|
thread = Thread(target=self._pooled, name='{}{}'.format(base_name, i))
|
|
|
|
self.__async_threads.add(thread)
|
|
|
|
thread.start()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def _set_singleton(cls, val):
|
|
|
|
cls.logger.debug('Setting singleton dispatcher as %s', val)
|
|
|
|
cls.__singleton = weakref.ref(val) if val else None
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_instance(cls):
|
|
|
|
"""Get the singleton instance of this class.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Dispatcher
|
|
|
|
|
|
|
|
"""
|
|
|
|
if cls.__singleton is not None:
|
|
|
|
return cls.__singleton()
|
|
|
|
else:
|
|
|
|
raise RuntimeError('{} not initialized or multiple instances exist'.format(
|
|
|
|
cls.__name__))
|
|
|
|
|
|
|
|
def _pooled(self):
|
|
|
|
"""
|
|
|
|
A wrapper to run a thread in a thread pool
|
|
|
|
"""
|
|
|
|
thr_name = current_thread().getName()
|
|
|
|
while 1:
|
|
|
|
promise = self.__async_queue.get()
|
|
|
|
|
|
|
|
# If unpacking fails, the thread pool is being closed from Updater._join_async_threads
|
|
|
|
if not isinstance(promise, Promise):
|
|
|
|
self.logger.debug("Closing run_async thread %s/%d", thr_name,
|
|
|
|
len(self.__async_threads))
|
|
|
|
break
|
|
|
|
|
2017-02-26 22:27:03 +01:00
|
|
|
promise.run()
|
2016-09-06 15:38:07 +02:00
|
|
|
|
|
|
|
def run_async(self, func, *args, **kwargs):
|
|
|
|
"""Queue a function (with given args/kwargs) to be run asynchronously.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
func (function): The function to run in the thread.
|
|
|
|
args (Optional[tuple]): Arguments to `func`.
|
|
|
|
kwargs (Optional[dict]): Keyword arguments to `func`.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Promise
|
|
|
|
|
|
|
|
"""
|
|
|
|
# TODO: handle exception in async threads
|
|
|
|
# set a threading.Event to notify caller thread
|
|
|
|
promise = Promise(func, args, kwargs)
|
|
|
|
self.__async_queue.put(promise)
|
|
|
|
return promise
|
2015-11-15 17:36:38 +01:00
|
|
|
|
|
|
|
def start(self):
|
|
|
|
"""
|
2015-11-22 14:47:38 +01:00
|
|
|
Thread target of thread 'dispatcher'. Runs in background and processes
|
2015-11-15 17:36:38 +01:00
|
|
|
the update queue.
|
|
|
|
"""
|
|
|
|
|
2016-02-06 09:10:08 +01:00
|
|
|
if self.running:
|
|
|
|
self.logger.warning('already running')
|
|
|
|
return
|
|
|
|
|
2016-02-06 11:38:56 +01:00
|
|
|
if self.__exception_event.is_set():
|
|
|
|
msg = 'reusing dispatcher after exception event is forbidden'
|
|
|
|
self.logger.error(msg)
|
|
|
|
raise TelegramError(msg)
|
|
|
|
|
2016-09-27 09:21:35 +02:00
|
|
|
self._init_async_threads(uuid4(), self.workers)
|
2016-02-06 09:10:08 +01:00
|
|
|
self.running = True
|
2016-03-15 02:56:20 +01:00
|
|
|
self.logger.debug('Dispatcher started')
|
2016-02-06 09:10:08 +01:00
|
|
|
|
2016-06-19 23:46:34 +02:00
|
|
|
while 1:
|
2016-02-06 09:10:08 +01:00
|
|
|
try:
|
|
|
|
# Pop update from update queue.
|
2016-04-14 23:57:40 +02:00
|
|
|
update = self.update_queue.get(True, 1)
|
2016-02-06 09:10:08 +01:00
|
|
|
except Empty:
|
|
|
|
if self.__stop_event.is_set():
|
2016-03-15 02:56:20 +01:00
|
|
|
self.logger.debug('orderly stopping')
|
2016-02-06 11:38:56 +01:00
|
|
|
break
|
2016-04-14 22:23:02 +02:00
|
|
|
elif self.__exception_event.is_set():
|
2016-05-15 03:46:40 +02:00
|
|
|
self.logger.critical('stopping due to exception in another thread')
|
2016-02-06 09:10:08 +01:00
|
|
|
break
|
|
|
|
continue
|
|
|
|
|
2016-04-14 23:57:40 +02:00
|
|
|
self.logger.debug('Processing Update: %s' % update)
|
2016-06-19 23:46:34 +02:00
|
|
|
self.process_update(update)
|
2016-02-06 09:10:08 +01:00
|
|
|
|
|
|
|
self.running = False
|
2016-03-15 02:56:20 +01:00
|
|
|
self.logger.debug('Dispatcher thread stopped')
|
2015-11-15 17:36:38 +01:00
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
"""
|
|
|
|
Stops the thread
|
|
|
|
"""
|
2016-02-06 09:10:08 +01:00
|
|
|
if self.running:
|
|
|
|
self.__stop_event.set()
|
|
|
|
while self.running:
|
|
|
|
sleep(0.1)
|
|
|
|
self.__stop_event.clear()
|
2015-11-15 17:36:38 +01:00
|
|
|
|
2016-09-06 15:38:07 +02:00
|
|
|
# async threads must be join()ed only after the dispatcher thread was joined,
|
|
|
|
# otherwise we can still have new async threads dispatched
|
|
|
|
threads = list(self.__async_threads)
|
|
|
|
total = len(threads)
|
|
|
|
|
|
|
|
# Stop all threads in the thread pool by put()ting one non-tuple per thread
|
|
|
|
for i in range(total):
|
|
|
|
self.__async_queue.put(None)
|
|
|
|
|
|
|
|
for i, thr in enumerate(threads):
|
|
|
|
self.logger.debug('Waiting for async thread {0}/{1} to end'.format(i + 1, total))
|
|
|
|
thr.join()
|
|
|
|
self.__async_threads.remove(thr)
|
|
|
|
self.logger.debug('async thread {0}/{1} has ended'.format(i + 1, total))
|
|
|
|
|
|
|
|
@property
|
|
|
|
def has_running_threads(self):
|
|
|
|
return self.running or bool(self.__async_threads)
|
|
|
|
|
2016-06-19 23:46:34 +02:00
|
|
|
def process_update(self, update):
|
2015-11-15 17:36:38 +01:00
|
|
|
"""
|
|
|
|
Processes a single update.
|
|
|
|
|
|
|
|
Args:
|
2016-04-18 18:13:54 +02:00
|
|
|
update (object):
|
2015-11-15 17:36:38 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
# An error happened while polling
|
|
|
|
if isinstance(update, TelegramError):
|
2016-06-19 23:46:34 +02:00
|
|
|
self.dispatch_error(None, update)
|
2015-11-05 13:52:33 +01:00
|
|
|
|
2016-04-14 23:57:40 +02:00
|
|
|
else:
|
2016-04-25 09:18:26 +02:00
|
|
|
for group in self.groups:
|
|
|
|
for handler in self.handlers[group]:
|
2016-04-14 23:57:40 +02:00
|
|
|
try:
|
2016-05-16 15:02:51 +02:00
|
|
|
if handler.check_update(update):
|
|
|
|
handler.handle_update(update, self)
|
2016-04-18 17:13:06 +02:00
|
|
|
break
|
2016-04-14 23:57:40 +02:00
|
|
|
# Dispatch any errors
|
|
|
|
except TelegramError as te:
|
2016-05-15 03:46:40 +02:00
|
|
|
self.logger.warn('A TelegramError was raised while processing the '
|
|
|
|
'Update.')
|
2016-04-14 23:57:40 +02:00
|
|
|
|
|
|
|
try:
|
2016-06-19 23:46:34 +02:00
|
|
|
self.dispatch_error(update, te)
|
2016-04-25 09:19:39 +02:00
|
|
|
except Exception:
|
2016-05-15 03:46:40 +02:00
|
|
|
self.logger.exception('An uncaught error was raised while '
|
|
|
|
'handling the error')
|
2016-04-18 17:13:06 +02:00
|
|
|
finally:
|
|
|
|
break
|
2016-04-14 23:57:40 +02:00
|
|
|
|
|
|
|
# Errors should not stop the thread
|
2016-04-25 09:19:39 +02:00
|
|
|
except Exception:
|
2016-05-15 03:46:40 +02:00
|
|
|
self.logger.exception('An uncaught error was raised while '
|
|
|
|
'processing the update')
|
2016-04-18 17:13:06 +02:00
|
|
|
break
|
2016-04-14 23:57:40 +02:00
|
|
|
|
2016-04-28 14:29:27 +02:00
|
|
|
def add_handler(self, handler, group=DEFAULT_GROUP):
|
2016-04-14 23:57:40 +02:00
|
|
|
"""
|
2016-04-25 09:45:55 +02:00
|
|
|
Register a handler.
|
|
|
|
|
|
|
|
TL;DR: Order and priority counts. 0 or 1 handlers per group will be
|
|
|
|
used.
|
|
|
|
|
|
|
|
A handler must be an instance of a subclass of
|
|
|
|
telegram.ext.Handler. All handlers are organized in groups with a
|
|
|
|
numeric value. The default group is 0. All groups will be evaluated for
|
|
|
|
handling an update, but only 0 or 1 handler per group will be used.
|
|
|
|
|
|
|
|
The priority/order of handlers is determined as follows:
|
|
|
|
|
|
|
|
* Priority of the group (lower group number == higher priority)
|
|
|
|
|
|
|
|
* The first handler in a group which should handle an update will be
|
|
|
|
used. Other handlers from the group will not be used. The order in
|
|
|
|
which handlers were added to the group defines the priority.
|
2015-11-15 17:36:38 +01:00
|
|
|
|
2015-11-05 13:52:33 +01:00
|
|
|
Args:
|
2016-11-01 06:53:51 +01:00
|
|
|
handler (telegram.ext.Handler): A Handler instance
|
2016-04-25 09:45:55 +02:00
|
|
|
group (Optional[int]): The group identifier. Default is 0
|
2015-11-05 13:52:33 +01:00
|
|
|
"""
|
2015-11-15 17:36:38 +01:00
|
|
|
|
2016-04-14 23:57:40 +02:00
|
|
|
if not isinstance(handler, Handler):
|
2016-05-15 03:46:40 +02:00
|
|
|
raise TypeError('handler is not an instance of {0}'.format(Handler.__name__))
|
2016-04-25 09:44:55 +02:00
|
|
|
if not isinstance(group, int):
|
|
|
|
raise TypeError('group is not int')
|
2015-11-05 13:52:33 +01:00
|
|
|
|
2016-04-14 23:57:40 +02:00
|
|
|
if group not in self.handlers:
|
|
|
|
self.handlers[group] = list()
|
2016-04-25 09:18:26 +02:00
|
|
|
self.groups.append(group)
|
|
|
|
self.groups = sorted(self.groups)
|
2015-11-05 13:52:33 +01:00
|
|
|
|
2016-04-14 23:57:40 +02:00
|
|
|
self.handlers[group].append(handler)
|
2015-11-05 13:52:33 +01:00
|
|
|
|
2016-04-28 14:29:27 +02:00
|
|
|
def remove_handler(self, handler, group=DEFAULT_GROUP):
|
2015-11-05 13:52:33 +01:00
|
|
|
"""
|
2016-04-14 23:57:40 +02:00
|
|
|
Remove a handler from the specified group
|
2015-11-15 17:36:38 +01:00
|
|
|
|
2015-11-05 13:52:33 +01:00
|
|
|
Args:
|
2016-11-01 06:53:51 +01:00
|
|
|
handler (telegram.ext.Handler): A Handler instance
|
2016-04-18 18:13:54 +02:00
|
|
|
group (optional[object]): The group identifier. Default is 0
|
2015-11-05 13:52:33 +01:00
|
|
|
"""
|
2016-04-14 23:57:40 +02:00
|
|
|
if handler in self.handlers[group]:
|
|
|
|
self.handlers[group].remove(handler)
|
2016-04-25 09:18:45 +02:00
|
|
|
if not self.handlers[group]:
|
|
|
|
del self.handlers[group]
|
|
|
|
self.groups.remove(group)
|
2015-11-15 17:36:38 +01:00
|
|
|
|
2016-04-28 14:29:27 +02:00
|
|
|
def add_error_handler(self, callback):
|
2015-11-05 13:52:33 +01:00
|
|
|
"""
|
2015-11-22 14:47:38 +01:00
|
|
|
Registers an error handler in the Dispatcher.
|
2015-11-15 17:36:38 +01:00
|
|
|
|
2015-11-05 13:52:33 +01:00
|
|
|
Args:
|
2016-04-18 18:13:54 +02:00
|
|
|
handler (function): A function that takes ``Bot, Update,
|
|
|
|
TelegramError`` as arguments.
|
2015-11-05 13:52:33 +01:00
|
|
|
"""
|
2015-11-15 17:36:38 +01:00
|
|
|
|
2016-04-14 23:57:40 +02:00
|
|
|
self.error_handlers.append(callback)
|
2016-01-04 17:31:06 +01:00
|
|
|
|
2016-04-28 14:29:27 +02:00
|
|
|
def remove_error_handler(self, callback):
|
2015-11-05 13:52:33 +01:00
|
|
|
"""
|
|
|
|
De-registers an error handler.
|
|
|
|
|
|
|
|
Args:
|
2016-04-18 18:13:54 +02:00
|
|
|
handler (function):
|
2015-11-05 13:52:33 +01:00
|
|
|
"""
|
|
|
|
|
2016-04-14 23:57:40 +02:00
|
|
|
if callback in self.error_handlers:
|
|
|
|
self.error_handlers.remove(callback)
|
2016-01-04 17:31:06 +01:00
|
|
|
|
2016-06-19 23:46:34 +02:00
|
|
|
def dispatch_error(self, update, error):
|
2015-11-05 13:52:33 +01:00
|
|
|
"""
|
2015-11-22 14:47:38 +01:00
|
|
|
Dispatches an error.
|
2015-11-05 13:52:33 +01:00
|
|
|
|
|
|
|
Args:
|
2016-04-18 18:13:54 +02:00
|
|
|
update (object): The update that caused the error
|
2015-11-05 13:52:33 +01:00
|
|
|
error (telegram.TelegramError): The Telegram error that was raised.
|
|
|
|
"""
|
|
|
|
|
2016-04-14 23:57:40 +02:00
|
|
|
for callback in self.error_handlers:
|
|
|
|
callback(self.bot, update, error)
|