-
Notifications
You must be signed in to change notification settings - Fork 232
Merge wait and timeout to one CLI option for verdi process {kill|play|pause}
#6902
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
Merged
Merged
Changes from 3 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
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 |
|---|---|---|
|
|
@@ -104,7 +104,7 @@ | |
|
|
||
|
|
||
| def play_processes( | ||
| processes: list[ProcessNode] | None = None, *, all_entries: bool = False, timeout: float = 5.0, wait: bool = False | ||
| processes: list[ProcessNode] | None = None, *, all_entries: bool = False, timeout: float = 5.0 | ||
| ) -> None: | ||
| """Play (unpause) paused processes. | ||
|
|
@@ -113,7 +113,6 @@ | |
| :param processes: List of processes to play. | ||
| :param all_entries: Play all paused processes. | ||
| :param timeout: Raise a ``ProcessTimeoutException`` if the process does not respond within this amount of seconds. | ||
| :param wait: Set to ``True`` to wait for process response, for ``False`` the action is fire-and-forget. | ||
| :raises ``ProcessTimeoutException``: If the processes do not respond within the timeout. | ||
| """ | ||
| if not get_daemon_client().is_daemon_running: | ||
|
|
@@ -130,7 +129,7 @@ | |
| return | ||
|
|
||
| controller = get_manager().get_process_controller() | ||
| _perform_actions(processes, controller.play_process, 'play', 'playing', timeout, wait) | ||
| _perform_actions(processes, controller.play_process, 'play', 'playing', timeout) | ||
|
|
||
|
|
||
| def pause_processes( | ||
|
|
@@ -139,7 +138,6 @@ | |
| msg_text: str = 'Paused through `aiida.engine.processes.control.pause_processes`', | ||
| all_entries: bool = False, | ||
| timeout: float = 5.0, | ||
| wait: bool = False, | ||
| ) -> None: | ||
| """Pause running processes. | ||
|
|
@@ -148,7 +146,6 @@ | |
| :param processes: List of processes to play. | ||
| :param all_entries: Pause all playing processes. | ||
| :param timeout: Raise a ``ProcessTimeoutException`` if the process does not respond within this amount of seconds. | ||
| :param wait: Set to ``True`` to wait for process response, for ``False`` the action is fire-and-forget. | ||
| :raises ``ProcessTimeoutException``: If the processes do not respond within the timeout. | ||
| """ | ||
| if not get_daemon_client().is_daemon_running: | ||
|
|
@@ -166,7 +163,7 @@ | |
|
|
||
| controller = get_manager().get_process_controller() | ||
| action = functools.partial(controller.pause_process, msg_text=msg_text) | ||
| _perform_actions(processes, action, 'pause', 'pausing', timeout, wait) | ||
| _perform_actions(processes, action, 'pause', 'pausing', timeout) | ||
|
|
||
|
|
||
| def kill_processes( | ||
|
|
@@ -176,7 +173,6 @@ | |
| force: bool = False, | ||
| all_entries: bool = False, | ||
| timeout: float = 5.0, | ||
| wait: bool = False, | ||
| ) -> None: | ||
| """Kill running processes. | ||
|
|
@@ -185,7 +181,6 @@ | |
| :param processes: List of processes to play. | ||
| :param all_entries: Kill all active processes. | ||
| :param timeout: Raise a ``ProcessTimeoutException`` if the process does not respond within this amount of seconds. | ||
| :param wait: Set to ``True`` to wait for process response, for ``False`` the action is fire-and-forget. | ||
| :raises ``ProcessTimeoutException``: If the processes do not respond within the timeout. | ||
| """ | ||
| if not get_daemon_client().is_daemon_running: | ||
|
|
@@ -203,7 +198,7 @@ | |
|
|
||
| controller = get_manager().get_process_controller() | ||
| action = functools.partial(controller.kill_process, msg_text=msg_text, force_kill=force) | ||
| _perform_actions(processes, action, 'kill', 'killing', timeout, wait) | ||
| _perform_actions(processes, action, 'kill', 'killing', timeout) | ||
|
|
||
|
|
||
| def _perform_actions( | ||
|
|
@@ -212,7 +207,6 @@ | |
| infinitive: str, | ||
| present: str, | ||
| timeout: t.Optional[float] = None, | ||
| wait: bool = False, | ||
| **kwargs: t.Any, | ||
| ) -> None: | ||
| """Perform an action on a list of processes. | ||
|
|
@@ -223,7 +217,6 @@ | |
| :param present: The present tense of the verb that represents the action. | ||
| :param past: The past tense of the verb that represents the action. | ||
| :param timeout: Raise a ``ProcessTimeoutException`` if the process does not respond within this amount of seconds. | ||
| :param wait: Set to ``True`` to wait for process response, for ``False`` the action is fire-and-forget. | ||
| :param kwargs: Keyword arguments that will be passed to the method ``action``. | ||
| :raises ``ProcessTimeoutException``: If the processes do not respond within the timeout. | ||
| """ | ||
|
|
@@ -236,86 +229,70 @@ | |
|
|
||
| try: | ||
| future = action(process.pk, **kwargs) | ||
| LOGGER.report(f'Request to {infinitive} Process<{process.pk}> sent.') | ||
| except communications.UnroutableError: | ||
| LOGGER.error(f'Process<{process.pk}> is unreachable.') | ||
| else: | ||
| futures[future] = process | ||
|
|
||
| _resolve_futures(futures, infinitive, present, wait, timeout) | ||
| _resolve_futures(futures, infinitive, present, timeout) | ||
|
|
||
|
|
||
| def _resolve_futures( | ||
| futures: dict[concurrent.futures.Future, ProcessNode], | ||
| infinitive: str, | ||
| present: str, | ||
| wait: bool = False, | ||
| timeout: t.Optional[float] = None, | ||
| ) -> None: | ||
| """Process a mapping of futures representing an action on an active process. | ||
| This function will echo the correct information strings based on the outcomes of the futures and the given verb | ||
| conjugations. You can optionally wait for any pending actions to be completed before the functions returns and use a | ||
| timeout to put a maximum wait time on the actions. | ||
| timeout to put a maximum wait time on the actions. TODO fix docstring | ||
|
||
| :param futures: The map of action futures and the corresponding processes. | ||
| :param infinitive: The infinitive form of the action verb. | ||
| :param present: The present tense form of the action verb. | ||
| :param wait: Set to ``True`` to wait for process response, for ``False`` the action is fire-and-forget. | ||
| :param timeout: Raise a ``ProcessTimeoutException`` if the process does not respond within this amount of seconds. | ||
| """ | ||
| scheduled = {} | ||
|
|
||
| def handle_result(result): | ||
| if result is True: | ||
| LOGGER.report(f'request to {infinitive} Process<{process.pk}> sent') | ||
| elif result is False: | ||
| LOGGER.error(f'problem {present} Process<{process.pk}>') | ||
| elif isinstance(result, kiwipy.Future): | ||
| LOGGER.report(f'scheduled {infinitive} Process<{process.pk}>') | ||
| scheduled[result] = process | ||
| else: | ||
| LOGGER.error(f'got unexpected response when {present} Process<{process.pk}>: {result}') | ||
|
|
||
| try: | ||
| for future, process in futures.items(): | ||
| # unwrap is need here since LoopCommunicator will also wrap a future | ||
| unwrapped = unwrap_kiwi_future(future) | ||
| try: | ||
| result = unwrapped.result(timeout=timeout) | ||
| except communications.TimeoutError: | ||
| cancelled = unwrapped.cancel() | ||
| if cancelled: | ||
| LOGGER.error(f'call to {infinitive} Process<{process.pk}> timed out and was cancelled.') | ||
| else: | ||
| LOGGER.error(f'call to {infinitive} Process<{process.pk}> timed out but could not be cancelled.') | ||
| except Exception as exception: | ||
| LOGGER.error(f'failed to {infinitive} Process<{process.pk}>: {exception}') | ||
| else: | ||
| if isinstance(result, kiwipy.Future): | ||
| LOGGER.report(f'scheduled {infinitive} Process<{process.pk}>') | ||
| scheduled[result] = process | ||
| else: | ||
| handle_result(result) | ||
|
|
||
| if not wait or not scheduled: | ||
| return | ||
|
|
||
| LOGGER.report(f"waiting for process(es) {','.join([str(proc.pk) for proc in scheduled.values()])}") | ||
| if not timeout or not futures: | ||
| if futures: | ||
| LOGGER.report( | ||
| f"Request to {infinitive} process(es) {','.join([str(proc.pk) for proc in futures.values()])}" | ||
| ' sent. Skipping waiting for response.' | ||
| ) | ||
| return | ||
|
|
||
| for future in concurrent.futures.as_completed(scheduled.keys(), timeout=timeout): | ||
| process = scheduled[future] | ||
| LOGGER.report(f"Waiting for process(es) {','.join([str(proc.pk) for proc in futures.values()])}.") | ||
|
|
||
| # Ensure that when futures are only are completed if they return an actual value (not a future) | ||
| unwrapped_futures = {unwrap_kiwi_future(future): process for future, process in futures.items()} | ||
| try: | ||
| # future does not interpret float('inf') correctly by changing it to None we get the intended behavior | ||
| for future in concurrent.futures.as_completed( | ||
| unwrapped_futures.keys(), timeout=None if timeout == float('inf') else timeout | ||
| ): | ||
| process = unwrapped_futures[future] | ||
| try: | ||
| result = future.result() | ||
| except Exception as exception: | ||
| LOGGER.error(f'failed to {infinitive} Process<{process.pk}>: {exception}') | ||
| LOGGER.error(f'Failed to {infinitive} Process<{process.pk}>: {exception}') | ||
| else: | ||
| handle_result(result) | ||
|
|
||
| if result is True: | ||
| LOGGER.report(f'Request to {infinitive} Process<{process.pk}> processed.') | ||
| elif result is False: | ||
| LOGGER.error(f'Problem {present} Process<{process.pk}>') | ||
| else: | ||
| LOGGER.error(f'Got unexpected response when {present} Process<{process.pk}>: {result}') | ||
| except concurrent.futures.TimeoutError: | ||
| raise ProcessTimeoutException( | ||
| f'timed out trying to {infinitive} processes {futures.values()}\n' | ||
| 'This could be because the daemon workers are too busy to respond, please try again later.\n' | ||
| 'If the problem persists, make sure the daemon and RabbitMQ are running properly by restarting them.\n' | ||
| 'If the processes remain unresponsive, as a last resort, try reviving them with ``revive_processes``.' | ||
| ) | ||
| # We cancel the tasks that are not done | ||
| undone_futures = {future: process for future, process in unwrapped_futures.items() if not future.done()} | ||
| if not undone_futures: | ||
| LOGGER.error(f'Call to {infinitive} timed out but already done.') | ||
| for future, process in undone_futures.items(): | ||
| if not future.done(): | ||
| cancelled = future.cancel() | ||
| if cancelled: | ||
| LOGGER.error(f'Call to {infinitive} Process<{process.pk}> timed out and was cancelled.') | ||
| else: | ||
| LOGGER.error(f'Call to {infinitive} Process<{process.pk}> timed out but could not be cancelled.') | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't forget it