feat: add get_job_status tool
This commit is contained in:
@@ -0,0 +1,20 @@
|
|||||||
|
# coding=utf-8
|
||||||
|
"""
|
||||||
|
@Time :2026/6/24
|
||||||
|
@Author :tao.chen
|
||||||
|
"""
|
||||||
|
from common.logging import logger
|
||||||
|
from spark_executor.core.job_store import JobStore
|
||||||
|
from spark_executor.core.yarn_client import get_application_status
|
||||||
|
from spark_executor.models import JobStatus
|
||||||
|
|
||||||
|
store = JobStore()
|
||||||
|
|
||||||
|
|
||||||
|
def get_job_status(job_id: str) -> JobStatus:
|
||||||
|
job = store.get(job_id)
|
||||||
|
if job is None:
|
||||||
|
raise KeyError(f"Unknown job_id: {job_id}")
|
||||||
|
state, raw = get_application_status(job.application_id)
|
||||||
|
logger.info(f"get_job_status job_id={job_id} state={state}")
|
||||||
|
return JobStatus(application_id=job.application_id, state=state, raw=raw)
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
# coding=utf-8
|
||||||
|
from datetime import datetime
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
from spark_executor.core.job_store import JobStore
|
||||||
|
from spark_executor.models import Job
|
||||||
|
from spark_executor.tools import status
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_job_status_returns_state():
|
||||||
|
status.store = JobStore()
|
||||||
|
status.store.put(
|
||||||
|
Job(
|
||||||
|
job_id="abc",
|
||||||
|
application_id="application_1",
|
||||||
|
script_path="/tmp/j.py",
|
||||||
|
queue="default",
|
||||||
|
submit_time=datetime(2026, 6, 24),
|
||||||
|
connection="prod",
|
||||||
|
)
|
||||||
|
)
|
||||||
|
with patch(
|
||||||
|
"spark_executor.tools.status.get_application_status",
|
||||||
|
return_value=("RUNNING", "State : RUNNING\n"),
|
||||||
|
):
|
||||||
|
out = status.get_job_status("abc")
|
||||||
|
assert out.application_id == "application_1"
|
||||||
|
assert out.state == "RUNNING"
|
||||||
|
assert "RUNNING" in out.raw
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_job_status_raises_for_unknown_job():
|
||||||
|
status.store = JobStore()
|
||||||
|
import pytest
|
||||||
|
with pytest.raises(KeyError):
|
||||||
|
status.get_job_status("missing")
|
||||||
Reference in New Issue
Block a user