Skip to content

job_control

job_control

Job lifecycle control service — conductor-only proxy.

Every operation routes through the conductor via DaemonClient IPC. If the conductor is not running, all operations fail with clear errors. There is no subprocess fallback.

Classes

JobStartResult dataclass

JobStartResult(job_id, job_name, status, workspace, total_sheets, pid=None, via_daemon=True)

Result of starting a job.

JobActionResult dataclass

JobActionResult(success, job_id, status, message, via_daemon=True)

Result of a job action (pause/resume/cancel).

ProcessHealth dataclass

ProcessHealth(pid, is_alive, is_zombie_state, process_exists, cpu_percent=None, memory_mb=None, uptime_seconds=None)

Process health check result (derived from conductor state).

JobControlService

JobControlService(daemon_client)

Conductor-only proxy for job lifecycle control.

Parameters

daemon_client: A DaemonClient connected to the conductor's Unix socket.

Source code in src/marianne/dashboard/services/job_control.py
def __init__(self, daemon_client: DaemonClient) -> None:
    if daemon_client is None:
        raise ValueError(
            "DaemonClient is required — the dashboard requires a running conductor."
        )
    self._client = daemon_client
Methods:
start_job async
start_job(config_path=None, config_content=None, workspace=None, start_sheet=1, self_healing=False, fresh=False, self_healing_auto_confirm=False, escalation=False, dry_run=False, chain_depth=None, client_cwd=None, runtime_variables=None)

Submit a new job to the conductor.

The conductor contract is path-based and asynchronous: accepted jobs keep the submitted path in the registry and may read it after this request returns. Inline config_content is therefore written to a durable dashboard submissions directory, not a throwaway temp file.

Parameters:

Name Type Description Default
config_path Path | None

Path to YAML config file.

None
config_content str | None

Inline YAML config content (written to durable file).

None
workspace Path | None

Override workspace directory.

None
start_sheet int

Starting sheet number.

1
self_healing bool

Enable self-healing mode.

False
fresh bool

Start with clean state, ignoring existing checkpoints.

False
self_healing_auto_confirm bool

Auto-confirm self-healing fixes.

False
escalation bool

Pause for composer decision on retry exhaustion.

False
dry_run bool

Forward dry-run intent to daemon clients that honor it.

False
chain_depth int | None

Concert chain depth for chained submissions.

None
client_cwd Path | None

Client working directory for relative path resolution.

None
runtime_variables dict[str, str] | None

Per-invocation template variables.

None

Returns:

Type Description
JobStartResult

JobStartResult with job details.

Raises:

Type Description
ValueError

If neither config_path nor config_content provided.

FileNotFoundError

If config_path doesn't exist.

RuntimeError

If the conductor rejects the submission.

Source code in src/marianne/dashboard/services/job_control.py
async def start_job(
    self,
    config_path: Path | None = None,
    config_content: str | None = None,
    workspace: Path | None = None,
    start_sheet: int = 1,
    self_healing: bool = False,
    fresh: bool = False,
    self_healing_auto_confirm: bool = False,
    escalation: bool = False,
    dry_run: bool = False,
    chain_depth: int | None = None,
    client_cwd: Path | None = None,
    runtime_variables: dict[str, str] | None = None,
) -> JobStartResult:
    """Submit a new job to the conductor.

    The conductor contract is path-based and asynchronous: accepted
    jobs keep the submitted path in the registry and may read it after
    this request returns. Inline ``config_content`` is therefore written
    to a durable dashboard submissions directory, not a throwaway temp file.

    Args:
        config_path: Path to YAML config file.
        config_content: Inline YAML config content (written to durable file).
        workspace: Override workspace directory.
        start_sheet: Starting sheet number.
        self_healing: Enable self-healing mode.
        fresh: Start with clean state, ignoring existing checkpoints.
        self_healing_auto_confirm: Auto-confirm self-healing fixes.
        escalation: Pause for composer decision on retry exhaustion.
        dry_run: Forward dry-run intent to daemon clients that honor it.
        chain_depth: Concert chain depth for chained submissions.
        client_cwd: Client working directory for relative path resolution.
        runtime_variables: Per-invocation template variables.

    Returns:
        JobStartResult with job details.

    Raises:
        ValueError: If neither config_path nor config_content provided.
        FileNotFoundError: If config_path doesn't exist.
        RuntimeError: If the conductor rejects the submission.
    """
    if not config_path and not config_content:
        raise ValueError("Must provide either config_path or config_content")

    resolved_path = config_path
    config: JobConfig

    if config_content:
        try:
            config = JobConfig.from_yaml_string(config_content)
        except _CONFIG_PARSE_FAILURES as exc:
            raise ValueError(f"Invalid job configuration: {exc}") from exc
        resolved_path = _write_dashboard_submission(config_content, config.name)

    assert resolved_path is not None

    if config_path and not config_content:
        resolved = config_path.resolve()
        if resolved.suffix not in (".yaml", ".yml"):
            raise ValueError(f"Config path must be a YAML file (.yaml/.yml): {config_path}")
        if ".." in config_path.parts:
            raise ValueError(f"Config path must not contain '..' traversal: {config_path}")
        if not resolved.exists():
            raise FileNotFoundError(f"Config file not found: {config_path}")
        resolved_path = resolved
        try:
            config = JobConfig.from_yaml(resolved_path)
        except _CONFIG_PARSE_FAILURES as exc:
            raise ValueError(f"Invalid job configuration: {exc}") from exc

    client_cwd_path = client_cwd.resolve() if client_cwd else Path.cwd().resolve()
    runtime_vars = runtime_variables or {}

    try:
        request = JobRequest(
            config_path=resolved_path.resolve(),
            workspace=workspace.resolve() if workspace else None,
            fresh=fresh,
            self_healing=self_healing,
            self_healing_auto_confirm=self_healing_auto_confirm,
            escalation=escalation,
            start_sheet=start_sheet if start_sheet > 1 else None,
            dry_run=dry_run,
            chain_depth=chain_depth,
            client_cwd=client_cwd_path,
            runtime_variables=runtime_vars,
        )
        response = await asyncio.wait_for(
            self._client.submit_job(request),
            timeout=DASHBOARD_DAEMON_REQUEST_TIMEOUT_SECONDS,
        )

        if response.status not in ("accepted", "pending"):
            detail = response.message or f"status={response.status}"
            raise RuntimeError(f"Conductor rejected job: {detail}")

        ws = workspace or (Path(config.workspace) if config.workspace else client_cwd_path)
        if client_cwd_path and not ws.is_absolute():
            ws = (client_cwd_path / ws).resolve()

        logger.info(
            "job_submitted_to_conductor",
            job_id=response.job_id,
            job_name=config.name,
            status=response.status,
        )

        return JobStartResult(
            job_id=response.job_id,
            job_name=config.name,
            status=response.status,
            workspace=ws,
            total_sheets=config.sheet.total_sheets,
        )

    except DaemonNotRunningError:
        raise RuntimeError("Conductor not running. Start it with: mzt start") from None
    except TimeoutError:
        raise RuntimeError("Conductor request timed out.") from None
    except (ValueError, FileNotFoundError, RuntimeError):
        raise
    except (DaemonError, OSError) as e:  # conductor refused or socket failed (#251)
        logger.warning("submit_job_failed", error_type=type(e).__name__, exc_info=True)
        raise RuntimeError(f"Failed to submit job to conductor: {e}") from e
pause_job async
pause_job(job_id)

Pause a running job via the conductor.

Source code in src/marianne/dashboard/services/job_control.py
async def pause_job(self, job_id: str) -> JobActionResult:
    """Pause a running job via the conductor."""
    try:
        response = await asyncio.wait_for(
            self._client.pause_job(job_id, ""),
            timeout=DASHBOARD_DAEMON_REQUEST_TIMEOUT_SECONDS,
        )
        paused = bool(response.get("paused")) if isinstance(response, dict) else False
        if not paused:
            message = _action_error_message(response, "Pause request was rejected")
            return JobActionResult(
                success=False,
                job_id=job_id,
                status="pause_rejected",
                message=message,
            )
        return JobActionResult(
            success=True,
            job_id=job_id,
            status="pause_requested",
            message=f"Pause request sent to conductor for job {job_id}",
        )
    except DaemonNotRunningError:
        raise RuntimeError("Conductor not running.") from None
    except TimeoutError:
        raise RuntimeError("Conductor pause request timed out.") from None
resume_job async
resume_job(job_id)

Resume a paused job via the conductor.

Source code in src/marianne/dashboard/services/job_control.py
async def resume_job(self, job_id: str) -> JobActionResult:
    """Resume a paused job via the conductor."""
    try:
        response = await asyncio.wait_for(
            self._client.resume_job(job_id, ""),
            timeout=DASHBOARD_DAEMON_REQUEST_TIMEOUT_SECONDS,
        )
        status = str(response.get("status", "")) if isinstance(response, dict) else ""
        if status not in ("accepted", "pending"):
            message = _action_error_message(response, "Resume request was rejected")
            return JobActionResult(
                success=False,
                job_id=job_id,
                status="resume_rejected",
                message=message,
            )
        return JobActionResult(
            success=True,
            job_id=job_id,
            status="resume_requested",
            message=f"Resume request sent to conductor for job {job_id}",
        )
    except DaemonNotRunningError:
        raise RuntimeError("Conductor not running.") from None
    except TimeoutError:
        raise RuntimeError("Conductor resume request timed out.") from None
cancel_job async
cancel_job(job_id)

Cancel a running or paused job via the conductor.

Source code in src/marianne/dashboard/services/job_control.py
async def cancel_job(self, job_id: str) -> JobActionResult:
    """Cancel a running or paused job via the conductor."""
    try:
        response = await asyncio.wait_for(
            self._client.cancel_job(job_id, ""),
            timeout=DASHBOARD_DAEMON_REQUEST_TIMEOUT_SECONDS,
        )
        cancelled = (
            bool(response.get("cancelled")) if isinstance(response, dict) else False
        )
        if not cancelled:
            message = _action_error_message(response, "Cancel request was rejected")
            return JobActionResult(
                success=False,
                job_id=job_id,
                status="cancel_rejected",
                message=message,
            )
        return JobActionResult(
            success=True,
            job_id=job_id,
            status="cancel_requested",
            message=f"Cancel request sent to conductor for job {job_id}",
        )
    except DaemonNotRunningError:
        raise RuntimeError("Conductor not running.") from None
    except TimeoutError:
        raise RuntimeError("Conductor cancel request timed out.") from None
delete_job async
delete_job(job_id)

Delete a terminal job from the conductor registry.

Source code in src/marianne/dashboard/services/job_control.py
async def delete_job(self, job_id: str) -> bool:
    """Delete a terminal job from the conductor registry."""
    try:
        result = await asyncio.wait_for(
            self._client.clear_jobs(job_ids=[job_id]),
            timeout=DASHBOARD_DAEMON_REQUEST_TIMEOUT_SECONDS,
        )
        deleted: bool = bool(result.get("deleted", 0))
        if deleted:
            logger.info("job_deleted", job_id=job_id)
        return deleted
    except DaemonNotRunningError:
        raise RuntimeError("Conductor not running.") from None
    except TimeoutError:
        raise RuntimeError("Conductor delete request timed out.") from None
verify_process_health async
verify_process_health(job_id)

Check job health by querying conductor state.

Returns a ProcessHealth derived from the job's CheckpointState as reported by the conductor.

Source code in src/marianne/dashboard/services/job_control.py
async def verify_process_health(self, job_id: str) -> ProcessHealth:
    """Check job health by querying conductor state.

    Returns a ``ProcessHealth`` derived from the job's ``CheckpointState``
    as reported by the conductor.
    """
    try:
        status_data = await asyncio.wait_for(
            self._client.get_job_status(job_id, ""),
            timeout=DASHBOARD_DAEMON_REQUEST_TIMEOUT_SECONDS,
        )
        from marianne.core.checkpoint import CheckpointState, JobStatus

        state = CheckpointState(**status_data)

        is_terminal = state.status in (
            JobStatus.COMPLETED,
            JobStatus.FAILED,
            JobStatus.CANCELLED,
        )

        return ProcessHealth(
            pid=state.pid,
            is_alive=not is_terminal,
            is_zombie_state=state.is_zombie() if not is_terminal else False,
            process_exists=not is_terminal,
            uptime_seconds=None,
            cpu_percent=None,
            memory_mb=None,
        )
    except DaemonNotRunningError:
        return ProcessHealth(
            pid=None,
            is_alive=False,
            is_zombie_state=False,
            process_exists=False,
        )
    except TimeoutError:
        return ProcessHealth(
            pid=None,
            is_alive=False,
            is_zombie_state=False,
            process_exists=False,
        )

Functions: