Skip to content

Add async ngen execution via new context manager class ForecastExecutionManager - #1

Merged
mxkpp merged 8 commits into
developmentfrom
maxkipp-ngwpc-9004-workflow-automation
Dec 3, 2025
Merged

Add async ngen execution via new context manager class ForecastExecutionManager#1
mxkpp merged 8 commits into
developmentfrom
maxkipp-ngwpc-9004-workflow-automation

Conversation

@mxkpp

@mxkpp mxkpp commented Nov 25, 2025

Copy link
Copy Markdown

This PR adds a new context manager class ForecastExecutionManager with primary methods preprocess, execute, and postprocess, which allows asynchronous subprocess calls to the ngen binary, external polling of the ngen subprocess, and external stopping of the ngen subprocess.

On exit, the context manager closes the log file and terminates ngen if it is still running. A wait parameter is provided on the execute method for cases where external polling is not desired, in which case the ForecastExecutionManager performs its own polling.

Function run_fcst still exists, and it uses the new class by calling execute(wait=True) such that its user interface is effectively unchanged.

To stop ngen execution, the user calls method schedule_ngen_stoppage().

See here for example of async user call (wait=False) where the user intentionally quits execution after certain messages are found in the ngen log file: https://github.com/NGWPC/nwm-rte/blob/64f79860170c04983916be898ebddd5cac02247d/bin_mounted/example_workflow.py#L245-L250

@mxkpp

mxkpp commented Nov 25, 2025

Copy link
Copy Markdown
Author

Note that the new class changes the type of exception raised during ngen execution. Previously it would raise a generic CalledProcessError. Now it will raise a NgenCalledProcessError, or a NgenIntentionallyStoppedError situationally.

@jswade-rtx jswade-rtx left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look good.

@mxkpp

mxkpp commented Nov 25, 2025

Copy link
Copy Markdown
Author

@peterkronenberg-rtx, these changes should not cause a change in default/existing behavior: when a forecast is executed via call to run_fcst().

If CERF leverages that function, could you please confirm that behavior is unchanged from CERF usage perspective?

@peterkronenberg-rtx peterkronenberg-rtx left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully ran a forecast from ngenCerf. No server changes were needed. Everything worked fine

…xit, move closure logic to new public method close() to allow clean closure outside of context manager interface.
@mxkpp

mxkpp commented Nov 29, 2025

Copy link
Copy Markdown
Author

The 2 commits I added earlier today were to support a testing environment, which needed:

  1. To be interruptible via ctrl+C (KeyboardInterrupt)
  2. A new log file per call, since it scans the log file for errors.

@mxkpp
mxkpp merged commit 5ed17a7 into development Dec 3, 2025
9 checks passed
@mxkpp
mxkpp deleted the maxkipp-ngwpc-9004-workflow-automation branch December 3, 2025 15:42
cmaynard-ngwpc pushed a commit that referenced this pull request Dec 5, 2025
Add async ngen execution via new context manager class `ForecastExecutionManager`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants