Skip to content

Commit d383ce3

Browse files
committed
♻️ clean: remove construct method on result object.
1 parent fb3dd97 commit d383ce3

3 files changed

Lines changed: 93 additions & 123 deletions

File tree

src/ddeutil/workflow/job.py

Lines changed: 92 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,7 +1136,6 @@ def self_hosted_execute(
11361136
params: DictData,
11371137
*,
11381138
run_id: StrOrNone = None,
1139-
parent_run_id: StrOrNone = None,
11401139
event: Optional[Event] = None,
11411140
) -> Result: # pragma: no cov
11421141
"""Self-Hosted job execution with passing dynamic parameters from the
@@ -1146,30 +1145,35 @@ def self_hosted_execute(
11461145
:param job: (Job) A job model that want to execute.
11471146
:param params: (DictData) A parameter data.
11481147
:param run_id: (str) A job running ID.
1149-
:param parent_run_id: (str) A parent workflow running ID.
11501148
:param event: (Event) An Event manager instance that use to cancel this
11511149
execution if it forces stopped by parent execution.
11521150
11531151
:rtype: Result
11541152
"""
1155-
result: Result = Result.construct_with_rs_or_id(
1156-
run_id=run_id,
1157-
parent_run_id=parent_run_id,
1158-
id_logic=(job.id or "EMPTY"),
1159-
extras=job.extras,
1153+
parent_run_id: StrOrNone = run_id
1154+
run_id: str = gen_id((job.id or "EMPTY"), unique=True)
1155+
trace: Trace = get_trace(
1156+
run_id, parent_run_id=parent_run_id, extras=job.extras
11601157
)
1161-
1162-
result.trace.info("[JOB]: Start self-hosted executor.")
1158+
context: DictData = {"status": WAIT}
1159+
trace.info("[JOB]: Start self-hosted executor.")
11631160

11641161
if event and event.is_set():
1165-
return result.catch(
1162+
return Result(
1163+
run_id=run_id,
1164+
parent_run_id=parent_run_id,
11661165
status=CANCEL,
1167-
context={
1168-
"errors": JobCancelError(
1169-
"Execution was canceled from the event before start "
1170-
"self-hosted execution."
1171-
).to_dict()
1172-
},
1166+
context=catch(
1167+
context,
1168+
status=CANCEL,
1169+
updated={
1170+
"errors": JobCancelError(
1171+
"Execution was canceled from the event before start "
1172+
"self-hosted execution."
1173+
).to_dict()
1174+
},
1175+
),
1176+
extras=job.extras,
11731177
)
11741178

11751179
import requests
@@ -1181,27 +1185,41 @@ def self_hosted_execute(
11811185
data={
11821186
"job": job.model_dump(),
11831187
"params": params,
1184-
"result": result.__dict__,
1188+
"run_id": parent_run_id,
1189+
"extras": job.extras,
11851190
},
11861191
)
11871192
except requests.exceptions.RequestException as e:
1188-
return result.catch(status=FAILED, context={"errors": to_dict(e)})
1193+
return Result(
1194+
run_id=run_id,
1195+
parent_run_id=parent_run_id,
1196+
status=FAILED,
1197+
context=catch(
1198+
context, status=FAILED, updated={"errors": to_dict(e)}
1199+
),
1200+
extras=job.extras,
1201+
)
11891202

11901203
if resp.status_code != 200:
11911204
raise JobError(
11921205
f"Job execution got error response from self-hosted: "
11931206
f"{job.runs_on.args.host!r}"
11941207
)
11951208

1196-
return result.catch(status=SUCCESS)
1209+
return Result(
1210+
run_id=run_id,
1211+
parent_run_id=parent_run_id,
1212+
status=SUCCESS,
1213+
context=catch(context, status=SUCCESS),
1214+
extras=job.extras,
1215+
)
11971216

11981217

11991218
def azure_batch_execute(
12001219
job: Job,
12011220
params: DictData,
12021221
*,
12031222
run_id: StrOrNone = None,
1204-
parent_run_id: StrOrNone = None,
12051223
event: Optional[Event] = None,
12061224
) -> Result: # pragma: no cov
12071225
"""Azure Batch job execution that will run all job's stages on the Azure
@@ -1225,32 +1243,43 @@ def azure_batch_execute(
12251243
:param job:
12261244
:param params:
12271245
:param run_id:
1228-
:param parent_run_id:
12291246
:param event:
12301247
12311248
:rtype: Result
12321249
"""
1233-
result: Result = Result.construct_with_rs_or_id(
1234-
run_id=run_id,
1235-
parent_run_id=parent_run_id,
1236-
id_logic=(job.id or "EMPTY"),
1237-
extras=job.extras,
1250+
parent_run_id: StrOrNone = run_id
1251+
run_id: str = gen_id((job.id or "EMPTY"), unique=True)
1252+
trace: Trace = get_trace(
1253+
run_id, parent_run_id=parent_run_id, extras=job.extras
12381254
)
1239-
1240-
result.trace.info("[JOB]: Start Azure Batch executor.")
1255+
context: DictData = {"status": WAIT}
1256+
trace.info("[JOB]: Start Azure Batch executor.")
12411257

12421258
if event and event.is_set():
1243-
return result.catch(
1259+
return Result(
1260+
run_id=run_id,
1261+
parent_run_id=parent_run_id,
12441262
status=CANCEL,
1245-
context={
1246-
"errors": JobCancelError(
1247-
"Execution was canceled from the event before start "
1248-
"azure-batch execution."
1249-
).to_dict()
1250-
},
1263+
context=catch(
1264+
context,
1265+
status=CANCEL,
1266+
updated={
1267+
"errors": JobCancelError(
1268+
"Execution was canceled from the event before start "
1269+
"self-hosted execution."
1270+
).to_dict()
1271+
},
1272+
),
1273+
extras=job.extras,
12511274
)
12521275
print(params)
1253-
return result.catch(status=SUCCESS)
1276+
return Result(
1277+
run_id=run_id,
1278+
parent_run_id=parent_run_id,
1279+
status=SUCCESS,
1280+
context=catch(context, status=SUCCESS),
1281+
extras=job.extras,
1282+
)
12541283

12551284

12561285
def docker_execution(
@@ -1268,24 +1297,36 @@ def docker_execution(
12681297
- Install this workflow package
12691298
- Start push job to run to target Docker container.
12701299
"""
1271-
result: Result = Result.construct_with_rs_or_id(
1272-
run_id=run_id,
1273-
parent_run_id=parent_run_id,
1274-
id_logic=(job.id or "EMPTY"),
1275-
extras=job.extras,
1300+
parent_run_id: StrOrNone = run_id
1301+
run_id: str = gen_id((job.id or "EMPTY"), unique=True)
1302+
trace: Trace = get_trace(
1303+
run_id, parent_run_id=parent_run_id, extras=job.extras
12761304
)
1277-
1278-
result.trace.info("[JOB]: Start Docker executor.")
1305+
context: DictData = {"status": WAIT}
1306+
trace.info("[JOB]: Start Docker executor.")
12791307

12801308
if event and event.is_set():
1281-
return result.catch(
1309+
return Result(
1310+
run_id=run_id,
1311+
parent_run_id=parent_run_id,
12821312
status=CANCEL,
1283-
context={
1284-
"errors": JobCancelError(
1285-
"Execution was canceled from the event before start "
1286-
"start docker execution."
1287-
).to_dict()
1288-
},
1313+
context=catch(
1314+
context,
1315+
status=CANCEL,
1316+
updated={
1317+
"errors": JobCancelError(
1318+
"Execution was canceled from the event before start "
1319+
"self-hosted execution."
1320+
).to_dict()
1321+
},
1322+
),
1323+
extras=job.extras,
12891324
)
12901325
print(params)
1291-
return result.catch(status=SUCCESS)
1326+
return Result(
1327+
run_id=run_id,
1328+
parent_run_id=parent_run_id,
1329+
status=SUCCESS,
1330+
context=catch(context, status=SUCCESS),
1331+
extras=job.extras,
1332+
)

src/ddeutil/workflow/result.py

Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
from .__types import DictData
4444
from .audits import Trace, get_trace
4545
from .errors import ResultError
46-
from .utils import default_gen_id, gen_id, get_dt_ntz_now
46+
from .utils import default_gen_id, get_dt_ntz_now
4747

4848

4949
class Status(str, Enum):
@@ -194,60 +194,6 @@ class Result:
194194
trace: Optional[Trace] = field(default=None, compare=False, repr=False)
195195
extras: DictData = field(default_factory=dict, compare=False, repr=False)
196196

197-
@classmethod
198-
def construct_with_id(
199-
cls,
200-
run_id: Optional[str] = None,
201-
parent_run_id: Optional[str] = None,
202-
id_logic: Optional[str] = None,
203-
*,
204-
extras: DictData | None = None,
205-
):
206-
return cls(
207-
run_id=(run_id or gen_id(id_logic or "", unique=True)),
208-
parent_run_id=parent_run_id,
209-
ts=get_dt_ntz_now(),
210-
extras=(extras or {}),
211-
)
212-
213-
@classmethod
214-
def construct_with_rs_or_id(
215-
cls,
216-
result: Optional[Result] = None,
217-
run_id: Optional[str] = None,
218-
parent_run_id: Optional[str] = None,
219-
id_logic: Optional[str] = None,
220-
*,
221-
extras: DictData | None = None,
222-
) -> Self:
223-
"""Create the Result object or set parent running id if passing Result
224-
object.
225-
226-
Args:
227-
result: A Result instance.
228-
run_id: A running ID.
229-
parent_run_id: A parent running ID.
230-
id_logic: A logic function that use to generate a running ID.
231-
extras: An extra parameter that want to override the core config.
232-
233-
Returns:
234-
Self: The constructed or updated Result instance.
235-
"""
236-
if result is None:
237-
return cls(
238-
run_id=(run_id or gen_id(id_logic or "", unique=True)),
239-
parent_run_id=parent_run_id,
240-
ts=get_dt_ntz_now(),
241-
extras=(extras or {}),
242-
)
243-
elif parent_run_id:
244-
result.set_parent_run_id(parent_run_id)
245-
246-
if extras is not None:
247-
result.extras.update(extras)
248-
249-
return result
250-
251197
@model_validator(mode="after")
252198
def __prepare_trace(self) -> Self:
253199
"""Prepare trace field that want to pass after its initialize step.

tests/test_result.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,6 @@ def test_result_default():
3838
assert rs.alive_time() > 0
3939

4040

41-
def test_result_construct_with_rs_or_id():
42-
rs = Result.construct_with_rs_or_id(run_id="foo", extras={"test": "value"})
43-
assert rs.run_id == "foo"
44-
assert rs.parent_run_id is None
45-
assert rs.extras == {"test": "value"}
46-
47-
rs = Result.construct_with_rs_or_id(
48-
run_id="foo",
49-
parent_run_id="baz",
50-
result=Result(run_id="bar"),
51-
)
52-
53-
assert rs.run_id != "foo"
54-
assert rs.run_id == "bar"
55-
assert rs.parent_run_id == "baz"
56-
57-
5841
def test_result_context():
5942
rs: Result = Result(context={"params": {"source": "src", "target": "tgt"}})
6043
rs.context.update({"additional-key": "new-value-to-add"})

0 commit comments

Comments
 (0)