Skip to content

Commit 52b6224

Browse files
committed
feat: add _run_task_with_optional_env function to the Session class
Signed-off-by: Godot Bian <13778003+godobyte@users.noreply.github.com>
1 parent 5fc31f0 commit 52b6224

2 files changed

Lines changed: 609 additions & 0 deletions

File tree

src/openjd/sessions/_session.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,85 @@ def run_task(
843843
# than after -- run() itself may end up setting the action state to FAILED.
844844
self._runner.run()
845845

846+
def _run_task_with_optional_env(
847+
self,
848+
*,
849+
step_script: StepScriptModel,
850+
task_parameter_values: TaskParameterSet,
851+
os_env_vars: Optional[dict[str, str]] = None,
852+
use_session_env_vars: Optional[bool] = True,
853+
log_task_banner: bool = True,
854+
) -> None:
855+
"""Run a Task within the Session.
856+
This method is non-blocking; it will exit when the subprocess is either confirmed to have
857+
started running, or has failed to be started.
858+
859+
Arguments:
860+
step_script (StepScriptModel): The Step Script that the Task will be running.
861+
task_parameter_values (TaskParameterSet): Values of the Task parameters that define the
862+
specific Task. This is a dictionary where the keys are parameter names, and the values
863+
are instances of ParameterValue (a dataclass containing the type and value of the parameter)
864+
os_env_vars (Optional[dict[str,str]): Definitions for additional OS Environment
865+
Variables that should be injected into the process that is run for this action.
866+
Values provided override values provided to the Session constructor, and are overriden
867+
by values defined in Environments.
868+
Key: Environment variable name
869+
Value: Value for the environment variable.
870+
apply_existing_env (Optional[bool]): Whether to apply environment variable changes from
871+
entered environments. When True (default), applies changes from all entered environments
872+
in order. When False, only uses base session and os_env_vars.
873+
log_task_banner (bool): Whether to log a banner before running the Task.
874+
Default: True
875+
"""
876+
if self.state != SessionState.READY:
877+
raise RuntimeError("Session must be in the READY state to run a task.")
878+
879+
if log_task_banner:
880+
log_section_banner(self._logger, "Running Task")
881+
882+
if task_parameter_values:
883+
self._logger.info(
884+
"Parameter values:",
885+
extra=LogExtraInfo(openjd_log_content=LogContent.PARAMETER_INFO),
886+
)
887+
for name, value in task_parameter_values.items():
888+
self._logger.info(
889+
f"{name}({str(value.type.value)}) = {value.value}",
890+
extra=LogExtraInfo(openjd_log_content=LogContent.PARAMETER_INFO),
891+
)
892+
893+
self._reset_action_state()
894+
symtab = self._symbol_table(step_script.revision, task_parameter_values)
895+
896+
# Evaluate environment variables
897+
if use_session_env_vars:
898+
action_env_vars = self._evaluate_current_session_env_vars(os_env_vars)
899+
else:
900+
action_env_vars = dict[str, Optional[str]](self._process_env) # Make a copy
901+
if os_env_vars:
902+
action_env_vars.update(**os_env_vars)
903+
904+
self._materialize_path_mapping(step_script.revision, action_env_vars, symtab)
905+
self._runner = StepScriptRunner(
906+
logger=self._logger,
907+
user=self._user,
908+
os_env_vars=action_env_vars,
909+
session_working_directory=self.working_directory,
910+
startup_directory=self.working_directory,
911+
callback=self._action_callback,
912+
script=step_script,
913+
symtab=symtab,
914+
session_files_directory=self.files_directory,
915+
)
916+
# Sets the subprocess running.
917+
# Returns immediately after it has started, or is running
918+
self._action_state = ActionState.RUNNING
919+
self._state = SessionState.RUNNING
920+
# Note: This may fail immediately (e.g. if we cannot write embedded files to disk),
921+
# so it's important to set the action_state to RUNNING before calling run(), rather
922+
# than after -- run() itself may end up setting the action state to FAILED.
923+
self._runner.run()
924+
846925
# =========================
847926
# Helpers
848927

0 commit comments

Comments
 (0)