-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Error intervals to stop flooding logs & set non_stop to true by default #2332
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -246,7 +246,7 @@ def _setup_change_detector(self, path_to_watch: str) -> None: | |
self.event_observer.schedule(self.event_handler, path, recursive=True) | ||
self.event_observer.start() | ||
|
||
async def polling(self, non_stop: bool=False, skip_pending=False, interval: int=0, timeout: int=20, | ||
async def polling(self, non_stop: bool=True, skip_pending=False, interval: int=0, timeout: int=20, | ||
request_timeout: Optional[int]=None, allowed_updates: Optional[List[str]]=None, | ||
none_stop: Optional[bool]=None, restart_on_change: Optional[bool]=False, path_to_watch: Optional[str]=None): | ||
""" | ||
|
@@ -257,11 +257,6 @@ async def polling(self, non_stop: bool=False, skip_pending=False, interval: int= | |
|
||
Always gets updates. | ||
|
||
.. note:: | ||
|
||
Set non_stop=True if you want your bot to continue receiving updates | ||
if there is an error. | ||
|
||
.. note:: | ||
|
||
Install watchdog and psutil before using restart_on_change option. | ||
|
@@ -393,6 +388,15 @@ def __hide_token(self, message: str) -> str: | |
return message.replace(code, "*" * len(code)) | ||
else: | ||
return message | ||
|
||
async def _handle_error_interval(self, error_interval: float): | ||
logger.debug('Waiting for %s seconds before retrying', error_interval) | ||
await asyncio.sleep(error_interval) | ||
if error_interval * 2 < 60: # same logic as sync | ||
error_interval *= 2 | ||
else: | ||
error_interval = 60 | ||
return error_interval | ||
|
||
async def _process_polling(self, non_stop: bool=False, interval: int=0, timeout: int=20, | ||
request_timeout: int=None, allowed_updates: Optional[List[str]]=None): | ||
|
@@ -426,16 +430,18 @@ async def _process_polling(self, non_stop: bool=False, interval: int=0, timeout: | |
|
||
self._polling = True | ||
|
||
error_interval = 0.25 | ||
|
||
try: | ||
while self._polling: | ||
try: | ||
|
||
updates = await self.get_updates(offset=self.offset, allowed_updates=allowed_updates, timeout=timeout, request_timeout=request_timeout) | ||
if updates: | ||
self.offset = updates[-1].update_id + 1 | ||
# noinspection PyAsyncCall | ||
asyncio.create_task(self.process_new_updates(updates)) # Seperate task for processing updates | ||
if interval: await asyncio.sleep(interval) | ||
error_interval = 0.25 # drop error_interval if no errors | ||
|
||
except KeyboardInterrupt: | ||
return | ||
|
@@ -446,9 +452,10 @@ async def _process_polling(self, non_stop: bool=False, interval: int=0, timeout: | |
if not handled: | ||
logger.error('Unhandled exception (full traceback for debug level): %s', self.__hide_token(str(e))) | ||
logger.debug(self.__hide_token(traceback.format_exc())) | ||
error_interval = await self._handle_error_interval(error_interval) | ||
|
||
if non_stop or handled: | ||
await asyncio.sleep(2) | ||
#await asyncio.sleep(2) # used error_interval instead | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed behaviour! In old version sleep(2) is for non_stop or handled. In new version it is for not hadled and independently from non_stop.
I propose:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so we still don't need sleep for handled? or should I add sleep to handled (2 seconds) back? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to sleep both for handled (as before) and not handled (it's reasonable). But I propose only if non_stop: otherwise sleeping is useless, because polling will stop anyway. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wait, before, script slept 2 seconds for handled or non_stop. So, should I:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this will not affect not handled: there will be no sleeping as you said, the sleep will be useless. currently we are talking about handled. Or, do you think the user should manually do that and not rely on internal behaviour? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am talking about the need to have "if handled" condition. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It is not. But if you copy my version - it will work both for handled and not handled. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It is not. I don't know what you did, but if you'll take my version - it will work both for handled and not handled. Isn't it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah, sorry |
||
continue | ||
else: | ||
return | ||
|
@@ -457,6 +464,7 @@ async def _process_polling(self, non_stop: bool=False, interval: int=0, timeout: | |
if not handled: | ||
logger.error('Unhandled exception (full traceback for debug level): %s', self.__hide_token(str(e))) | ||
logger.debug(self.__hide_token(traceback.format_exc())) | ||
error_interval = await self._handle_error_interval(error_interval) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The same here.
|
||
|
||
if non_stop or handled: | ||
continue | ||
|
@@ -467,6 +475,7 @@ async def _process_polling(self, non_stop: bool=False, interval: int=0, timeout: | |
if not handled: | ||
logger.error('Unhandled exception (full traceback for debug level): %s', str(e)) | ||
logger.debug(traceback.format_exc()) | ||
error_interval = await self._handle_error_interval(error_interval) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And here. |
||
|
||
if non_stop or handled: | ||
continue | ||
|
@@ -4014,6 +4023,10 @@ async def send_document( | |
if reply_parameters and (reply_parameters.allow_sending_without_reply is None): | ||
reply_parameters.allow_sending_without_reply = self.allow_sending_without_reply | ||
|
||
if isinstance(document, types.InputFile) and visible_file_name: | ||
# inputfile name ignored, warn | ||
logger.warning('Cannot use both InputFile and visible_file_name. InputFile name will be ignored.') | ||
|
||
return types.Message.de_json( | ||
await asyncio_helper.send_data( | ||
self.token, chat_id, document, 'document', | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -130,6 +130,8 @@ def _prepare_data(params=None, files=None): | |
if isinstance(f, tuple): | ||
if len(f) == 2: | ||
file_name, file = f | ||
if isinstance(file, types.InputFile): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cannot verify this, never worked with InputFile. |
||
file = file.file | ||
else: | ||
raise ValueError('Tuple must have exactly 2 elements: filename, fileobj') | ||
elif isinstance(f, types.InputFile): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7735,8 +7735,11 @@ class InputFile: | |
InputFile(pathlib.Path('/path/to/file/file.txt')) | ||
) | ||
""" | ||
def __init__(self, file) -> None: | ||
self._file, self.file_name = self._resolve_file(file) | ||
def __init__(self, file: Union[str, IOBase, Path], file_name: Optional[str] = None): | ||
self._file, self._file_name = self._resolve_file(file) | ||
if file_name: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not ) |
||
self._file_name = file_name | ||
|
||
|
||
@staticmethod | ||
def _resolve_file(file): | ||
|
@@ -7757,6 +7760,13 @@ def file(self): | |
File object. | ||
""" | ||
return self._file | ||
|
||
@property | ||
def file_name(self): | ||
""" | ||
File name. | ||
""" | ||
return self._file_name | ||
|
||
|
||
class ForumTopicCreated(JsonDeserializable): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
files[key] = (value.file_name, value.file)
visible_file_name will be ignoread, isn't it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it returns a tuple and in apihelper, tuple value[0] is unchanged, thus ignoring the inputfile naming