feat: add kill_job tool
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
# 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 kill_application
|
||||
|
||||
store = JobStore()
|
||||
|
||||
|
||||
def kill_job(job_id: str) -> dict[str, str]:
|
||||
job = store.get(job_id)
|
||||
if job is None:
|
||||
raise KeyError(f"Unknown job_id: {job_id}")
|
||||
kill_application(job.application_id)
|
||||
logger.info(f"kill_job job_id={job_id} application_id={job.application_id}")
|
||||
return {
|
||||
"job_id": job_id,
|
||||
"application_id": job.application_id,
|
||||
"status": "KILLED",
|
||||
}
|
||||
@@ -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 kill
|
||||
|
||||
|
||||
def test_kill_job_calls_yarn_kill():
|
||||
kill.store = JobStore()
|
||||
kill.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.kill.kill_application") as m:
|
||||
result = kill.kill_job("abc")
|
||||
m.assert_called_once_with("application_1")
|
||||
assert result == {
|
||||
"job_id": "abc",
|
||||
"application_id": "application_1",
|
||||
"status": "KILLED",
|
||||
}
|
||||
|
||||
|
||||
def test_kill_job_raises_for_unknown_job():
|
||||
kill.store = JobStore()
|
||||
import pytest
|
||||
with pytest.raises(KeyError):
|
||||
kill.kill_job("missing")
|
||||
Reference in New Issue
Block a user