-
Notifications
You must be signed in to change notification settings - Fork 20
update task_resume_workflows to also resume processes in CREATED/RESUMED status #984
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tjeerddie
wants to merge
4
commits into
main
Choose a base branch
from
898-improve-worker-process
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
# Copyright 2019-2025 SURF, GÉANT, ESnet. | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
from collections.abc import Callable | ||
from functools import partial | ||
from uuid import UUID | ||
|
||
import structlog | ||
|
||
from oauth2_lib.fastapi import OIDCUserModel | ||
from orchestrator.db import ProcessTable, db | ||
from orchestrator.services.input_state import retrieve_input_state | ||
from orchestrator.services.processes import ( | ||
SYSTEM_USER, | ||
StateMerger, | ||
_get_process, | ||
_run_process_async, | ||
create_process, | ||
load_process, | ||
safe_logstep, | ||
) | ||
from orchestrator.types import BroadcastFunc | ||
from orchestrator.workflow import ( | ||
ProcessStat, | ||
ProcessStatus, | ||
runwf, | ||
) | ||
from orchestrator.workflows.removed_workflow import removed_workflow | ||
from pydantic_forms.types import State | ||
|
||
logger = structlog.get_logger(__name__) | ||
|
||
|
||
def thread_start_process( | ||
pstat: ProcessStat, | ||
user: str = SYSTEM_USER, | ||
user_model: OIDCUserModel | None = None, | ||
broadcast_func: BroadcastFunc | None = None, | ||
) -> UUID: | ||
if pstat.workflow == removed_workflow: | ||
raise ValueError("This workflow cannot be started") | ||
tjeerddie marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
process = _get_process(pstat.process_id) | ||
process.last_status = ProcessStatus.RUNNING | ||
db.session.add(process) | ||
db.session.commit() | ||
|
||
pstat = load_process(process) | ||
input_data = retrieve_input_state(process.process_id, "initial_state") | ||
pstat.update(state=pstat.state.map(lambda state: StateMerger.merge(state, input_data.input_state))) | ||
|
||
_safe_logstep_with_func = partial(safe_logstep, broadcast_func=broadcast_func) | ||
return _run_process_async(pstat.process_id, lambda: runwf(pstat, _safe_logstep_with_func)) | ||
|
||
|
||
def thread_resume_process( | ||
process: ProcessTable, | ||
*, | ||
user: str | None = None, | ||
user_model: OIDCUserModel | None = None, | ||
broadcast_func: BroadcastFunc | None = None, | ||
) -> UUID: | ||
# ATTENTION!! When modifying this function make sure you make similar changes to `resume_workflow` in the test code | ||
pstat = load_process(process) | ||
if pstat.workflow == removed_workflow: | ||
raise ValueError("This workflow cannot be resumed because it has been removed") | ||
|
||
if user: | ||
pstat.update(current_user=user) | ||
|
||
input_data = retrieve_input_state(process.process_id, "user_input") | ||
pstat.update(state=pstat.state.map(lambda state: StateMerger.merge(state, input_data.input_state))) | ||
|
||
# enforce an update to the process status to properly show the process | ||
process.last_status = ProcessStatus.RUNNING | ||
db.session.add(process) | ||
db.session.commit() | ||
|
||
_safe_logstep_prep = partial(safe_logstep, broadcast_func=broadcast_func) | ||
_run_process_async(pstat.process_id, lambda: runwf(pstat, _safe_logstep_prep)) | ||
return pstat.process_id | ||
|
||
|
||
def thread_validate_workflow(validation_workflow: str, json: list[State] | None) -> UUID: | ||
pstat = create_process(validation_workflow, user_inputs=json) | ||
return thread_start_process(pstat) | ||
|
||
|
||
THREADPOOL_EXECUTION_CONTEXT: dict[str, Callable] = { | ||
"start": thread_start_process, | ||
"resume": thread_resume_process, | ||
"validate": thread_validate_workflow, | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,7 +49,6 @@ | |
Success, | ||
Workflow, | ||
abort_wf, | ||
runwf, | ||
) | ||
from orchestrator.workflow import Process as WFProcess | ||
from orchestrator.workflows import get_workflow | ||
|
@@ -69,10 +68,12 @@ | |
|
||
def get_execution_context() -> dict[str, Callable]: | ||
if app_settings.EXECUTOR == ExecutorType.WORKER: | ||
from orchestrator.services.celery import CELERY_EXECUTION_CONTEXT | ||
from orchestrator.services.executors.celery import CELERY_EXECUTION_CONTEXT | ||
|
||
return CELERY_EXECUTION_CONTEXT | ||
|
||
from orchestrator.services.executors.threadpool import THREADPOOL_EXECUTION_CONTEXT | ||
|
||
return THREADPOOL_EXECUTION_CONTEXT | ||
|
||
|
||
|
@@ -440,7 +441,6 @@ def create_process( | |
} | ||
|
||
try: | ||
|
||
state = post_form(workflow.initial_input_form, initial_state, user_inputs) | ||
except FormValidationError: | ||
logger.exception("Validation errors", user_inputs=user_inputs) | ||
|
@@ -460,19 +460,6 @@ def create_process( | |
return pstat | ||
|
||
|
||
def thread_start_process( | ||
tjeerddie marked this conversation as resolved.
Show resolved
Hide resolved
|
||
workflow_key: str, | ||
user_inputs: list[State] | None = None, | ||
user: str = SYSTEM_USER, | ||
user_model: OIDCUserModel | None = None, | ||
broadcast_func: BroadcastFunc | None = None, | ||
) -> UUID: | ||
pstat = create_process(workflow_key, user_inputs=user_inputs, user=user, user_model=user_model) | ||
|
||
_safe_logstep_with_func = partial(safe_logstep, broadcast_func=broadcast_func) | ||
return _run_process_async(pstat.process_id, lambda: runwf(pstat, _safe_logstep_with_func)) | ||
|
||
|
||
def start_process( | ||
workflow_key: str, | ||
user_inputs: list[State] | None = None, | ||
|
@@ -493,57 +480,33 @@ def start_process( | |
process id | ||
|
||
""" | ||
pstat = create_process(workflow_key, user_inputs=user_inputs, user=user) | ||
|
||
start_func = get_execution_context()["start"] | ||
return start_func( | ||
workflow_key, user_inputs=user_inputs, user=user, user_model=user_model, broadcast_func=broadcast_func | ||
) | ||
return start_func(pstat, user=user, user_model=user_model, broadcast_func=broadcast_func) | ||
|
||
|
||
def thread_resume_process( | ||
def restart_process( | ||
process: ProcessTable, | ||
*, | ||
user_inputs: list[State] | None = None, | ||
user: str | None = None, | ||
broadcast_func: BroadcastFunc | None = None, | ||
) -> UUID: | ||
# ATTENTION!! When modifying this function make sure you make similar changes to `resume_workflow` in the test code | ||
|
||
if user_inputs is None: | ||
user_inputs = [{}] | ||
|
||
pstat = load_process(process) | ||
|
||
if pstat.workflow == removed_workflow: | ||
raise ValueError("This workflow cannot be resumed") | ||
|
||
form = pstat.log[0].form | ||
|
||
user_input = post_form(form, pstat.state.unwrap(), user_inputs) | ||
|
||
if user: | ||
pstat.update(current_user=user) | ||
|
||
if user_input: | ||
pstat.update(state=pstat.state.map(lambda state: StateMerger.merge(state, user_input))) | ||
store_input_state(pstat.process_id, user_input, "user_input") | ||
# enforce an update to the process status to properly show the process | ||
process.last_status = ProcessStatus.RUNNING | ||
db.session.add(process) | ||
db.session.commit() | ||
|
||
_safe_logstep_prep = partial(safe_logstep, broadcast_func=broadcast_func) | ||
return _run_process_async(pstat.process_id, lambda: runwf(pstat, _safe_logstep_prep)) | ||
"""Start a process for workflow. | ||
|
||
Args: | ||
process: Process from database | ||
user: user who resumed this process | ||
broadcast_func: Optional function to broadcast process data | ||
|
||
def thread_validate_workflow(validation_workflow: str, json: list[State] | None) -> UUID: | ||
return thread_start_process(validation_workflow, user_inputs=json) | ||
Returns: | ||
process id | ||
|
||
""" | ||
pstat = load_process(process) | ||
|
||
THREADPOOL_EXECUTION_CONTEXT: dict[str, Callable] = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe the PR should mention that these dicts were moved. And that the signature of the functions has changed. Just in case someone is using them directly |
||
"start": thread_start_process, | ||
"resume": thread_resume_process, | ||
"validate": thread_validate_workflow, | ||
} | ||
start_func = get_execution_context()["start"] | ||
return start_func(pstat, user=user, broadcast_func=broadcast_func) | ||
|
||
|
||
def resume_process( | ||
|
@@ -552,7 +515,7 @@ def resume_process( | |
user_inputs: list[State] | None = None, | ||
user: str | None = None, | ||
broadcast_func: BroadcastFunc | None = None, | ||
) -> UUID: | ||
) -> bool: | ||
"""Resume a failed or suspended process. | ||
|
||
Args: | ||
|
@@ -567,14 +530,19 @@ def resume_process( | |
""" | ||
pstat = load_process(process) | ||
|
||
if pstat.workflow == removed_workflow: | ||
raise ValueError("This workflow cannot be resumed because it has been removed") | ||
|
||
try: | ||
post_form(pstat.log[0].form, pstat.state.unwrap(), user_inputs=user_inputs or []) | ||
user_input = post_form(pstat.log[0].form, pstat.state.unwrap(), user_inputs=user_inputs or [{}]) | ||
except FormValidationError: | ||
logger.exception("Validation errors", user_inputs=user_inputs) | ||
raise | ||
|
||
store_input_state(pstat.process_id, user_input, "user_input") | ||
|
||
resume_func = get_execution_context()["resume"] | ||
return resume_func(process, user_inputs=user_inputs, user=user, broadcast_func=broadcast_func) | ||
return resume_func(process, user=user, broadcast_func=broadcast_func) | ||
|
||
|
||
def ensure_correct_callback_token(pstat: ProcessStat, *, token: str) -> None: | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.