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