Skip to content

Commit d8b664b

Browse files
authored
Fix: Run plan in default event loop instead of an executor (#727)
* Access state sync to ensure its creation on API startup * Run plan in default event loop instead of an executor * Fix style
1 parent d5cc53a commit d8b664b

2 files changed

Lines changed: 16 additions & 20 deletions

File tree

web/server/api/endpoints/environments.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111

1212

1313
@router.get("")
14-
def get_environments(context: Context = Depends(get_loaded_context)) -> t.Dict[str, Environment]:
14+
async def get_environments(
15+
context: Context = Depends(get_loaded_context),
16+
) -> t.Dict[str, Environment]:
1517
"""Get the environments"""
1618
environments = {env.name: env for env in context.state_reader.get_environments()}
1719
if c.PROD not in environments:

web/server/api/endpoints/plan.py

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
from __future__ import annotations
22

3-
import asyncio
4-
import functools
53
import typing as t
64

75
from fastapi import APIRouter, Body, Depends, HTTPException, Request, Response, status
@@ -12,7 +10,6 @@
1210
from sqlmesh.utils.errors import PlanError
1311
from web.server import models
1412
from web.server.settings import get_loaded_context
15-
from web.server.utils import run_in_executor
1613

1714
router = APIRouter()
1815

@@ -39,23 +36,20 @@ async def run_plan(
3936

4037
context.refresh()
4138

42-
plan_func = functools.partial(
43-
context.plan,
44-
environment=environment,
45-
no_prompts=True,
46-
start=plan_dates.start if plan_dates else None,
47-
end=plan_dates.end if plan_dates else None,
48-
create_from=plan_options.create_from,
49-
skip_tests=plan_options.skip_tests,
50-
restate_models=plan_options.restate_models,
51-
no_gaps=plan_options.no_gaps,
52-
skip_backfill=plan_options.skip_backfill,
53-
forward_only=plan_options.forward_only,
54-
no_auto_categorization=plan_options.no_auto_categorization,
55-
)
56-
request.app.state.task = asyncio.create_task(run_in_executor(plan_func))
5739
try:
58-
plan = await request.app.state.task
40+
plan = context.plan(
41+
environment=environment,
42+
no_prompts=True,
43+
start=plan_dates.start if plan_dates else None,
44+
end=plan_dates.end if plan_dates else None,
45+
create_from=plan_options.create_from,
46+
skip_tests=plan_options.skip_tests,
47+
restate_models=plan_options.restate_models,
48+
no_gaps=plan_options.no_gaps,
49+
skip_backfill=plan_options.skip_backfill,
50+
forward_only=plan_options.forward_only,
51+
no_auto_categorization=plan_options.no_auto_categorization,
52+
)
5953
except PlanError as e:
6054
raise HTTPException(
6155
status_code=HTTP_422_UNPROCESSABLE_ENTITY,

0 commit comments

Comments
 (0)