-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from guyshe/release/1.4
Release/1.4
- Loading branch information
Showing
9 changed files
with
106 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,5 @@ venv/ | |
.idea/ | ||
*.sqlite | ||
*.egg-info | ||
build/ | ||
dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
from telegram_handler.blocking_telegram_handler import BlockingTelegramHandler | ||
from telegram_handler.async_telegram_handler import AsyncTelegramHandler | ||
from telegram_handler.handler import TelegramLoggingHandler |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
FLUSH_INTERVAL = 0.5 | ||
|
||
API_HOST = 'api.telegram.org' | ||
API_URL = f'https://{API_HOST}/bot{{bot_token}}/sendMessage?' \ | ||
f'chat_id=@{{channel_name}}&text={{message}}' | ||
NEED_COOLDOWN_STATUS = 429 | ||
COOLDOWN_TIME = 1 | ||
RETRY_ATTEMPTS = 6 | ||
f'chat_id=@{{channel_name}}&parse_mode=HTML&text={{message}}' | ||
RETRY_COOLDOWN_TIME = 3 | ||
MAX_RETRYS = 20 | ||
# max valid size is 2048, we take buffer to be on the safe side | ||
MAX_MESSAGE_SIZE = 1900 | ||
MAX_TELEGRAM_MESSAGES = 300 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
from distutils.log import Log | ||
import logging | ||
from time import sleep | ||
from threading import Thread, RLock | ||
from queue import Empty, Queue | ||
import requests | ||
from retry import retry | ||
from telegram_handler.consts import API_URL, RETRY_COOLDOWN_TIME, MAX_RETRYS, MAX_MESSAGE_SIZE, FLUSH_INTERVAL | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class TelegramLoggingHandler(logging.Handler): | ||
|
||
_sentinel = None | ||
|
||
def __init__(self, bot_token, channel_name, level=logging.NOTSET): | ||
super().__init__(level) | ||
self.bot_token = bot_token | ||
self.channel_name = channel_name | ||
self._buffer_lock = RLock() | ||
self._buffer = '' | ||
self.telegram_messages_queue = Queue() | ||
self._writer_thread = None | ||
self._start_writer_thread() | ||
|
||
@retry((requests.RequestException), | ||
tries=MAX_RETRYS, | ||
delay=RETRY_COOLDOWN_TIME, | ||
logger=logger) | ||
def write(self, message): | ||
url = API_URL.format(bot_token=self.bot_token, | ||
channel_name=self.channel_name, | ||
message=message) | ||
requests.get(url).raise_for_status() | ||
|
||
def emit(self, record: logging.LogRecord) -> None: | ||
message = self.format(record) | ||
self._buffer_lock.acquire() | ||
new_buffer = f'{self._buffer}\n{message}' | ||
if len(new_buffer) > MAX_MESSAGE_SIZE: | ||
self._flush_buffer() | ||
new_buffer = message[:MAX_MESSAGE_SIZE] | ||
self._buffer = new_buffer | ||
self._buffer_lock.release() | ||
|
||
def close(self): | ||
self._flush_buffer() | ||
self.telegram_messages_queue.put(TelegramLoggingHandler._sentinel) | ||
self.telegram_messages_queue.join() | ||
self._writer_thread.join() | ||
|
||
def _flush_buffer(self): | ||
self._buffer_lock.acquire() | ||
# Avoid unnecessary flushing | ||
if self._buffer != '': | ||
self.telegram_messages_queue.put(self._buffer[:MAX_MESSAGE_SIZE]) | ||
self._buffer = '' | ||
self._buffer_lock.release() | ||
|
||
def _write_manager(self): | ||
q = self.telegram_messages_queue | ||
while True: | ||
try: | ||
message = q.get(timeout=FLUSH_INTERVAL) | ||
if message is self._sentinel: | ||
q.task_done() | ||
break | ||
self.write(message) | ||
q.task_done() | ||
except Empty: | ||
self._flush_buffer() | ||
|
||
def _start_writer_thread(self): | ||
self._writer_thread = Thread(target=self._write_manager) | ||
self._writer_thread.daemon = True | ||
self._writer_thread.start() |