runnable 0.35.0__py3-none-any.whl → 0.36.0__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.
Files changed (42) hide show
  1. extensions/job_executor/__init__.py +3 -4
  2. extensions/job_executor/emulate.py +106 -0
  3. extensions/job_executor/k8s.py +8 -8
  4. extensions/job_executor/local_container.py +13 -14
  5. extensions/nodes/__init__.py +0 -0
  6. extensions/nodes/conditional.py +7 -5
  7. extensions/nodes/fail.py +72 -0
  8. extensions/nodes/map.py +350 -0
  9. extensions/nodes/parallel.py +159 -0
  10. extensions/nodes/stub.py +89 -0
  11. extensions/nodes/success.py +72 -0
  12. extensions/nodes/task.py +92 -0
  13. extensions/pipeline_executor/__init__.py +24 -26
  14. extensions/pipeline_executor/argo.py +18 -15
  15. extensions/pipeline_executor/emulate.py +112 -0
  16. extensions/pipeline_executor/local.py +4 -4
  17. extensions/pipeline_executor/local_container.py +19 -79
  18. extensions/pipeline_executor/mocked.py +4 -4
  19. extensions/pipeline_executor/retry.py +6 -10
  20. extensions/tasks/torch.py +1 -1
  21. runnable/__init__.py +0 -8
  22. runnable/catalog.py +1 -21
  23. runnable/cli.py +0 -59
  24. runnable/context.py +519 -28
  25. runnable/datastore.py +51 -54
  26. runnable/defaults.py +12 -34
  27. runnable/entrypoints.py +82 -440
  28. runnable/exceptions.py +35 -34
  29. runnable/executor.py +13 -20
  30. runnable/names.py +1 -1
  31. runnable/nodes.py +16 -15
  32. runnable/parameters.py +2 -2
  33. runnable/sdk.py +66 -163
  34. runnable/tasks.py +62 -21
  35. runnable/utils.py +6 -268
  36. {runnable-0.35.0.dist-info → runnable-0.36.0.dist-info}/METADATA +1 -1
  37. runnable-0.36.0.dist-info/RECORD +74 -0
  38. {runnable-0.35.0.dist-info → runnable-0.36.0.dist-info}/entry_points.txt +8 -7
  39. extensions/nodes/nodes.py +0 -778
  40. runnable-0.35.0.dist-info/RECORD +0 -66
  41. {runnable-0.35.0.dist-info → runnable-0.36.0.dist-info}/WHEEL +0 -0
  42. {runnable-0.35.0.dist-info → runnable-0.36.0.dist-info}/licenses/LICENSE +0 -0
extensions/nodes/nodes.py DELETED
@@ -1,778 +0,0 @@
1
- import importlib
2
- import logging
3
- import os
4
- import sys
5
- from collections import OrderedDict
6
- from copy import deepcopy
7
- from datetime import datetime
8
- from typing import Any, Callable, Dict, List, Optional, Tuple, Union, cast
9
-
10
- from pydantic import ConfigDict, Field, field_serializer
11
-
12
- from runnable import console, datastore, defaults, utils
13
- from runnable.datastore import (
14
- JsonParameter,
15
- MetricParameter,
16
- ObjectParameter,
17
- Parameter,
18
- StepLog,
19
- )
20
- from runnable.defaults import TypeMapVariable
21
- from runnable.graph import Graph, create_graph
22
- from runnable.nodes import CompositeNode, ExecutableNode, TerminalNode
23
- from runnable.tasks import BaseTaskType, create_task
24
-
25
- logger = logging.getLogger(defaults.LOGGER_NAME)
26
-
27
-
28
- class TaskNode(ExecutableNode):
29
- """
30
- A node of type Task.
31
-
32
- This node does the actual function execution of the graph in all cases.
33
- """
34
-
35
- executable: BaseTaskType = Field(exclude=True)
36
- node_type: str = Field(default="task", serialization_alias="type")
37
-
38
- # It is technically not allowed as parse_from_config filters them.
39
- # This is just to get the task level configuration to be present during serialization.
40
- model_config = ConfigDict(extra="allow")
41
-
42
- @classmethod
43
- def parse_from_config(cls, config: Dict[str, Any]) -> "TaskNode":
44
- # separate task config from node config
45
- task_config = {
46
- k: v for k, v in config.items() if k not in TaskNode.model_fields.keys()
47
- }
48
- node_config = {
49
- k: v for k, v in config.items() if k in TaskNode.model_fields.keys()
50
- }
51
-
52
- executable = create_task(task_config)
53
- return cls(executable=executable, **node_config, **task_config)
54
-
55
- def get_summary(self) -> Dict[str, Any]:
56
- summary = {
57
- "name": self.name,
58
- "type": self.node_type,
59
- "executable": self.executable.get_summary(),
60
- "catalog": self._get_catalog_settings(),
61
- }
62
-
63
- return summary
64
-
65
- def execute(
66
- self,
67
- mock=False,
68
- map_variable: TypeMapVariable = None,
69
- attempt_number: int = 1,
70
- ) -> StepLog:
71
- """
72
- All that we do in runnable is to come to this point where we actually execute the command.
73
-
74
- Args:
75
- executor (_type_): The executor class
76
- mock (bool, optional): If we should just mock and not execute. Defaults to False.
77
- map_variable (dict, optional): If the node is part of internal branch. Defaults to None.
78
-
79
- Returns:
80
- StepAttempt: The attempt object
81
- """
82
- step_log = self._context.run_log_store.get_step_log(
83
- self._get_step_log_name(map_variable), self._context.run_id
84
- )
85
-
86
- if not mock:
87
- # Do not run if we are mocking the execution, could be useful for caching and dry runs
88
- attempt_log = self.executable.execute_command(map_variable=map_variable)
89
- attempt_log.attempt_number = attempt_number
90
- else:
91
- attempt_log = datastore.StepAttempt(
92
- status=defaults.SUCCESS,
93
- start_time=str(datetime.now()),
94
- end_time=str(datetime.now()),
95
- attempt_number=attempt_number,
96
- )
97
-
98
- logger.info(f"attempt_log: {attempt_log}")
99
- logger.info(f"Step {self.name} completed with status: {attempt_log.status}")
100
-
101
- step_log.status = attempt_log.status
102
- step_log.attempts.append(attempt_log)
103
-
104
- return step_log
105
-
106
-
107
- class FailNode(TerminalNode):
108
- """
109
- A leaf node of the graph that represents a failure node
110
- """
111
-
112
- node_type: str = Field(default="fail", serialization_alias="type")
113
-
114
- @classmethod
115
- def parse_from_config(cls, config: Dict[str, Any]) -> "FailNode":
116
- return cast("FailNode", super().parse_from_config(config))
117
-
118
- def get_summary(self) -> Dict[str, Any]:
119
- summary = {
120
- "name": self.name,
121
- "type": self.node_type,
122
- }
123
-
124
- return summary
125
-
126
- def execute(
127
- self,
128
- mock=False,
129
- map_variable: TypeMapVariable = None,
130
- attempt_number: int = 1,
131
- ) -> StepLog:
132
- """
133
- Execute the failure node.
134
- Set the run or branch log status to failure.
135
-
136
- Args:
137
- executor (_type_): the executor class
138
- mock (bool, optional): If we should just mock and not do the actual execution. Defaults to False.
139
- map_variable (dict, optional): If the node belongs to internal branches. Defaults to None.
140
-
141
- Returns:
142
- StepAttempt: The step attempt object
143
- """
144
- step_log = self._context.run_log_store.get_step_log(
145
- self._get_step_log_name(map_variable), self._context.run_id
146
- )
147
-
148
- attempt_log = datastore.StepAttempt(
149
- status=defaults.SUCCESS,
150
- start_time=str(datetime.now()),
151
- end_time=str(datetime.now()),
152
- attempt_number=attempt_number,
153
- )
154
-
155
- run_or_branch_log = self._context.run_log_store.get_branch_log(
156
- self._get_branch_log_name(map_variable), self._context.run_id
157
- )
158
- run_or_branch_log.status = defaults.FAIL
159
- self._context.run_log_store.add_branch_log(
160
- run_or_branch_log, self._context.run_id
161
- )
162
-
163
- step_log.status = attempt_log.status
164
-
165
- step_log.attempts.append(attempt_log)
166
-
167
- return step_log
168
-
169
-
170
- class SuccessNode(TerminalNode):
171
- """
172
- A leaf node of the graph that represents a success node
173
- """
174
-
175
- node_type: str = Field(default="success", serialization_alias="type")
176
-
177
- @classmethod
178
- def parse_from_config(cls, config: Dict[str, Any]) -> "SuccessNode":
179
- return cast("SuccessNode", super().parse_from_config(config))
180
-
181
- def get_summary(self) -> Dict[str, Any]:
182
- summary = {
183
- "name": self.name,
184
- "type": self.node_type,
185
- }
186
-
187
- return summary
188
-
189
- def execute(
190
- self,
191
- mock=False,
192
- map_variable: TypeMapVariable = None,
193
- attempt_number: int = 1,
194
- ) -> StepLog:
195
- """
196
- Execute the success node.
197
- Set the run or branch log status to success.
198
-
199
- Args:
200
- executor (_type_): The executor class
201
- mock (bool, optional): If we should just mock and not perform anything. Defaults to False.
202
- map_variable (dict, optional): If the node belongs to an internal branch. Defaults to None.
203
-
204
- Returns:
205
- StepAttempt: The step attempt object
206
- """
207
- step_log = self._context.run_log_store.get_step_log(
208
- self._get_step_log_name(map_variable), self._context.run_id
209
- )
210
-
211
- attempt_log = datastore.StepAttempt(
212
- status=defaults.SUCCESS,
213
- start_time=str(datetime.now()),
214
- end_time=str(datetime.now()),
215
- attempt_number=attempt_number,
216
- )
217
-
218
- run_or_branch_log = self._context.run_log_store.get_branch_log(
219
- self._get_branch_log_name(map_variable), self._context.run_id
220
- )
221
- run_or_branch_log.status = defaults.SUCCESS
222
- self._context.run_log_store.add_branch_log(
223
- run_or_branch_log, self._context.run_id
224
- )
225
-
226
- step_log.status = attempt_log.status
227
-
228
- step_log.attempts.append(attempt_log)
229
-
230
- return step_log
231
-
232
-
233
- class ParallelNode(CompositeNode):
234
- """
235
- A composite node containing many graph objects within itself.
236
-
237
- The structure is generally:
238
- ParallelNode:
239
- Branch A:
240
- Sub graph definition
241
- Branch B:
242
- Sub graph definition
243
- . . .
244
-
245
- """
246
-
247
- node_type: str = Field(default="parallel", serialization_alias="type")
248
- branches: Dict[str, Graph]
249
-
250
- def get_summary(self) -> Dict[str, Any]:
251
- summary = {
252
- "name": self.name,
253
- "type": self.node_type,
254
- "branches": [branch.get_summary() for branch in self.branches.values()],
255
- }
256
-
257
- return summary
258
-
259
- @field_serializer("branches")
260
- def ser_branches(self, branches: Dict[str, Graph]) -> Dict[str, Graph]:
261
- ret: Dict[str, Graph] = {}
262
-
263
- for branch_name, branch in branches.items():
264
- ret[branch_name.split(".")[-1]] = branch
265
-
266
- return ret
267
-
268
- @classmethod
269
- def parse_from_config(cls, config: Dict[str, Any]) -> "ParallelNode":
270
- internal_name = cast(str, config.get("internal_name"))
271
-
272
- config_branches = config.pop("branches", {})
273
- branches = {}
274
- for branch_name, branch_config in config_branches.items():
275
- sub_graph = create_graph(
276
- deepcopy(branch_config),
277
- internal_branch_name=internal_name + "." + branch_name,
278
- )
279
- branches[internal_name + "." + branch_name] = sub_graph
280
-
281
- if not branches:
282
- raise Exception("A parallel node should have branches")
283
- return cls(branches=branches, **config)
284
-
285
- def _get_branch_by_name(self, branch_name: str) -> Graph:
286
- if branch_name in self.branches:
287
- return self.branches[branch_name]
288
-
289
- raise Exception(f"Branch {branch_name} does not exist")
290
-
291
- def fan_out(self, map_variable: TypeMapVariable = None):
292
- """
293
- The general fan out method for a node of type Parallel.
294
- This method assumes that the step log has already been created.
295
-
296
- 3rd party orchestrators should create the step log and use this method to create the branch logs.
297
-
298
- Args:
299
- executor (BaseExecutor): The executor class as defined by the config
300
- map_variable (dict, optional): If the node is part of a map node. Defaults to None.
301
- """
302
- # Prepare the branch logs
303
- for internal_branch_name, _ in self.branches.items():
304
- effective_branch_name = self._resolve_map_placeholders(
305
- internal_branch_name, map_variable=map_variable
306
- )
307
-
308
- branch_log = self._context.run_log_store.create_branch_log(
309
- effective_branch_name
310
- )
311
- branch_log.status = defaults.PROCESSING
312
- self._context.run_log_store.add_branch_log(branch_log, self._context.run_id)
313
-
314
- def execute_as_graph(self, map_variable: TypeMapVariable = None):
315
- """
316
- This function does the actual execution of the sub-branches of the parallel node.
317
-
318
- From a design perspective, this function should not be called if the execution is 3rd party orchestrated.
319
-
320
- The modes that render the job specifications, do not need to interact with this node at all as they have their
321
- own internal mechanisms of handing parallel states.
322
- If they do not, you can find a way using as-is nodes as hack nodes.
323
-
324
- The execution of a dag, could result in
325
- * The dag being completely executed with a definite (fail, success) state in case of
326
- local or local-container execution
327
- * The dag being in a processing state with PROCESSING status in case of local-aws-batch
328
-
329
- Only fail state is considered failure during this phase of execution.
330
-
331
- Args:
332
- executor (Executor): The Executor as per the use config
333
- **kwargs: Optional kwargs passed around
334
- """
335
- self.fan_out(map_variable=map_variable)
336
-
337
- for _, branch in self.branches.items():
338
- self._context.executor.execute_graph(branch, map_variable=map_variable)
339
-
340
- self.fan_in(map_variable=map_variable)
341
-
342
- def fan_in(self, map_variable: TypeMapVariable = None):
343
- """
344
- The general fan in method for a node of type Parallel.
345
-
346
- 3rd party orchestrators should use this method to find the status of the composite step.
347
-
348
- Args:
349
- executor (BaseExecutor): The executor class as defined by the config
350
- map_variable (dict, optional): If the node is part of a map. Defaults to None.
351
- """
352
- effective_internal_name = self._resolve_map_placeholders(
353
- self.internal_name, map_variable=map_variable
354
- )
355
- step_success_bool = True
356
- for internal_branch_name, _ in self.branches.items():
357
- effective_branch_name = self._resolve_map_placeholders(
358
- internal_branch_name, map_variable=map_variable
359
- )
360
- branch_log = self._context.run_log_store.get_branch_log(
361
- effective_branch_name, self._context.run_id
362
- )
363
-
364
- if branch_log.status != defaults.SUCCESS:
365
- step_success_bool = False
366
-
367
- # Collate all the results and update the status of the step
368
-
369
- step_log = self._context.run_log_store.get_step_log(
370
- effective_internal_name, self._context.run_id
371
- )
372
-
373
- if step_success_bool: #  If none failed
374
- step_log.status = defaults.SUCCESS
375
- else:
376
- step_log.status = defaults.FAIL
377
-
378
- self._context.run_log_store.add_step_log(step_log, self._context.run_id)
379
-
380
-
381
- class MapNode(CompositeNode):
382
- """
383
- A composite node that contains ONE graph object within itself that has to be executed with an iterable.
384
-
385
- The structure is generally:
386
- MapNode:
387
- branch
388
-
389
- The config is expected to have a variable 'iterate_on' and iterate_as which are looked for in the parameters.
390
- for iter_variable in parameters['iterate_on']:
391
- Execute the Branch by sending {'iterate_as': iter_variable}
392
-
393
- The internal naming convention creates branches dynamically based on the iteration value
394
- """
395
-
396
- # TODO: Should it be one function or a dict of functions indexed by the return name
397
-
398
- node_type: str = Field(default="map", serialization_alias="type")
399
- iterate_on: str
400
- iterate_as: str
401
- reducer: Optional[str] = Field(default=None)
402
- branch: Graph
403
-
404
- def get_summary(self) -> Dict[str, Any]:
405
- summary = {
406
- "name": self.name,
407
- "type": self.node_type,
408
- "branch": self.branch.get_summary(),
409
- "iterate_on": self.iterate_on,
410
- "iterate_as": self.iterate_as,
411
- "reducer": self.reducer,
412
- }
413
-
414
- return summary
415
-
416
- def get_reducer_function(self):
417
- if not self.reducer:
418
- return lambda *x: list(x) # returns a list of the args
419
-
420
- # try a lambda function
421
- try:
422
- f = eval(self.reducer)
423
- if callable(f):
424
- return f
425
- except SyntaxError:
426
- logger.info(f"{self.reducer} is not a lambda function")
427
-
428
- # Load the reducer function from dotted path
429
- mod, func = utils.get_module_and_attr_names(self.reducer)
430
- sys.path.insert(0, os.getcwd()) # Need to add the current directory to path
431
- imported_module = importlib.import_module(mod)
432
- f = getattr(imported_module, func)
433
-
434
- return f
435
-
436
- @classmethod
437
- def parse_from_config(cls, config: Dict[str, Any]) -> "MapNode":
438
- internal_name = cast(str, config.get("internal_name"))
439
-
440
- config_branch = config.pop("branch", {})
441
- if not config_branch:
442
- raise Exception("A map node should have a branch")
443
-
444
- branch = create_graph(
445
- deepcopy(config_branch),
446
- internal_branch_name=internal_name + "." + defaults.MAP_PLACEHOLDER,
447
- )
448
- return cls(branch=branch, **config)
449
-
450
- @property
451
- def branch_returns(self):
452
- branch_returns: List[
453
- Tuple[str, Union[ObjectParameter, MetricParameter, JsonParameter]]
454
- ] = []
455
- for _, node in self.branch.nodes.items():
456
- if isinstance(node, TaskNode):
457
- for task_return in node.executable.returns:
458
- if task_return.kind == "json":
459
- branch_returns.append(
460
- (
461
- task_return.name,
462
- JsonParameter(kind="json", value="", reduced=False),
463
- )
464
- )
465
- elif task_return.kind == "object":
466
- branch_returns.append(
467
- (
468
- task_return.name,
469
- ObjectParameter(
470
- kind="object",
471
- value="Will be reduced",
472
- reduced=False,
473
- ),
474
- )
475
- )
476
- elif task_return.kind == "metric":
477
- branch_returns.append(
478
- (
479
- task_return.name,
480
- MetricParameter(kind="metric", value="", reduced=False),
481
- )
482
- )
483
- else:
484
- raise Exception("kind should be either json or object")
485
-
486
- return branch_returns
487
-
488
- def _get_branch_by_name(self, branch_name: str) -> Graph:
489
- """
490
- Retrieve a branch by name.
491
-
492
- In the case of a Map Object, the branch naming is dynamic as it is parameterized on iterable.
493
- This method takes no responsibility in checking the validity of the naming.
494
-
495
- Returns a Graph Object
496
-
497
- Args:
498
- branch_name (str): The name of the branch to retrieve
499
-
500
- Raises:
501
- Exception: If the branch by that name does not exist
502
- """
503
- return self.branch
504
-
505
- def fan_out(self, map_variable: TypeMapVariable = None):
506
- """
507
- The general method to fan out for a node of type map.
508
- This method assumes that the step log has already been created.
509
-
510
- 3rd party orchestrators should call this method to create the individual branch logs.
511
-
512
- Args:
513
- executor (BaseExecutor): The executor class as defined by the config
514
- map_variable (dict, optional): If the node is part of map. Defaults to None.
515
- """
516
- iterate_on = self._context.run_log_store.get_parameters(self._context.run_id)[
517
- self.iterate_on
518
- ].get_value()
519
-
520
- # Prepare the branch logs
521
- for iter_variable in iterate_on:
522
- effective_branch_name = self._resolve_map_placeholders(
523
- self.internal_name + "." + str(iter_variable), map_variable=map_variable
524
- )
525
- branch_log = self._context.run_log_store.create_branch_log(
526
- effective_branch_name
527
- )
528
-
529
- console.print(
530
- f"Branch log created for {effective_branch_name}: {branch_log}"
531
- )
532
- branch_log.status = defaults.PROCESSING
533
- self._context.run_log_store.add_branch_log(branch_log, self._context.run_id)
534
-
535
- # Gather all the returns of the task nodes and create parameters in reduced=False state.
536
- # TODO: Why are we preemptively creating the parameters?
537
- raw_parameters = {}
538
- if map_variable:
539
- # If we are in a map state already, the param should have an index of the map variable.
540
- for _, v in map_variable.items():
541
- for branch_return in self.branch_returns:
542
- param_name, param_type = branch_return
543
- raw_parameters[f"{v}_{param_name}"] = param_type.copy()
544
- else:
545
- for branch_return in self.branch_returns:
546
- param_name, param_type = branch_return
547
- raw_parameters[f"{param_name}"] = param_type.copy()
548
-
549
- self._context.run_log_store.set_parameters(
550
- parameters=raw_parameters, run_id=self._context.run_id
551
- )
552
-
553
- def execute_as_graph(self, map_variable: TypeMapVariable = None):
554
- """
555
- This function does the actual execution of the branch of the map node.
556
-
557
- From a design perspective, this function should not be called if the execution is 3rd party orchestrated.
558
-
559
- The modes that render the job specifications, do not need to interact with this node at all as
560
- they have their own internal mechanisms of handing map states or dynamic parallel states.
561
- If they do not, you can find a way using as-is nodes as hack nodes.
562
-
563
- The actual logic is :
564
- * We iterate over the iterable as mentioned in the config
565
- * For every value in the iterable we call the executor.execute_graph(branch, iterate_as: iter_variable)
566
-
567
- The execution of a dag, could result in
568
- * The dag being completely executed with a definite (fail, success) state in case of local
569
- or local-container execution
570
- * The dag being in a processing state with PROCESSING status in case of local-aws-batch
571
-
572
- Only fail state is considered failure during this phase of execution.
573
-
574
- Args:
575
- executor (Executor): The Executor as per the use config
576
- map_variable (dict): The map variables the graph belongs to
577
- **kwargs: Optional kwargs passed around
578
- """
579
-
580
- iterate_on = None
581
- try:
582
- iterate_on = self._context.run_log_store.get_parameters(
583
- self._context.run_id
584
- )[self.iterate_on].get_value()
585
- except KeyError as e:
586
- raise Exception(
587
- (
588
- f"Expected parameter {self.iterate_on}",
589
- "not present in Run Log parameters",
590
- "was it ever set before?",
591
- )
592
- ) from e
593
-
594
- if not isinstance(iterate_on, list):
595
- raise Exception("Only list is allowed as a valid iterator type")
596
-
597
- self.fan_out(map_variable=map_variable)
598
-
599
- for iter_variable in iterate_on:
600
- effective_map_variable = map_variable or OrderedDict()
601
- effective_map_variable[self.iterate_as] = iter_variable
602
-
603
- self._context.executor.execute_graph(
604
- self.branch, map_variable=effective_map_variable
605
- )
606
-
607
- self.fan_in(map_variable=map_variable)
608
-
609
- def fan_in(self, map_variable: TypeMapVariable = None):
610
- """
611
- The general method to fan in for a node of type map.
612
-
613
- 3rd party orchestrators should call this method to find the status of the step log.
614
-
615
- Args:
616
- executor (BaseExecutor): The executor class as defined by the config
617
- map_variable (dict, optional): If the node is part of map node. Defaults to None.
618
- """
619
- params = self._context.run_log_store.get_parameters(self._context.run_id)
620
- iterate_on = params[self.iterate_on].get_value()
621
- # # Find status of the branches
622
- step_success_bool = True
623
- effective_internal_name = self._resolve_map_placeholders(
624
- self.internal_name, map_variable=map_variable
625
- )
626
-
627
- for iter_variable in iterate_on:
628
- effective_branch_name = self._resolve_map_placeholders(
629
- self.internal_name + "." + str(iter_variable), map_variable=map_variable
630
- )
631
- branch_log = self._context.run_log_store.get_branch_log(
632
- effective_branch_name, self._context.run_id
633
- )
634
- # console.print(f"Branch log for {effective_branch_name}: {branch_log}")
635
-
636
- if branch_log.status != defaults.SUCCESS:
637
- step_success_bool = False
638
-
639
- # Collate all the results and update the status of the step
640
- step_log = self._context.run_log_store.get_step_log(
641
- effective_internal_name, self._context.run_id
642
- )
643
-
644
- if step_success_bool: #  If none failed and nothing is waiting
645
- step_log.status = defaults.SUCCESS
646
- else:
647
- step_log.status = defaults.FAIL
648
-
649
- self._context.run_log_store.add_step_log(step_log, self._context.run_id)
650
-
651
- # If we failed, we return without any collection
652
- if not step_log.status == defaults.SUCCESS:
653
- return
654
-
655
- # Apply the reduce function and reduce the returns of the task nodes.
656
- # The final value of the parameter is the result of the reduce function.
657
- reducer_f = self.get_reducer_function()
658
-
659
- def update_param(
660
- params: Dict[str, Parameter], reducer_f: Callable, map_prefix: str = ""
661
- ):
662
- for branch_return in self.branch_returns:
663
- param_name, _ = branch_return
664
-
665
- to_reduce = []
666
- for iter_variable in iterate_on:
667
- try:
668
- to_reduce.append(
669
- params[f"{iter_variable}_{param_name}"].get_value()
670
- )
671
- except KeyError as e:
672
- from extensions.pipeline_executor.mocked import MockedExecutor
673
-
674
- if isinstance(self._context.executor, MockedExecutor):
675
- pass
676
- else:
677
- raise Exception(
678
- (
679
- f"Expected parameter {iter_variable}_{param_name}",
680
- "not present in Run Log parameters",
681
- "was it ever set before?",
682
- )
683
- ) from e
684
-
685
- param_name = f"{map_prefix}{param_name}"
686
- if to_reduce:
687
- params[param_name].value = reducer_f(*to_reduce)
688
- else:
689
- params[param_name].value = ""
690
- params[param_name].reduced = True
691
-
692
- if map_variable:
693
- # If we are in a map state already, the param should have an index of the map variable.
694
- for _, v in map_variable.items():
695
- update_param(params, reducer_f, map_prefix=f"{v}_")
696
- else:
697
- update_param(params, reducer_f)
698
-
699
- self._context.run_log_store.set_parameters(
700
- parameters=params, run_id=self._context.run_id
701
- )
702
-
703
-
704
- class StubNode(ExecutableNode):
705
- """
706
- Stub is a convenience design node.
707
- It always returns success in the attempt log and does nothing.
708
-
709
- This node is very similar to pass state in Step functions.
710
-
711
- This node type could be handy when designing the pipeline and stubbing functions
712
- --8<-- [start:stub_reference]
713
- An stub execution node of the pipeline.
714
- Please refer to define pipeline/tasks/stub for more information.
715
-
716
- As part of the dag definition, a stub task is defined as follows:
717
-
718
- dag:
719
- steps:
720
- stub_task: # The name of the node
721
- type: stub
722
- on_failure: The name of the step to traverse in case of failure
723
- next: The next node to execute after this task, use "success" to terminate the pipeline successfully
724
- or "fail" to terminate the pipeline with an error.
725
-
726
- It can take arbritary number of parameters, which is handy to temporarily silence a task node.
727
- --8<-- [end:stub_reference]
728
- """
729
-
730
- node_type: str = Field(default="stub", serialization_alias="type")
731
- model_config = ConfigDict(extra="ignore")
732
-
733
- def get_summary(self) -> Dict[str, Any]:
734
- summary = {
735
- "name": self.name,
736
- "type": self.node_type,
737
- }
738
-
739
- return summary
740
-
741
- @classmethod
742
- def parse_from_config(cls, config: Dict[str, Any]) -> "StubNode":
743
- return cls(**config)
744
-
745
- def execute(
746
- self,
747
- mock=False,
748
- map_variable: TypeMapVariable = None,
749
- attempt_number: int = 1,
750
- ) -> StepLog:
751
- """
752
- Do Nothing node.
753
- We just send an success attempt log back to the caller
754
-
755
- Args:
756
- executor ([type]): [description]
757
- mock (bool, optional): [description]. Defaults to False.
758
- map_variable (str, optional): [description]. Defaults to ''.
759
-
760
- Returns:
761
- [type]: [description]
762
- """
763
- step_log = self._context.run_log_store.get_step_log(
764
- self._get_step_log_name(map_variable), self._context.run_id
765
- )
766
-
767
- attempt_log = datastore.StepAttempt(
768
- status=defaults.SUCCESS,
769
- start_time=str(datetime.now()),
770
- end_time=str(datetime.now()),
771
- attempt_number=attempt_number,
772
- )
773
-
774
- step_log.status = attempt_log.status
775
-
776
- step_log.attempts.append(attempt_log)
777
-
778
- return step_log