service.py
python
sha256:2313b6a6353294a0ca08ff579624198da250b99163a86215aed31a905912e360
Land Scooling Lab T0/T2 training contract and seven-tier tests
Human
minor
⚠ breaking
43 days ago
| 1 | """Application service for Scooling Lab training API operations.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | from scooling_lab.contracts import TrainingJobRequest |
| 6 | from scooling_lab.fake_worker import FakeTrainingWorker |
| 7 | from scooling_lab.store import TrainingJobStore |
| 8 | |
| 9 | |
| 10 | class TrainingApiService: |
| 11 | """Implements the T2 API contract over a store and fake worker.""" |
| 12 | |
| 13 | def __init__(self, store: TrainingJobStore, auto_run_worker: bool = True) -> None: |
| 14 | """Create a service with optional synchronous fake-worker completion.""" |
| 15 | |
| 16 | self._store = store |
| 17 | self._worker = FakeTrainingWorker(store) |
| 18 | self._auto_run_worker = auto_run_worker |
| 19 | |
| 20 | def create_training_job(self, payload: dict[str, object]) -> dict[str, object]: |
| 21 | """Validate, create, and optionally complete a fixture training job.""" |
| 22 | |
| 23 | request = TrainingJobRequest.from_mapping(payload) |
| 24 | job = self._store.create(request) |
| 25 | if self._auto_run_worker: |
| 26 | job = self._worker.run_job(job.id) |
| 27 | return job.to_public_dict() |
| 28 | |
| 29 | def get_training_job(self, job_id: str) -> dict[str, object]: |
| 30 | """Return the public status for one training job.""" |
| 31 | |
| 32 | return self._store.get(job_id).to_public_dict() |
| 33 | |
| 34 | def cancel_training_job(self, job_id: str) -> dict[str, object]: |
| 35 | """Cancel a queued or running training job.""" |
| 36 | |
| 37 | from scooling_lab.contracts import TrainingJobStatus |
| 38 | |
| 39 | return self._store.update_status( |
| 40 | job_id, TrainingJobStatus.CANCELLED |
| 41 | ).to_public_dict() |
| 42 | |
| 43 | def list_artifacts(self, job_id: str) -> dict[str, object]: |
| 44 | """Return placeholder artifacts registered for one job.""" |
| 45 | |
| 46 | artifacts = [artifact.to_dict() for artifact in self._store.list_artifacts(job_id)] |
| 47 | return {"jobId": job_id, "artifacts": artifacts} |
File History
1 commit
sha256:2313b6a6353294a0ca08ff579624198da250b99163a86215aed31a905912e360
Land Scooling Lab T0/T2 training contract and seven-tier tests
Human
minor
⚠
43 days ago