@@ -75,6 +75,14 @@ def _jsonify_cli_context(ctx: click.core.Context) -> Dict[str, Any]:
75
75
}
76
76
77
77
78
+ class KedroSessionError (Exception ):
79
+ """``KedroSessionError`` raised by ``KedroSession``
80
+ in case of run failure as part of a session.
81
+ """
82
+
83
+ pass
84
+
85
+
78
86
class KedroSession :
79
87
"""``KedroSession`` is the object that is responsible for managing the lifecycle
80
88
of a Kedro run.
@@ -94,18 +102,20 @@ class KedroSession:
94
102
>>>
95
103
"""
96
104
97
- def __init__ (
105
+ def __init__ ( # pylint: disable=too-many-arguments
98
106
self ,
99
107
session_id : str ,
100
108
package_name : str = None ,
101
109
project_path : Union [Path , str ] = None ,
102
110
save_on_close : bool = False ,
111
+ run_called : bool = False ,
103
112
):
104
113
self ._project_path = Path (project_path or Path .cwd ()).resolve ()
105
114
self .session_id = session_id
106
115
self .save_on_close = save_on_close
107
116
self ._package_name = package_name
108
117
self ._store = self ._init_store ()
118
+ self ._run_called = run_called
109
119
110
120
hook_manager = _create_hook_manager ()
111
121
_register_hooks (hook_manager , settings .HOOKS )
@@ -318,6 +328,8 @@ def run( # pylint: disable=too-many-arguments,too-many-locals
318
328
defined by `register_pipelines`.
319
329
Exception: Any uncaught exception during the run will be re-raised
320
330
after being passed to ``on_pipeline_error`` hook.
331
+ KedroSessionError: If more than one run is attempted to be executed during
332
+ a single session.
321
333
Returns:
322
334
Any node outputs that cannot be processed by the ``DataCatalog``.
323
335
These are returned in a dictionary, where the keys are defined
@@ -327,6 +339,13 @@ def run( # pylint: disable=too-many-arguments,too-many-locals
327
339
# Report project name
328
340
self ._logger .info ("** Kedro project %s" , self ._project_path .name )
329
341
342
+ if self ._run_called :
343
+ raise KedroSessionError (
344
+ "A run has already been executed as part of the"
345
+ " active KedroSession. KedroSession has a 1-1 mapping with"
346
+ " runs, and thus only one run should be executed per session."
347
+ )
348
+
330
349
save_version = self .store ["session_id" ]
331
350
extra_params = self .store .get ("extra_params" ) or {}
332
351
context = self .load_context ()
@@ -380,6 +399,7 @@ def run( # pylint: disable=too-many-arguments,too-many-locals
380
399
381
400
try :
382
401
run_result = runner .run (filtered_pipeline , catalog , hook_manager )
402
+ self ._run_called = True
383
403
except Exception as error :
384
404
hook_manager .hook .on_pipeline_error (
385
405
error = error ,
0 commit comments