hopefully clearer exception handling and _work fn

This commit is contained in:
əlemi 2022-04-23 22:09:53 +02:00
parent 261d18f854
commit e52b4d5a37
No known key found for this signature in database
GPG key ID: BBCBFE5D7244634E

View file

@ -1,15 +1,13 @@
import re
import json
import logging
import asyncio
import datetime
import uuid
import pkg_resources
from typing import List, Dict, Optional, Union, Any, Type, get_args, get_origin, get_type_hints, Set, Callable
from typing import Coroutine, List, Dict, Optional, Union, Any, Type, get_args, get_origin, get_type_hints, Set, Callable
from time import time
from dataclasses import dataclass, MISSING, fields
from configparser import ConfigParser
from configparser import ConfigParser, SectionProxy
from apscheduler.schedulers.asyncio import AsyncIOScheduler
@ -99,7 +97,7 @@ class Addon:
async def cleanup(self):
pass
class Notifier(Addon): # TODO this should be an Addon too!
class Notifier(Addon):
_report_functions : List[Callable]
def __init__(self, *args, **kwargs):
@ -212,7 +210,7 @@ class Treepuncher(
self.storage._set_state(state)
@property
def cfg(self) -> ConfigParser:
def cfg(self) -> SectionProxy:
return self.config["Treepuncher"]
@property
@ -265,6 +263,7 @@ class Treepuncher(
return module
async def _work(self):
try:
if "force_proto" in self.cfg:
self.dispatcher.set_proto(self.cfg.getint('force_proto'))
else:
@ -272,23 +271,25 @@ class Treepuncher(
server_data = await self.info()
if "version" in server_data and "protocol" in server_data["version"]:
self.dispatcher.set_proto(server_data['version']['protocol'])
except (ConnectionRefusedError, OSError) as e:
except OSError as e:
self.logger.error("Connection error : %s", str(e))
except Exception:
self.logger.exception("Unhandled exception while pinging server")
self.dispatcher.whitelist(self.callback_keys(filter=Packet))
while self._processing:
try:
self.dispatcher.whitelist(self.callback_keys(filter=Packet))
await self.join()
except (ConnectionRefusedError, OSError) as e:
except OSError as e:
self.logger.error("Connection error : %s", str(e))
except AuthException as e:
self.logger.error("Auth exception : [%s|%d] %s (%s)", e.endpoint, e.code, e.data, e.kwargs)
break
if self._processing: # don't sleep if Treepuncher is stopping
await asyncio.sleep(self.cfg.getfloat('reconnect_delay', fallback=5))
except Exception:
self.logger.exception("Unhandled exception")
break
if self._processing:
await asyncio.sleep(self.config['Treepuncher'].getfloat('reconnect_delay', fallback=5))
if self._processing:
await self.stop(force=True)