Skip to content

Commit

Permalink
Add caching & force-update command; release v0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dgw committed Jun 19, 2021
1 parent 6e14955 commit 4a72989
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 11 deletions.
9 changes: 9 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
0.2.0
=====

Added:
* Caching behavior: The configured Sphinx inventory is fetched at startup, and
re-fetched automatically every 24 hours
* Bot admins can force an update with the new `.rtfmupdate` command


0.1.1
=====

Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = sopel-rtfm
version = 0.1.1
version = 0.2.0
description = A plugin to suggest documentation links when someone asks a basic question.
author = dgw
author_email = [email protected]
Expand Down
51 changes: 41 additions & 10 deletions sopel_rtfm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""
from __future__ import unicode_literals, absolute_import, division, print_function

from datetime import datetime
import os
from urllib.error import URLError
from urllib.parse import urlparse
Expand Down Expand Up @@ -47,22 +48,44 @@ def configure(config):
def setup(bot):
bot.config.define_section('rtfm', RTFMSection)

bot.memory['rtfm_base'] = (
bot.config.rtfm.link_base or
os.path.dirname(bot.config.rtfm.inventory)
)

if not bot.memory['rtfm_base'].endswith('/'):
bot.memory['rtfm_base'] += '/'

update_sphinx_objects(bot)


@module.interval(3600)
def update_sphinx_objects(bot, force=False):
now = datetime.utcnow()
age = now - bot.memory.get('rtfm_cache_time', datetime.fromtimestamp(0))

if not force and age.total_seconds() < 86400: # 1 day / 24 hours in seconds
LOGGER.debug("Inventory cache is under one day old, skipping update.")
return

LOGGER.debug("Beginning inventory cache update.")

try:
inv = sphobjinv.Inventory(url=bot.config.rtfm.inventory)
except ValueError:
# invalid URL, probably
LOGGER.error(
LOGGER.exception(
'Could not fetch inventory file; URL seems to be malformed: %s',
bot.config.rtfm.inventory,
)
raise
return
except (SphobjinvError, URLError) as e:
# couldn't fetch due to urllib error or unrecognized file format
LOGGER.error(
LOGGER.exception(
'Could not fetch inventory file: %s',
str(e),
)
raise
return

objects = {
name: url
Expand All @@ -79,15 +102,11 @@ def setup(bot):
]
}

bot.memory['rtfm_base'] = (
bot.config.rtfm.link_base or
os.path.dirname(bot.config.rtfm.inventory)
)
bot.memory['rtfm_inventory'] = inv
bot.memory['rtfm_objects'] = objects
bot.memory['rtfm_cache_time'] = now

if not bot.memory['rtfm_base'].endswith('/'):
bot.memory['rtfm_base'] += '/'
LOGGER.debug("Finished updating inventory cache.")


def shutdown(bot):
Expand Down Expand Up @@ -116,3 +135,15 @@ def suggest_doc_link(bot, trigger):
link = bot.memory['rtfm_base'] + bot.memory['rtfm_objects'][result]

bot.say(link)


@module.commands('rtfmupdate')
@module.require_admin
def force_update(bot, trigger):
"""Force an update of the target Sphinx object inventory.
In lieu of restarting the bot or waiting for the next automatic refresh
(every 24 hours), a bot admin can force an immediate update.
"""
bot.reply("Attempting to update Sphinx objects.")
update_sphinx_objects(bot, force=True)

0 comments on commit 4a72989

Please sign in to comment.