# SPDX-License-Identifier: MIT
# Copyright (c) 2001-{{ COPYRIGHT_END_YEAR }} JCMwave GmbH
"""Daemon namespaced API facades."""
from __future__ import annotations
from collections.abc import Coroutine, Sequence
from typing import TYPE_CHECKING, Literal, overload
if TYPE_CHECKING:
from ..results import (
BatchSolveResult,
BatchSolveSubmission,
SolveResult,
SolveSubmission,
)
from .handler import DaemonHandler
from .models import DaemonStatus
[docs]
class DaemonLifecycle:
"""Lifecycle namespace facade."""
def __init__(self, *, handler: DaemonHandler) -> None:
self._handler: DaemonHandler = handler
[docs]
def startup(self) -> None:
"""Start the daemon and connect the handler.
Returns
-------
None
The handler is connected to the running daemon.
"""
self._handler.start()
self._handler.connect()
[docs]
def shutdown(self) -> None:
"""Disconnect and stop the daemon.
Returns
-------
None
The daemon is stopped.
"""
self._handler.shutdown()
[docs]
def status(self) -> DaemonStatus:
"""Return the current daemon status.
Returns
-------
DaemonStatus
Information about the connection and registered resources.
"""
return self._handler.status()
[docs]
class DaemonJobs:
"""Jobs namespace facade."""
def __init__(self, *, handler: DaemonHandler) -> None:
self._handler: DaemonHandler = handler
@overload
def wait(
self,
submission: SolveSubmission,
*,
asynchronous: Literal[False] = False,
break_when: Literal["all", "any"] = "all",
timeout: float | None = None,
on_timeout: Literal["raise", "return"] = "raise",
on_error: Literal["raise", "collect"] = "raise",
progress: bool = False,
progress_label: str | None = None,
) -> SolveResult: ...
@overload
def wait(
self,
submission: BatchSolveSubmission,
*,
asynchronous: Literal[False] = False,
break_when: Literal["all", "any"] = "all",
timeout: float | None = None,
on_timeout: Literal["raise", "return"] = "raise",
on_error: Literal["raise", "collect"] = "raise",
progress: bool = False,
progress_label: str | None = None,
) -> BatchSolveResult: ...
@overload
def wait(
self,
submission: SolveSubmission,
*,
asynchronous: Literal[True],
break_when: Literal["all", "any"] = "all",
timeout: float | None = None,
on_timeout: Literal["raise", "return"] = "raise",
on_error: Literal["raise", "collect"] = "raise",
progress: bool = False,
progress_label: str | None = None,
) -> Coroutine[object, object, SolveResult]: ...
@overload
def wait(
self,
submission: BatchSolveSubmission,
*,
asynchronous: Literal[True],
break_when: Literal["all", "any"] = "all",
timeout: float | None = None,
on_timeout: Literal["raise", "return"] = "raise",
on_error: Literal["raise", "collect"] = "raise",
progress: bool = False,
progress_label: str | None = None,
) -> Coroutine[object, object, BatchSolveResult]: ...
[docs]
def wait(
self,
submission: SolveSubmission | BatchSolveSubmission,
*,
asynchronous: bool = False,
break_when: Literal["all", "any"] = "all",
timeout: float | None = None,
on_timeout: Literal["raise", "return"] = "raise",
on_error: Literal["raise", "collect"] = "raise",
progress: bool = False,
progress_label: str | None = None,
) -> (
SolveResult
| BatchSolveResult
| Coroutine[object, object, SolveResult | BatchSolveResult]
):
"""Wait for a daemon submission and return its results.
Parameters
----------
submission : SolveSubmission | BatchSolveSubmission
submission returned by `solve(..., dispatch="submit")`.
asynchronous : bool, optional
If `True`, return a value that you must use with `await`.
break_when : {"all", "any"}, optional
Condition that ends a batch wait. `"all"` waits for all requested
solves. `"any"` returns after the first requested solve finishes.
timeout : float | None, optional
Maximum number of seconds to wait. If omitted, wait without a time
limit.
on_timeout : {"raise", "return"}, optional
Action at the time limit. `"raise"` raises an error. `"return"`
returns the results that are available.
on_error : {"raise", "collect"}, optional
Action for a failed batch entry. `"raise"` raises an error.
`"collect"` stores a `BatchSolveFailure` in the batch result.
progress : bool, optional
If `True`, show progress while waiting for a batch.
progress_label : str | None, optional
Text to show with the progress display. If omitted, use
`"daemon.wait"`.
Returns
-------
SolveResult | BatchSolveResult | Coroutine[...]
Result that matches the submission. If `asynchronous=True`, return
a value that you must use with `await`.
"""
if asynchronous:
return self._handler.wait(
submission=submission,
asynchronous=True,
break_when=break_when,
timeout=timeout,
on_timeout=on_timeout,
on_error=on_error,
progress=progress,
progress_label=progress_label,
)
return self._handler.wait(
submission=submission,
asynchronous=False,
break_when=break_when,
timeout=timeout,
on_timeout=on_timeout,
on_error=on_error,
progress=progress,
progress_label=progress_label,
)
[docs]
def info(
self,
job_ids: Sequence[str | int] | None = None,
*,
status_only: bool = False,
) -> object:
"""Return information about submitted daemon solves.
Parameters
----------
job_ids : Sequence[str | int] | None, optional
Daemon submission identifiers to select. If omitted, select all known
daemon submissions.
status_only : bool, optional
If `True`, request only status information when the daemon supports
this option.
Returns
-------
object
Information supplied by the daemon. The available fields depend on
the daemon version.
"""
return self._handler._job_info_impl( # noqa: SLF001
job_ids=job_ids,
status_only=status_only,
)
[docs]
def kill(self, job_ids: Sequence[str | int] | None = None) -> tuple[int, ...]:
"""Stop submitted daemon solves.
Parameters
----------
job_ids : Sequence[str | int] | None, optional
Daemon submission identifiers to stop. If omitted, stop all known daemon
submissions.
Returns
-------
tuple[int, ...]
Identifiers of the stopped daemon jobs.
"""
return self._handler._kill_jobs_impl(job_ids=job_ids) # noqa: SLF001
class DaemonEnvironment:
"""Environment namespace facade."""
def __init__(self, *, handler: DaemonHandler) -> None:
self._handler: DaemonHandler = handler