tasklite-engine 3.0.0.dev0__py3-none-any.whl → 3.0.0.dev2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- tasklite/__init__.py +42 -18
- tasklite/__main__.py +205 -0
- tasklite/_report.py +562 -0
- tasklite/backend/__init__.py +19 -2
- tasklite/backend/base.py +327 -156
- tasklite/backend/memory.py +261 -339
- tasklite/backend/sqlite_backend.py +416 -488
- tasklite/edit.py +1276 -0
- tasklite/engine/__init__.py +6 -7
- tasklite/engine/_worker_bootstrap.py +221 -0
- tasklite/engine/_worker_runtime.py +229 -0
- tasklite/engine/admission.py +20 -114
- tasklite/engine/authorization.py +989 -0
- tasklite/engine/channel.py +531 -468
- tasklite/engine/completion.py +193 -275
- tasklite/engine/config.py +5 -11
- tasklite/engine/dispatch.py +299 -384
- tasklite/engine/governor.py +134 -69
- tasklite/engine/in_flight.py +20 -88
- tasklite/engine/launcher.py +1173 -0
- tasklite/engine/recovery.py +147 -100
- tasklite/engine/resource.py +50 -103
- tasklite/engine/runtime.py +158 -525
- tasklite/engine/scheduler.py +9 -46
- tasklite/engine/session.py +60 -15
- tasklite/engine/store.py +235 -462
- tasklite/engine/types.py +29 -40
- tasklite/engine/wait.py +8 -0
- tasklite/exceptions.py +0 -1
- tasklite/models/artifact.py +84 -62
- tasklite/models/context.py +37 -30
- tasklite/models/graph.py +114 -0
- tasklite/models/job.py +3 -5
- tasklite/models/records.py +482 -0
- tasklite/models/state.py +19 -125
- tasklite/models/trigger.py +63 -3
- tasklite/pipeline.py +92 -100
- tasklite/resource.py +79 -2
- tasklite/snapshot.py +608 -405
- tasklite/testing.py +424 -11
- tasklite/utils/artifact.py +1091 -179
- tasklite/utils/encoding.py +21 -40
- tasklite/utils/ipc.py +49 -1
- tasklite/utils/lockfile.py +50 -19
- tasklite/wrappers/__init__.py +1 -1
- tasklite/wrappers/discovery.py +13 -16
- tasklite/wrappers/http.py +77 -43
- {tasklite_engine-3.0.0.dev0.dist-info → tasklite_engine-3.0.0.dev2.dist-info}/METADATA +23 -16
- tasklite_engine-3.0.0.dev2.dist-info/RECORD +60 -0
- tasklite/contrib/__init__.py +0 -7
- tasklite/engine/execution_access.py +0 -39
- tasklite/engine/files.py +0 -52
- tasklite/engine/ops.py +0 -25
- tasklite/engine/selection.py +0 -186
- tasklite/hooks.py +0 -49
- tasklite/triggers.py +0 -47
- tasklite_engine-3.0.0.dev0.dist-info/RECORD +0 -58
- {tasklite_engine-3.0.0.dev0.dist-info → tasklite_engine-3.0.0.dev2.dist-info}/WHEEL +0 -0
- {tasklite_engine-3.0.0.dev0.dist-info → tasklite_engine-3.0.0.dev2.dist-info}/licenses/LICENSE +0 -0
- {tasklite_engine-3.0.0.dev0.dist-info → tasklite_engine-3.0.0.dev2.dist-info}/top_level.txt +0 -0
tasklite/engine/recovery.py
CHANGED
|
@@ -2,146 +2,193 @@
|
|
|
2
2
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
|
-
from
|
|
5
|
+
from collections import defaultdict
|
|
6
|
+
from collections.abc import Mapping, Set as AbstractSet
|
|
7
|
+
import copy
|
|
8
|
+
from typing import Any, TYPE_CHECKING
|
|
6
9
|
|
|
7
10
|
if TYPE_CHECKING:
|
|
8
|
-
from
|
|
9
|
-
|
|
10
|
-
from
|
|
11
|
-
from .
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
"""装载状态和登记恢复失败所需的仓库接口。"""
|
|
17
|
-
|
|
18
|
-
@property
|
|
19
|
-
def backend(self) -> AbstractStateBackend: ...
|
|
20
|
-
|
|
21
|
-
@property
|
|
22
|
-
def state(self) -> PipelineState: ...
|
|
23
|
-
|
|
24
|
-
def set_state(self, state: PipelineState | None) -> None: ...
|
|
25
|
-
|
|
26
|
-
def apply_failure(
|
|
27
|
-
self, job_uid: str, failure_metadata: dict[str, Any], *,
|
|
28
|
-
job_snapshot: dict[str, Any] | None = None,
|
|
29
|
-
cascade: bool = True, attempt_no: int | None = None,
|
|
30
|
-
execution_id: str | None = None,
|
|
31
|
-
run_id: str | None = None, started_at: str | None = None,
|
|
32
|
-
) -> Any: ...
|
|
33
|
-
|
|
34
|
-
class RecoveryCompletion(Protocol):
|
|
35
|
-
"""恢复编排消费的完成机器契约(CompletionMachine 自动满足)。
|
|
36
|
-
|
|
37
|
-
异常中止只清除内存在途归属,持久化 running 留给下次恢复。
|
|
38
|
-
"""
|
|
39
|
-
|
|
40
|
-
def settle_aborted(
|
|
41
|
-
self,
|
|
42
|
-
cancelled_entries: Sequence[InFlightJob],
|
|
43
|
-
done_entries: Sequence[tuple[InFlightJob, ExecutionResult]],
|
|
44
|
-
) -> None: ...
|
|
45
|
-
|
|
11
|
+
from .channel import ExecutionChannel
|
|
12
|
+
from .completion import CompletionMachine
|
|
13
|
+
from .in_flight import InFlightTracker
|
|
14
|
+
from .store import StateStore
|
|
15
|
+
|
|
16
|
+
from ..backend.base import RunningAttemptRecovery, execution_id_matches_run
|
|
17
|
+
from ..exceptions import TaskLiteError
|
|
18
|
+
from ..models.records import build_result_record
|
|
46
19
|
from ..models.state import PipelineState, job_uid_from_snapshot
|
|
47
|
-
from ..
|
|
20
|
+
from ..utils.clock import utc_now_iso
|
|
21
|
+
|
|
22
|
+
UNSETTLED_RUNNING_ERROR_MESSAGE = "上次执行未完成结算,实际结果未知"
|
|
23
|
+
UNSETTLED_RUNNING_REASON = "unsettled_running_recovery"
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def validate_running_execution_identity(
|
|
27
|
+
row: Mapping[str, Any],
|
|
28
|
+
*,
|
|
29
|
+
attempt_sequence: int,
|
|
30
|
+
settled_attempt_nos: AbstractSet[int],
|
|
31
|
+
) -> tuple[str, str, str, int]:
|
|
32
|
+
"""校验遗留 running 队列行的执行身份完整性(无 IO 副作用纯函数)。
|
|
33
|
+
|
|
34
|
+
返回 ``(job_uid, execution_id, run_id, attempt_no)``;若身份非法、与序号进度
|
|
35
|
+
不匹配或对应 attempt_no 已存在已结算轨迹,抛 TaskLiteError。
|
|
36
|
+
"""
|
|
37
|
+
uid = row.get("job_uid")
|
|
38
|
+
execution_id = row.get("execution_id")
|
|
39
|
+
run_id = row.get("run_id")
|
|
40
|
+
attempt_no = row.get("attempt_no")
|
|
41
|
+
job_snap = row.get("job")
|
|
42
|
+
if (
|
|
43
|
+
not isinstance(uid, str) or not uid
|
|
44
|
+
or not isinstance(execution_id, str) or not execution_id
|
|
45
|
+
or not isinstance(run_id, str) or not run_id
|
|
46
|
+
or not execution_id_matches_run(execution_id, run_id)
|
|
47
|
+
or type(attempt_no) is not int or attempt_no < 1
|
|
48
|
+
or not isinstance(job_snap, dict)
|
|
49
|
+
or job_uid_from_snapshot(job_snap) != uid
|
|
50
|
+
or attempt_sequence != attempt_no
|
|
51
|
+
or attempt_no in settled_attempt_nos
|
|
52
|
+
):
|
|
53
|
+
raise TaskLiteError(
|
|
54
|
+
f"Invariant conflict: Job {uid!r} has an invalid or already settled running execution "
|
|
55
|
+
f"(execution_id={execution_id!r}, run_id={run_id!r}, "
|
|
56
|
+
f"attempt_no={attempt_no!r})"
|
|
57
|
+
)
|
|
58
|
+
return uid, execution_id, run_id, attempt_no
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def build_unsettled_running_recovery(
|
|
62
|
+
row: Mapping[str, Any],
|
|
63
|
+
*,
|
|
64
|
+
attempt_sequence: int,
|
|
65
|
+
settled_attempt_nos: AbstractSet[int],
|
|
66
|
+
settled_at: str | None = None,
|
|
67
|
+
) -> tuple[RunningAttemptRecovery, dict[str, Any]]:
|
|
68
|
+
"""校验遗留 running 执行身份并构造待补写失败 Attempt 与默认 failure_metadata(无 IO 副作用)。"""
|
|
69
|
+
uid, execution_id, run_id, attempt_no = validate_running_execution_identity(
|
|
70
|
+
row,
|
|
71
|
+
attempt_sequence=attempt_sequence,
|
|
72
|
+
settled_attempt_nos=settled_attempt_nos,
|
|
73
|
+
)
|
|
74
|
+
effective_settled_at = settled_at if settled_at is not None else utc_now_iso()
|
|
75
|
+
job_snap = copy.deepcopy(row["job"])
|
|
76
|
+
files = copy.deepcopy(job_snap.get("files") or {})
|
|
77
|
+
result = build_result_record(
|
|
78
|
+
message=UNSETTLED_RUNNING_ERROR_MESSAGE,
|
|
79
|
+
annotations={"completion_source": "execution"},
|
|
80
|
+
)
|
|
81
|
+
failure_metadata: dict[str, Any] = {
|
|
82
|
+
"error_message": UNSETTLED_RUNNING_ERROR_MESSAGE,
|
|
83
|
+
"reason": UNSETTLED_RUNNING_REASON,
|
|
84
|
+
"execution_id": execution_id,
|
|
85
|
+
"run_id": run_id,
|
|
86
|
+
"attempt_no": attempt_no,
|
|
87
|
+
}
|
|
88
|
+
recovery = RunningAttemptRecovery(
|
|
89
|
+
job_uid=uid,
|
|
90
|
+
attempt_no=attempt_no,
|
|
91
|
+
execution_id=execution_id,
|
|
92
|
+
run_id=run_id,
|
|
93
|
+
started_at=None,
|
|
94
|
+
settled_at=effective_settled_at,
|
|
95
|
+
job_snapshot=job_snap,
|
|
96
|
+
files=files,
|
|
97
|
+
result=result,
|
|
98
|
+
error_message=UNSETTLED_RUNNING_ERROR_MESSAGE,
|
|
99
|
+
)
|
|
100
|
+
return recovery, failure_metadata
|
|
48
101
|
|
|
49
102
|
|
|
50
103
|
class RecoveryOrchestrator:
|
|
51
|
-
"""
|
|
104
|
+
"""按数据库事实恢复遗留执行,并在框架异常退出时单口径清扫内存与进程归属。"""
|
|
52
105
|
|
|
53
106
|
def __init__(
|
|
54
107
|
self,
|
|
55
108
|
*,
|
|
56
|
-
store: "
|
|
109
|
+
store: "StateStore",
|
|
57
110
|
channel: "ExecutionChannel",
|
|
58
|
-
resources: "ResourceManager",
|
|
59
111
|
in_flight: "InFlightTracker",
|
|
60
|
-
completion: "
|
|
112
|
+
completion: "CompletionMachine",
|
|
61
113
|
) -> None:
|
|
62
114
|
self._store = store
|
|
63
115
|
self._channel = channel
|
|
64
|
-
self._resources = resources
|
|
65
116
|
self._in_flight = in_flight
|
|
66
117
|
self._completion = completion
|
|
67
118
|
|
|
68
119
|
def load_and_recover(self) -> PipelineState:
|
|
69
|
-
"""
|
|
70
|
-
|
|
71
|
-
successes =
|
|
72
|
-
failed =
|
|
120
|
+
"""单次一致快照装载并校验全部冲突,再按原执行身份逐个结算遗留 running。"""
|
|
121
|
+
snap = self._store.backend.load_consistent_snapshot()
|
|
122
|
+
successes = snap["successes"]
|
|
123
|
+
failed = snap["failed"]
|
|
73
124
|
overlap = successes.keys() & failed.keys()
|
|
74
125
|
if overlap:
|
|
75
|
-
raise
|
|
126
|
+
raise TaskLiteError(f"Invariant conflict: Jobs have both terminal records: {overlap}")
|
|
76
127
|
|
|
77
|
-
queue_rows =
|
|
128
|
+
queue_rows = snap["queue_jobs"]
|
|
78
129
|
running = [row for row in queue_rows if row["status"] == "running"]
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
)
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
)
|
|
95
|
-
):
|
|
96
|
-
raise RuntimeError(
|
|
97
|
-
f"Invariant conflict: Job {uid!r} has an invalid or already settled running execution "
|
|
98
|
-
f"(execution_id={execution_id!r}, run_id={run_id!r}, "
|
|
99
|
-
f"attempt_no={attempt_no!r})"
|
|
130
|
+
prepared: list[tuple[dict[str, Any], RunningAttemptRecovery, dict[str, Any]]] = []
|
|
131
|
+
if running:
|
|
132
|
+
job_sequences = snap.get("job_sequences") or {}
|
|
133
|
+
settled_by_uid: dict[str, set[int]] = defaultdict(set)
|
|
134
|
+
for attempt in snap.get("attempts") or ():
|
|
135
|
+
att_uid = attempt.get("job_uid")
|
|
136
|
+
att_no = attempt.get("attempt_no")
|
|
137
|
+
if isinstance(att_uid, str) and isinstance(att_no, int):
|
|
138
|
+
settled_by_uid[att_uid].add(att_no)
|
|
139
|
+
for row in running:
|
|
140
|
+
uid = row["job_uid"]
|
|
141
|
+
recovery, failure_metadata = build_unsettled_running_recovery(
|
|
142
|
+
row,
|
|
143
|
+
attempt_sequence=job_sequences.get(uid, 0),
|
|
144
|
+
settled_attempt_nos=settled_by_uid.get(uid, frozenset()),
|
|
100
145
|
)
|
|
146
|
+
prepared.append((row, recovery, failure_metadata))
|
|
101
147
|
|
|
102
148
|
state = PipelineState(
|
|
103
|
-
successes,
|
|
149
|
+
successes,
|
|
150
|
+
failed,
|
|
151
|
+
snap["cursors"],
|
|
104
152
|
[row["job"] for row in queue_rows if row["status"] == "pending"],
|
|
105
153
|
)
|
|
106
154
|
self._store.set_state(state)
|
|
107
|
-
for row in
|
|
155
|
+
for row, recovery, failure_metadata in prepared:
|
|
108
156
|
self._store.apply_failure(
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
"error_message": "上次执行未完成结算,实际结果未知",
|
|
112
|
-
"reason": "unsettled_running_recovery",
|
|
113
|
-
"execution_id": row["execution_id"],
|
|
114
|
-
"run_id": row["run_id"],
|
|
115
|
-
"attempt_no": row["attempt_no"],
|
|
116
|
-
},
|
|
157
|
+
recovery.job_uid,
|
|
158
|
+
failure_metadata,
|
|
117
159
|
job_snapshot=row["job"],
|
|
118
160
|
cascade=False,
|
|
119
|
-
attempt_no=
|
|
120
|
-
execution_id=
|
|
121
|
-
run_id=
|
|
122
|
-
started_at=
|
|
161
|
+
attempt_no=recovery.attempt_no,
|
|
162
|
+
execution_id=recovery.execution_id,
|
|
163
|
+
run_id=recovery.run_id,
|
|
164
|
+
started_at=recovery.started_at,
|
|
123
165
|
)
|
|
124
166
|
return state
|
|
125
167
|
|
|
126
168
|
def abort_in_flight(self) -> None:
|
|
127
|
-
"""
|
|
128
|
-
if not self._in_flight:
|
|
129
|
-
return
|
|
169
|
+
"""框架异常退出单口径清扫:取消待启动 → 终止通道在途 → 统一释放资源、注销在途与清空授权等待。
|
|
130
170
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
# 2. 委托 channel 执行底层 TOCTOU 闭环中止(kill、重查、清理)
|
|
171
|
+
未提交的持久化 running 记录原样保留,留待下次启动按原执行身份恢复,绝不补提交结果。
|
|
172
|
+
"""
|
|
173
|
+
self._channel.cancel_pending_launches()
|
|
135
174
|
handles = self._in_flight.active_handles()
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
175
|
+
cleanup_error: BaseException | None = None
|
|
176
|
+
try:
|
|
177
|
+
self._channel.cleanup_in_flight(handles)
|
|
178
|
+
except BaseException as error:
|
|
179
|
+
cleanup_error = cleanup_error or error
|
|
180
|
+
try:
|
|
181
|
+
self._completion.discard_in_flight()
|
|
182
|
+
except BaseException as error:
|
|
183
|
+
cleanup_error = cleanup_error or error
|
|
184
|
+
if cleanup_error is not None:
|
|
185
|
+
raise cleanup_error
|
|
143
186
|
|
|
144
187
|
|
|
145
188
|
__all__ = [
|
|
146
189
|
"RecoveryOrchestrator",
|
|
190
|
+
"UNSETTLED_RUNNING_ERROR_MESSAGE",
|
|
191
|
+
"UNSETTLED_RUNNING_REASON",
|
|
192
|
+
"build_unsettled_running_recovery",
|
|
193
|
+
"validate_running_execution_identity",
|
|
147
194
|
]
|
tasklite/engine/resource.py
CHANGED
|
@@ -8,22 +8,27 @@ from __future__ import annotations
|
|
|
8
8
|
|
|
9
9
|
import logging
|
|
10
10
|
import math
|
|
11
|
+
import os
|
|
11
12
|
import threading
|
|
12
13
|
import time
|
|
13
14
|
from abc import ABC, abstractmethod
|
|
14
15
|
from collections.abc import Iterable, Iterator, Mapping, MutableMapping
|
|
15
16
|
from dataclasses import dataclass, field
|
|
16
|
-
from enum import Enum
|
|
17
17
|
from typing import Any
|
|
18
18
|
|
|
19
19
|
from ..exceptions import TaskLiteError
|
|
20
20
|
from ..models.job import WORKER_RESOURCE
|
|
21
21
|
from ..models.task import TaskRegistry
|
|
22
|
+
from ..resource import (
|
|
23
|
+
CapacityResource as PublicCapacityResource,
|
|
24
|
+
IntervalResource as PublicIntervalResource,
|
|
25
|
+
_validate_interval_seconds,
|
|
26
|
+
_validate_max_capacity,
|
|
27
|
+
_validate_resource_name,
|
|
28
|
+
)
|
|
22
29
|
|
|
23
30
|
logger = logging.getLogger("tasklite")
|
|
24
31
|
|
|
25
|
-
_REAL_SLEEP = time.sleep
|
|
26
|
-
|
|
27
32
|
|
|
28
33
|
@dataclass(frozen=True)
|
|
29
34
|
class ResourceQueryResult:
|
|
@@ -50,9 +55,7 @@ class Resource(ABC):
|
|
|
50
55
|
"""统一内部资源基类。"""
|
|
51
56
|
|
|
52
57
|
def __init__(self, name: str) -> None:
|
|
53
|
-
|
|
54
|
-
raise TaskLiteError(f"Resource name must be a non-empty string, got {name!r}")
|
|
55
|
-
self.name = name
|
|
58
|
+
self.name = _validate_resource_name(name)
|
|
56
59
|
self._notifier: Any = None
|
|
57
60
|
|
|
58
61
|
def bind_notifier(self, notifier: Any) -> None:
|
|
@@ -93,17 +96,7 @@ class IntervalResource(Resource):
|
|
|
93
96
|
|
|
94
97
|
def __init__(self, name: str, *, interval_seconds: float) -> None:
|
|
95
98
|
super().__init__(name)
|
|
96
|
-
|
|
97
|
-
interval_seconds, bool
|
|
98
|
-
):
|
|
99
|
-
raise TaskLiteError(
|
|
100
|
-
f"interval_seconds must be a number, got {type(interval_seconds).__name__}"
|
|
101
|
-
)
|
|
102
|
-
if not math.isfinite(interval_seconds) or interval_seconds <= 0:
|
|
103
|
-
raise TaskLiteError(
|
|
104
|
-
f"interval_seconds must be finite and > 0, got {interval_seconds!r}"
|
|
105
|
-
)
|
|
106
|
-
self.interval_seconds = float(interval_seconds)
|
|
99
|
+
self.interval_seconds = _validate_interval_seconds(interval_seconds)
|
|
107
100
|
self.next_available = time.monotonic()
|
|
108
101
|
|
|
109
102
|
def can_acquire(self, amount: float) -> ResourceQueryResult:
|
|
@@ -137,13 +130,7 @@ class CapacityResource(Resource):
|
|
|
137
130
|
|
|
138
131
|
def __init__(self, name: str, *, max_capacity: float) -> None:
|
|
139
132
|
super().__init__(name)
|
|
140
|
-
|
|
141
|
-
raise TaskLiteError(
|
|
142
|
-
f"max_capacity must be a number, got {type(max_capacity).__name__} ({max_capacity!r})"
|
|
143
|
-
)
|
|
144
|
-
if not math.isfinite(max_capacity) or max_capacity < 0:
|
|
145
|
-
raise TaskLiteError(f"max_capacity must be finite and >= 0, got {max_capacity!r}")
|
|
146
|
-
self.max_capacity = float(max_capacity)
|
|
133
|
+
self.max_capacity = _validate_max_capacity(max_capacity)
|
|
147
134
|
self.used = 0.0
|
|
148
135
|
self.release_overruns = 0
|
|
149
136
|
|
|
@@ -207,65 +194,6 @@ class ResourceEvaluation:
|
|
|
207
194
|
reason: str | None = None
|
|
208
195
|
|
|
209
196
|
|
|
210
|
-
class LeaseStatus(str, Enum):
|
|
211
|
-
"""资源租约状态(兼容保留)。"""
|
|
212
|
-
|
|
213
|
-
RESERVED = "reserved"
|
|
214
|
-
CLAIMED = "claimed"
|
|
215
|
-
RELEASED = "released"
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
@dataclass
|
|
219
|
-
class ResourceLease:
|
|
220
|
-
"""资源租约(兼容保留)。"""
|
|
221
|
-
|
|
222
|
-
manager: ResourceManager | None = None
|
|
223
|
-
job_uid: str = ""
|
|
224
|
-
acquired: list[tuple[str, float]] = field(default_factory=list)
|
|
225
|
-
rate_limits: list[tuple[str, float]] = field(default_factory=list)
|
|
226
|
-
status: LeaseStatus = LeaseStatus.CLAIMED
|
|
227
|
-
|
|
228
|
-
def claim(self) -> None:
|
|
229
|
-
self.status = LeaseStatus.CLAIMED
|
|
230
|
-
|
|
231
|
-
def release(self) -> None:
|
|
232
|
-
if self.status == LeaseStatus.RELEASED:
|
|
233
|
-
return
|
|
234
|
-
self.status = LeaseStatus.RELEASED
|
|
235
|
-
if self.manager is not None and self.acquired:
|
|
236
|
-
self.manager.release_all(self.acquired, job_uid=self.job_uid)
|
|
237
|
-
|
|
238
|
-
def cancel(self) -> None:
|
|
239
|
-
self.release()
|
|
240
|
-
|
|
241
|
-
def __enter__(self) -> ResourceLease:
|
|
242
|
-
return self
|
|
243
|
-
|
|
244
|
-
def __exit__(self, exc_type, exc_val, exc_tb) -> None:
|
|
245
|
-
if exc_type is not None and self.status != LeaseStatus.RELEASED:
|
|
246
|
-
self.release()
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
@dataclass
|
|
250
|
-
class NullResourceLease(ResourceLease):
|
|
251
|
-
"""空资源租约。"""
|
|
252
|
-
|
|
253
|
-
manager: Any = None
|
|
254
|
-
job_uid: str = ""
|
|
255
|
-
acquired: list[tuple[str, float]] = field(default_factory=list)
|
|
256
|
-
rate_limits: list[tuple[str, float]] = field(default_factory=list)
|
|
257
|
-
status: LeaseStatus = LeaseStatus.RELEASED
|
|
258
|
-
|
|
259
|
-
def claim(self) -> None:
|
|
260
|
-
pass
|
|
261
|
-
|
|
262
|
-
def release(self) -> None:
|
|
263
|
-
pass
|
|
264
|
-
|
|
265
|
-
def cancel(self) -> None:
|
|
266
|
-
pass
|
|
267
|
-
|
|
268
|
-
|
|
269
197
|
class RunNotifier:
|
|
270
198
|
"""内部运行期唤醒通知器。
|
|
271
199
|
|
|
@@ -288,15 +216,37 @@ class RunNotifier:
|
|
|
288
216
|
with self._lock:
|
|
289
217
|
return self._seq
|
|
290
218
|
|
|
219
|
+
_WIN_WAIT_SLICE_SECONDS: float = 0.1
|
|
220
|
+
|
|
291
221
|
def wait(self, last_seq: int, timeout: float | None = None) -> bool:
|
|
292
|
-
"""如果快照后已有新通知,立即返回 True;否则进入等待,直到到期或被新通知唤醒。
|
|
222
|
+
"""如果快照后已有新通知,立即返回 True;否则进入等待,直到到期或被新通知唤醒。
|
|
223
|
+
|
|
224
|
+
Windows 下长超时或无限等待按 <= 0.1s 切片循环,确保主线程阻塞在长作业
|
|
225
|
+
等待期间也能在 100ms 内响应 SIGBREAK / SIGINT。
|
|
226
|
+
"""
|
|
293
227
|
with self._lock:
|
|
294
228
|
if self._seq != last_seq:
|
|
295
229
|
return True
|
|
296
230
|
self._event.clear()
|
|
297
|
-
if
|
|
298
|
-
|
|
299
|
-
|
|
231
|
+
if os.name != "nt" or (timeout is not None and timeout <= self._WIN_WAIT_SLICE_SECONDS):
|
|
232
|
+
return self._event.wait(timeout=timeout)
|
|
233
|
+
if timeout is None:
|
|
234
|
+
while True:
|
|
235
|
+
if self._event.wait(timeout=self._WIN_WAIT_SLICE_SECONDS):
|
|
236
|
+
return True
|
|
237
|
+
with self._lock:
|
|
238
|
+
if self._seq != last_seq:
|
|
239
|
+
return True
|
|
240
|
+
deadline = time.monotonic() + max(0.0, timeout)
|
|
241
|
+
while True:
|
|
242
|
+
remaining = deadline - time.monotonic()
|
|
243
|
+
if remaining <= 0:
|
|
244
|
+
return self._event.is_set()
|
|
245
|
+
if self._event.wait(timeout=min(self._WIN_WAIT_SLICE_SECONDS, remaining)):
|
|
246
|
+
return True
|
|
247
|
+
with self._lock:
|
|
248
|
+
if self._seq != last_seq:
|
|
249
|
+
return True
|
|
300
250
|
|
|
301
251
|
def clear(self) -> None:
|
|
302
252
|
with self._lock:
|
|
@@ -488,19 +438,6 @@ class ResourceManager(MutableMapping[str, Resource]):
|
|
|
488
438
|
continue
|
|
489
439
|
res.release(amount)
|
|
490
440
|
|
|
491
|
-
def reserve(
|
|
492
|
-
self,
|
|
493
|
-
task_type: str,
|
|
494
|
-
declared_resources: Mapping[str, float]
|
|
495
|
-
| Iterable[tuple[str, float]]
|
|
496
|
-
| None = None,
|
|
497
|
-
*,
|
|
498
|
-
job_uid: str | None = None,
|
|
499
|
-
) -> ResourceLease:
|
|
500
|
-
"""租约封装(直接连续获取,不再使用两阶段预扣)。"""
|
|
501
|
-
acquired = self.acquire_all(task_type, declared_resources, job_uid=job_uid)
|
|
502
|
-
return ResourceLease(manager=self, job_uid=job_uid or "", acquired=acquired)
|
|
503
|
-
|
|
504
441
|
def can_acquire_worker(self, amount: float = 1.0) -> ResourceQueryResult:
|
|
505
442
|
"""检查工作者槽位可用性。"""
|
|
506
443
|
worker_res = self._resources.get(WORKER_RESOURCE)
|
|
@@ -509,15 +446,25 @@ class ResourceManager(MutableMapping[str, Resource]):
|
|
|
509
446
|
return worker_res.can_acquire(amount)
|
|
510
447
|
|
|
511
448
|
|
|
449
|
+
def adapt_resource(resource: PublicCapacityResource | PublicIntervalResource) -> Resource:
|
|
450
|
+
"""将公开资源配置规格适配为引擎内部可调度资源实例。"""
|
|
451
|
+
if isinstance(resource, PublicCapacityResource):
|
|
452
|
+
return CapacityResource(resource.name, max_capacity=resource.max_capacity)
|
|
453
|
+
if isinstance(resource, PublicIntervalResource):
|
|
454
|
+
return IntervalResource(resource.name, interval_seconds=resource.interval_seconds)
|
|
455
|
+
raise TaskLiteError(
|
|
456
|
+
f"register_resource expects a CapacityResource or IntervalResource instance, "
|
|
457
|
+
f"got {type(resource).__name__}"
|
|
458
|
+
)
|
|
459
|
+
|
|
460
|
+
|
|
512
461
|
__all__ = [
|
|
513
462
|
"Resource",
|
|
514
463
|
"IntervalResource",
|
|
515
464
|
"CapacityResource",
|
|
465
|
+
"adapt_resource",
|
|
516
466
|
"ResourceQueryResult",
|
|
517
467
|
"ResourceEvaluation",
|
|
518
468
|
"ResourceManager",
|
|
519
469
|
"RunNotifier",
|
|
520
|
-
"ResourceLease",
|
|
521
|
-
"NullResourceLease",
|
|
522
|
-
"LeaseStatus",
|
|
523
470
|
]
|