Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

startup() plugin hook #834

Closed
simonw opened this issue Jun 11, 2020 · 6 comments
Closed

startup() plugin hook #834

simonw opened this issue Jun 11, 2020 · 6 comments

Comments

@simonw
Copy link
Owner

simonw commented Jun 11, 2020

It might be useful to have an startup hook which gets passed the datasette object as soon as Datasette has finished initializing.

My initial use-case for this is configuration verification - checking that the "plugins" configuration block for this plugin contains valid details.

I imagine there are plenty of other potential uses for this as well.

@simonw simonw changed the title Plugin idea: on_startup Plugin hook idea: "startup" Jun 12, 2020
@simonw
Copy link
Owner Author

simonw commented Jun 12, 2020

Another use-case for this: I want to use the --root option on Glitch but it gives me a 127.0.0.1 URL. Glitch has a PROJECT_DOMAIN environment variable which tells me the URL. A datasette-glitch plugin could use a startup hook to output the correct login URL.

@simonw
Copy link
Owner Author

simonw commented Jun 12, 2020

This should be able to optionally return an async function which is then awaited.

@simonw
Copy link
Owner Author

simonw commented Jun 12, 2020

Another use-case: plugins that need their own database with the correct tables. They can write to the database on startup to create their tables.

@simonw
Copy link
Owner Author

simonw commented Jun 13, 2020

Implementing this is proving surprisingly tricky, because of the need to be able to optionally await the returned value. It's a bit of a fiddle to get this to work within unit tests because they run in non-async functions - due to this cunning async_to_sync usage in the test client:

datasette/tests/fixtures.py

Lines 115 to 133 in b906030

@async_to_sync
async def get(
self, path, allow_redirects=True, redirect_count=0, method="GET", cookies=None
):
return await self._request(
path, allow_redirects, redirect_count, method, cookies
)
@async_to_sync
async def post(
self,
path,
post_data=None,
allow_redirects=True,
redirect_count=0,
content_type="application/x-www-form-urlencoded",
cookies=None,
csrftoken_from=None,
):

I could switch to using async def test_* functions decorated with @pytest.mark.asyncio but I'd rather not re-engineer the entire test suite just for this one feature, so I'll try to find another way.

@simonw simonw changed the title Plugin hook idea: "startup" Plugin hook: "startup" Jun 13, 2020
@simonw simonw closed this as completed in 09a3479 Jun 13, 2020
simonw added a commit that referenced this issue Jun 13, 2020
@simonw
Copy link
Owner Author

simonw commented Jun 13, 2020

simonw added a commit that referenced this issue Jun 13, 2020
simonw added a commit that referenced this issue Jun 18, 2020
@simonw simonw changed the title Plugin hook: "startup" startup() plugin hook Jun 28, 2020
@simonw
Copy link
Owner Author

simonw commented Jun 28, 2020

I have two plugins in progress that use this hook now:

simonw added a commit that referenced this issue Jul 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant