config: Make max_retry_wait configurable.

This commit is contained in:
Brett Smith 2020-04-20 10:01:22 -04:00
parent 74cdc1e4a8
commit 96df7f89b2
3 changed files with 21 additions and 2 deletions

View file

@ -21,11 +21,14 @@ Settings for the entire bot are configured in a ``[Bot]`` section::
password = ExamplePassword
nickname = FowardBot
loglevel = warning
max_retry_wait = 300
``jid`` and ``password`` are the required details for the bot's connection. The bot will use ``nickname`` when it joins MUCs and sends message, if specified.
Logs are written to standard error. The ``loglevel`` setting controls how much is logged. You can specify ``debug``, ``info``, ``warning``, ``error``, or ``critical``.
``max_retry_wait`` specifies the longest amount of time to wait before retrying operations, in seconds. You can specify any floating-point value >= 5. The default is 300.
Forwarding rules
~~~~~~~~~~~~~~~~

View file

@ -15,12 +15,12 @@ class XEP(enum.Enum):
class ForwardBot(slixmpp.ClientXMPP):
MAX_QUERY_WAIT = 300
MUC_FEATURE = 'http://jabber.org/protocol/muc'
def __init__(self, config):
super().__init__(config.bot_jid().full, config.bot_password())
self.nick = config.bot_nick()
self.max_retry_wait = config.get_max_retry_wait(300)
self.forwards = dict(config.forwards())
self.jid_mtype_map = {}
@ -49,7 +49,7 @@ class ForwardBot(slixmpp.ClientXMPP):
query['host'], error.iq['error']['condition'])
except IqTimeout:
try:
wait_secs = min(query['wait_secs'] * 2, self.MAX_QUERY_WAIT)
wait_secs = min(query['wait_secs'] * 2, self.max_retry_wait)
except KeyError:
wait_secs = 10
logger.warning("queryhost: timeout querying %r: will retry in %s seconds",

View file

@ -14,6 +14,7 @@ class Config(configparser.ConfigParser):
super().__init__(*args, **kwargs)
bot_section = self.setdefault('Bot', {})
bot_section.setdefault('loglevel', 'warning')
bot_section.setdefault('max_retry_wait', '300')
def read_paths(self, paths_seq, encoding=None):
for path in paths_seq:
@ -39,6 +40,9 @@ class Config(configparser.ConfigParser):
if self.get_loglevel(-1) == -1:
logger.error("invalid loglevel setting %r", self['Bot']['loglevel'])
retval = False
if self.get_max_retry_wait(-1) == -1:
logger.error("invalid max_retry_wait setting %r", self['Bot']['max_retry_wait'])
retval = False
return retval
def bot_jid(self):
@ -87,3 +91,15 @@ class Config(configparser.ConfigParser):
return fallback
else:
return retval
def get_max_retry_wait(self, fallback=None):
try:
retval = float(self['Bot'].get('max_retry_wait', 300))
if retval < 5:
raise ValueError("max_retry_wait {} too low".format(retval))
except ValueError:
if fallback is None:
raise
else:
retval = fallback
return retval