New "startup" plugin hook, closes #834

This commit is contained in:
Simon Willison 2020-06-13 10:55:41 -07:00
commit 09a3479a54
8 changed files with 61 additions and 0 deletions

View file

@ -302,6 +302,13 @@ class Datasette:
self._permission_checks = collections.deque(maxlen=200)
self._root_token = secrets.token_hex(32)
async def invoke_startup(self):
for hook in pm.hook.startup(datasette=self):
if callable(hook):
hook = hook()
if asyncio.iscoroutine(hook):
hook = await hook
def sign(self, value, namespace="default"):
return URLSafeSerializer(self._secret, namespace).dumps(value)

View file

@ -397,6 +397,9 @@ def serve(
# Private utility mechanism for writing unit tests
return ds
# Run the "startup" plugin hooks
asyncio.get_event_loop().run_until_complete(ds.invoke_startup())
# Run async sanity checks - but only if we're not under pytest
asyncio.get_event_loop().run_until_complete(check_databases(ds))

View file

@ -5,6 +5,11 @@ hookspec = HookspecMarker("datasette")
hookimpl = HookimplMarker("datasette")
@hookspec
def startup(datasette):
"Fires directly after Datasette first starts running"
@hookspec
def asgi_wrapper(datasette):
"Returns an ASGI middleware callable to wrap our ASGI application with"