-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add eager scheduler + pickers for >1 app|dest
- Loading branch information
1 parent
76fc0c3
commit ba6e715
Showing
13 changed files
with
368 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from os import getloadavg, sched_getaffinity | ||
|
||
from fastapi import HTTPException | ||
from starlette import status | ||
|
||
|
||
def check_load(max_load: float = 1.0) -> None: | ||
"""Check if machine load is too high. | ||
Args: | ||
max_load: Maximum load allowed. | ||
Raises: | ||
HTTPException: When machine load is too high. | ||
""" | ||
nr_cpus = len(sched_getaffinity(0)) | ||
load_avg_last_minute = getloadavg()[0] / nr_cpus | ||
if load_avg_last_minute > max_load: | ||
raise HTTPException( | ||
status_code=status.HTTP_503_SERVICE_UNAVAILABLE, | ||
detail="Machine load is too high, please try again later.", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
from asyncio import create_subprocess_shell, wait_for | ||
from asyncio.subprocess import Process | ||
from pathlib import Path | ||
from typing import Literal | ||
from uuid import uuid1 | ||
|
||
from pydantic import BaseModel, PositiveInt | ||
from pydantic.types import PositiveFloat | ||
|
||
from bartender.check_load import check_load | ||
from bartender.db.models.job_model import State | ||
from bartender.schedulers.abstract import ( | ||
AbstractScheduler, | ||
JobDescription, | ||
JobSubmissionError, | ||
) | ||
|
||
|
||
class EagerSchedulerConfig(BaseModel): | ||
"""Configuration for eager scheduler. | ||
Args: | ||
max_load: Maximum load that scheduler will process submissions. | ||
timeout: Maximum time to wait for job to finish. In seconds. | ||
""" | ||
|
||
type: Literal["eager"] = "eager" | ||
max_load: PositiveFloat = 1.0 | ||
timeout: PositiveInt = 300 | ||
|
||
|
||
async def _exec(description: JobDescription, timeout: int) -> None: | ||
stderr_fn = description.job_dir / "stderr.txt" | ||
stdout_fn = description.job_dir / "stdout.txt" | ||
|
||
with open(stderr_fn, "w") as stderr: | ||
with open(stdout_fn, "w") as stdout: | ||
proc = await create_subprocess_shell( | ||
description.command, | ||
stdout=stdout, | ||
stderr=stderr, | ||
cwd=description.job_dir, | ||
) | ||
try: | ||
await _handle_job_completion(timeout, proc, description.job_dir) | ||
except TimeoutError: | ||
raise JobSubmissionError(f"Job took longer than {timeout} seconds") | ||
|
||
|
||
async def _handle_job_completion(timeout: int, proc: Process, job_dir: Path) -> None: | ||
returncode = await wait_for(proc.wait(), timeout=timeout) | ||
(job_dir / "returncode").write_text(str(returncode)) | ||
if returncode != 0: | ||
raise JobSubmissionError( | ||
f"Job failed with return code {returncode}", | ||
) | ||
|
||
|
||
class EagerScheduler(AbstractScheduler): | ||
"""Scheduler that runs jobs immediately on submission.""" | ||
|
||
def __init__(self, config: EagerSchedulerConfig) -> None: | ||
self.config = config | ||
|
||
async def submit(self, description: JobDescription) -> str: # noqa: D102 | ||
check_load(self.config.max_load) | ||
await _exec(description, self.config.timeout) | ||
return str(uuid1()) | ||
|
||
async def state(self, job_id: str) -> State: # noqa: D102 | ||
return "ok" | ||
|
||
async def states(self, job_ids: list[str]) -> list[State]: # noqa: D102 | ||
return ["ok" for _ in job_ids] | ||
|
||
async def cancel(self, job_id: str) -> None: # noqa: D102 | ||
pass # noqa: WPS420 -- cannot cancel job that is already completed. | ||
|
||
async def close(self) -> None: # noqa: D102 | ||
pass # noqa: WPS420 -- nothing to close. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
from bartender.schedulers.abstract import JobDescription, JobSubmissionError | ||
from bartender.schedulers.eager import EagerScheduler, EagerSchedulerConfig | ||
|
||
|
||
@pytest.mark.anyio | ||
async def test_ok_running_job(tmp_path: Path) -> None: | ||
async with EagerScheduler(EagerSchedulerConfig()) as scheduler: | ||
description = JobDescription(command="echo -n hello", job_dir=tmp_path) | ||
|
||
jid = await scheduler.submit(description) | ||
|
||
assert (await scheduler.state(jid)) == "ok" | ||
assert (tmp_path / "returncode").read_text() == "0" | ||
assert (tmp_path / "stdout.txt").read_text() == "hello" | ||
|
||
|
||
@pytest.mark.anyio | ||
async def test_bad_running_job(tmp_path: Path) -> None: | ||
async with EagerScheduler(EagerSchedulerConfig()) as scheduler: | ||
description = JobDescription(command="exit 42", job_dir=tmp_path) | ||
|
||
with pytest.raises(JobSubmissionError, match="Job failed with return code 42"): | ||
await scheduler.submit(description) |
Oops, something went wrong.