mirror of
https://github.com/simonw/datasette.git
synced 2026-09-09 01:54:15 +02:00
Add datasette.add_background_task() with supervised launch after startup
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
parent
bdc9731740
commit
867dd4aba0
6 changed files with 764 additions and 2 deletions
|
|
@ -52,6 +52,56 @@ def test_serve_with_get(tmp_path_factory):
|
|||
pm.unregister(to_unregister)
|
||||
|
||||
|
||||
def test_serve_with_get_does_not_launch_background_tasks(tmp_path_factory):
|
||||
# Per decision #3 in plans/first-request/04-core-plan.md, --get must
|
||||
# never launch background tasks, even though its TestClient request
|
||||
# flows through the full ASGI stack (including the AsgiRunOnFirstRequest
|
||||
# fallback that would otherwise launch them). The plugin's startup hook
|
||||
# itself still runs (registration happens) - only the launch is
|
||||
# suppressed, so the sentinel file the background task would write must
|
||||
# never appear.
|
||||
plugins_dir = tmp_path_factory.mktemp("plugins_for_get_background_tasks")
|
||||
sentinel = plugins_dir / "sentinel.txt"
|
||||
(plugins_dir / "bg_task_for_get.py").write_text(
|
||||
textwrap.dedent(
|
||||
"""
|
||||
from datasette import hookimpl
|
||||
|
||||
@hookimpl
|
||||
def startup(datasette):
|
||||
async def inner():
|
||||
async def task(datasette):
|
||||
with open("{sentinel}", "w") as fp:
|
||||
fp.write("ran")
|
||||
|
||||
datasette.add_background_task(task, name="get-sentinel-task")
|
||||
|
||||
return inner
|
||||
""".format(sentinel=str(sentinel)),
|
||||
),
|
||||
"utf-8",
|
||||
)
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(
|
||||
cli,
|
||||
[
|
||||
"serve",
|
||||
"--memory",
|
||||
"--plugins-dir",
|
||||
str(plugins_dir),
|
||||
"--get",
|
||||
"/_memory/-/query.json?sql=select+1",
|
||||
],
|
||||
)
|
||||
assert result.exit_code == 0, result.output
|
||||
assert not sentinel.exists()
|
||||
|
||||
to_unregister = [
|
||||
p for p in pm.get_plugins() if p.__name__ == "bg_task_for_get.py"
|
||||
][0]
|
||||
pm.unregister(to_unregister)
|
||||
|
||||
|
||||
def test_serve_with_get_headers():
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue