167 lines
4.2 KiB
Python
167 lines
4.2 KiB
Python
"""持久批次执行任务租约、取消和事件测试。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import UTC, datetime, timedelta
|
|
|
|
from web.backend.models import ExecutionJob, User
|
|
from web.backend.services import execution_worker
|
|
from web.backend.services.execution_jobs import (
|
|
JOB_LOGIN_BATCH,
|
|
LANE_AUTH,
|
|
STATUS_CANCELLED,
|
|
STATUS_RUNNING,
|
|
claim_execution_jobs,
|
|
enqueue_execution_job,
|
|
finish_execution_job,
|
|
heartbeat_execution_job,
|
|
request_execution_cancel,
|
|
)
|
|
|
|
|
|
def _user(db, name: str = "execution-user") -> User:
|
|
user = User(
|
|
username=name,
|
|
password_hash="h",
|
|
role="super_admin",
|
|
is_active=True,
|
|
)
|
|
db.add(user)
|
|
db.commit()
|
|
return user
|
|
|
|
|
|
def test_job_claim_heartbeat_cancel_and_finish(db):
|
|
user = _user(db)
|
|
job = enqueue_execution_job(
|
|
db,
|
|
job_type=JOB_LOGIN_BATCH,
|
|
lane_scope=LANE_AUTH,
|
|
batch_id="exec-claim",
|
|
payload={"account_ids": [1]},
|
|
created_by=int(user.id),
|
|
)
|
|
db.commit()
|
|
|
|
claimed = claim_execution_jobs(
|
|
lane_scopes=(LANE_AUTH,),
|
|
owner="worker-a",
|
|
limit=1,
|
|
)
|
|
assert [row.id for row in claimed] == [job.id]
|
|
assert claimed[0].status == STATUS_RUNNING
|
|
assert claimed[0].attempt_count == 1
|
|
|
|
held, cancel_requested = heartbeat_execution_job(
|
|
job_id=int(job.id),
|
|
owner="worker-a",
|
|
)
|
|
assert held is True
|
|
assert cancel_requested is False
|
|
|
|
request_execution_cancel(db, batch_id="exec-claim")
|
|
db.commit()
|
|
held, cancel_requested = heartbeat_execution_job(
|
|
job_id=int(job.id),
|
|
owner="worker-a",
|
|
)
|
|
assert held is True
|
|
assert cancel_requested is True
|
|
assert finish_execution_job(
|
|
job_id=int(job.id),
|
|
owner="worker-a",
|
|
status=STATUS_CANCELLED,
|
|
)
|
|
|
|
|
|
def test_queued_job_cancel_is_immediate_and_token_scoped(db):
|
|
user = _user(db, "execution-cancel")
|
|
job = enqueue_execution_job(
|
|
db,
|
|
job_type=JOB_LOGIN_BATCH,
|
|
lane_scope=LANE_AUTH,
|
|
batch_id="exec-cancel",
|
|
payload={},
|
|
created_by=int(user.id),
|
|
)
|
|
db.commit()
|
|
|
|
request_execution_cancel(db, batch_id=job.batch_id)
|
|
db.commit()
|
|
db.refresh(job)
|
|
|
|
assert job.status == STATUS_CANCELLED
|
|
assert job.cancel_requested_at is not None
|
|
|
|
|
|
def test_expired_running_job_is_recovered(db):
|
|
user = _user(db, "execution-recover")
|
|
job = enqueue_execution_job(
|
|
db,
|
|
job_type=JOB_LOGIN_BATCH,
|
|
lane_scope=LANE_AUTH,
|
|
batch_id="exec-recover",
|
|
payload={},
|
|
created_by=int(user.id),
|
|
)
|
|
job.status = STATUS_RUNNING
|
|
job.lease_owner = "dead-worker"
|
|
job.lease_expires_at = datetime.now(UTC) - timedelta(minutes=1)
|
|
job.attempt_count = 1
|
|
job.max_attempts = 2
|
|
db.commit()
|
|
|
|
claimed = claim_execution_jobs(
|
|
lane_scopes=(LANE_AUTH,),
|
|
owner="worker-b",
|
|
limit=1,
|
|
)
|
|
|
|
assert len(claimed) == 1
|
|
assert claimed[0].lease_owner == "worker-b"
|
|
assert claimed[0].attempt_count == 2
|
|
|
|
|
|
def test_execution_worker_runs_claimed_login_batch(db, monkeypatch):
|
|
user = _user(db, "execution-run")
|
|
job = enqueue_execution_job(
|
|
db,
|
|
job_type=JOB_LOGIN_BATCH,
|
|
lane_scope=LANE_AUTH,
|
|
batch_id="exec-run",
|
|
payload={
|
|
"account_ids": [1],
|
|
"mode": "login",
|
|
"concurrency": 1,
|
|
},
|
|
created_by=int(user.id),
|
|
)
|
|
db.commit()
|
|
worker = execution_worker.ExecutionJobWorker()
|
|
claimed = claim_execution_jobs(
|
|
lane_scopes=(LANE_AUTH,),
|
|
owner=worker._owner,
|
|
limit=1,
|
|
)
|
|
assert [row.id for row in claimed] == [job.id]
|
|
seen: dict = {}
|
|
|
|
class FakeLoginRunner:
|
|
def __init__(self, **kwargs):
|
|
seen.update(kwargs)
|
|
|
|
def run(self):
|
|
seen["ran"] = True
|
|
|
|
monkeypatch.setattr(execution_worker, "LoginBatchRunner", FakeLoginRunner)
|
|
worker._run_job(int(job.id))
|
|
assert seen.get("ran") is True
|
|
|
|
db.rollback()
|
|
db.expire_all()
|
|
refreshed = db.get(ExecutionJob, job.id)
|
|
assert refreshed is not None
|
|
assert refreshed.status == "succeeded"
|
|
assert seen["ran"] is True
|
|
assert seen["batch_id"] == "exec-run"
|