| 1 |
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE. |
| 2 |
# Copyright (C) NIWA & British Crown (Met Office) & Contributors. |
| 3 |
# |
| 4 |
# This program is free software: you can redistribute it and/or modify |
| 5 |
# it under the terms of the GNU General Public License as published by |
| 6 |
# the Free Software Foundation, either version 3 of the License, or |
| 7 |
# (at your option) any later version. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
"""Workflow status constants.""" |
| 17 |
|
| 18 |
from enum import Enum |
| 19 |
from typing import TYPE_CHECKING, Optional, Union |
| 20 |
|
| 21 |
from cylc.flow.cycling.loader import get_point |
| 22 |
from cylc.flow.id import tokenise |
| 23 |
from cylc.flow.wallclock import get_time_string_from_unix_time as time2str |
| 24 |
|
| 25 |
if TYPE_CHECKING: |
| 26 |
from cylc.flow.cycling import PointBase |
| 27 |
from cylc.flow.scheduler import Scheduler |
| 28 |
from cylc.flow.task_pool import TaskPool |
| 29 |
|
| 30 |
# Keys for identify API call |
| 31 |
KEY_GROUP = "group" |
| 32 |
KEY_META = "meta" |
| 33 |
KEY_NAME = "name" |
| 34 |
KEY_OWNER = "owner" |
| 35 |
KEY_STATES = "states" |
| 36 |
KEY_TASKS_BY_STATE = "tasks-by-state" |
| 37 |
KEY_UPDATE_TIME = "update-time" |
| 38 |
KEY_VERSION = "version" |
| 39 |
|
| 40 |
# Status message strings |
| 41 |
WORKFLOW_STATUS_RUNNING_TO_STOP = "running to stop at %s" |
| 42 |
WORKFLOW_STATUS_RUNNING_TO_HOLD = "running to hold at %s" |
| 43 |
|
| 44 |
# TODO - the workflow status should be a property of the Scheduler instance |
| 45 |
# rather than derived from scheduler state? |
| 46 |
|
| 47 |
|
| 48 |
class WorkflowStatus(Enum): |
| 49 |
"""The possible statuses of a workflow.""" |
| 50 |
|
| 51 |
PAUSED = "paused" |
| 52 |
"""Workflow will not submit any new jobs.""" |
| 53 |
|
| 54 |
RUNNING = "running" |
| 55 |
"""Workflow is running as normal.""" |
| 56 |
|
| 57 |
STOPPING = "stopping" |
| 58 |
"""Workflow is in the process of shutting down.""" |
| 59 |
|
| 60 |
STOPPED = "stopped" |
| 61 |
"""Workflow is not running.""" |
| 62 |
|
| 63 |
|
| 64 |
class StopMode(Enum): |
| 65 |
"""The possible modes of a workflow shutdown""" |
| 66 |
|
| 67 |
AUTO = 'AUTOMATIC' |
| 68 |
"""Workflow has reached a state where it can automatically stop""" |
| 69 |
|
| 70 |
AUTO_ON_TASK_FAILURE = 'AUTOMATIC(ON-TASK-FAILURE)' |
| 71 |
"""A task has failed and ``--abort-if-any-task-fails`` was used.""" |
| 72 |
|
| 73 |
REQUEST_CLEAN = 'REQUEST(CLEAN)' |
| 74 |
"""External shutdown request, will wait for active jobs to complete.""" |
| 75 |
|
| 76 |
REQUEST_KILL = 'REQUEST(KILL)' |
| 77 |
"""External shutdown request, will wait for active jobs to be killed.""" |
| 78 |
|
| 79 |
REQUEST_NOW = 'REQUEST(NOW)' |
| 80 |
"""External shutdown request, will wait for event handlers to complete.""" |
| 81 |
|
| 82 |
REQUEST_NOW_NOW = 'REQUEST(NOW-NOW)' |
| 83 |
"""External immediate shutdown request.""" |
| 84 |
|
| 85 |
def explain(self) -> str: |
| 86 |
"""Return an explanation of what a workflow in this state is doing. |
| 87 |
|
| 88 |
This is used in the workflow status message if the workflow is stopping |
| 89 |
to clarify what the scheduler is doing. |
| 90 |
""" |
| 91 |
if self in (self.AUTO, self.REQUEST_CLEAN): # type: ignore |
| 92 |
return 'waiting for active jobs to complete' |
| 93 |
if self == self.REQUEST_KILL: # type: ignore |
| 94 |
return 'killing active jobs' |
| 95 |
if self in ( # type: ignore |
| 96 |
self.AUTO_ON_TASK_FAILURE, # type: ignore |
| 97 |
self.REQUEST_NOW, |
| 98 |
self.REQUEST_NOW_NOW, |
| 99 |
): |
| 100 |
return 'shutting down' |
| 101 |
return '' |
| 102 |
|
| 103 |
def describe(self) -> str: |
| 104 |
"""Return a user-friendly description of this state. |
| 105 |
|
| 106 |
This is used in the schema to convey what the different stop modes do. |
| 107 |
""" |
| 108 |
if self == self.AUTO: # type: ignore |
| 109 |
return 'Wait until workflow has completed.' |
| 110 |
if self == self.AUTO_ON_TASK_FAILURE: # type: ignore |
| 111 |
return 'Wait until the first task fails.' |
| 112 |
if self == self.REQUEST_CLEAN: # type: ignore |
| 113 |
return ( |
| 114 |
'Regular shutdown:\n' |
| 115 |
'* Wait for all active jobs to complete.\n' |
| 116 |
'* Run workflow event handlers and wait for them to complete.' |
| 117 |
) |
| 118 |
if self == self.REQUEST_KILL: # type: ignore |
| 119 |
return ( |
| 120 |
'Kill shutdown:\n' |
| 121 |
'* Wait for all active jobs to be killed.\n' |
| 122 |
'* Run workflow event handlers and wait for them to complete.' |
| 123 |
) |
| 124 |
if self == self.REQUEST_NOW: # type: ignore |
| 125 |
return ( |
| 126 |
'Immediate shutdown\n' |
| 127 |
"* Don't kill submitted or running jobs.\n" |
| 128 |
'* Run workflow event handlers and wait for them to complete.' |
| 129 |
) |
| 130 |
if self == self.REQUEST_NOW_NOW: # type: ignore |
| 131 |
return ( |
| 132 |
'Immediate shutdown\n' |
| 133 |
"* Don't kill submitted or running jobs.\n" |
| 134 |
"* Don't run event handlers." |
| 135 |
) |
| 136 |
raise KeyError(f'No description for {self}.') |
| 137 |
|
| 138 |
|
| 139 |
class AutoRestartMode(Enum): |
| 140 |
"""The possible modes of a workflow auto-restart.""" |
| 141 |
|
| 142 |
RESTART_NORMAL = 'stop and restart' |
| 143 |
"""Workflow will stop immediately and attempt to restart.""" |
| 144 |
|
| 145 |
FORCE_STOP = 'stop' |
| 146 |
"""Workflow will stop immediately but *not* attempt to restart.""" |
| 147 |
|
| 148 |
|
| 149 |
def get_workflow_status(schd: 'Scheduler') -> WorkflowStatus: |
| 150 |
"""Return the status of the provided workflow.""" |
| 151 |
if schd.stop_mode is not None: |
| 152 |
return WorkflowStatus.STOPPING |
| 153 |
if schd.is_paused or schd.reload_pending: |
| 154 |
return WorkflowStatus.PAUSED |
| 155 |
return WorkflowStatus.RUNNING |
| 156 |
|
| 157 |
|
| 158 |
def get_workflow_status_msg(schd: 'Scheduler') -> str: |
| 159 |
"""Return a short, concise status message for the provided workflow.""" |
| 160 |
if schd.stop_mode is not None: |
| 161 |
return f'stopping: {schd.stop_mode.explain()}' |
| 162 |
if schd.reload_pending: |
| 163 |
return f'reloading: {schd.reload_pending}' |
| 164 |
if schd.is_stalled: |
| 165 |
if schd.is_paused: |
| 166 |
return 'stalled and paused' |
| 167 |
return 'stalled' |
| 168 |
if schd.is_paused: |
| 169 |
return 'paused' |
| 170 |
if schd.stop_clock_time is not None: |
| 171 |
return WORKFLOW_STATUS_RUNNING_TO_STOP % time2str( |
| 172 |
schd.stop_clock_time |
| 173 |
) |
| 174 |
stop_point_msg = _get_earliest_stop_point_status_msg(schd.pool) |
| 175 |
if stop_point_msg is not None: |
| 176 |
return stop_point_msg |
| 177 |
if schd.config and schd.config.final_point: |
| 178 |
return WORKFLOW_STATUS_RUNNING_TO_STOP % schd.config.final_point |
| 179 |
# fallback - running indefinitely |
| 180 |
return 'running' |
| 181 |
|
| 182 |
|
| 183 |
def _get_earliest_stop_point_status_msg(pool: 'TaskPool') -> Optional[str]: |
| 184 |
"""Return the status message for the earliest stop point in the pool, |
| 185 |
if any.""" |
| 186 |
template = WORKFLOW_STATUS_RUNNING_TO_STOP |
| 187 |
prop: Union[PointBase, str, None] = pool.stop_task_id |
| 188 |
min_point: Optional[PointBase] = get_point( |
| 189 |
tokenise(pool.stop_task_id, relative=True)['cycle'] |
| 190 |
if pool.stop_task_id else None |
| 191 |
) |
| 192 |
for point, tmpl in ( |
| 193 |
(pool.stop_point, WORKFLOW_STATUS_RUNNING_TO_STOP), |
| 194 |
(pool.hold_point, WORKFLOW_STATUS_RUNNING_TO_HOLD) |
| 195 |
): |
| 196 |
if point is not None and (min_point is None or point < min_point): |
| 197 |
template = tmpl |
| 198 |
min_point = point |
| 199 |
prop = point |
| 200 |
if prop is None: |
| 201 |
return None |
| 202 |
return template % prop |