| 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 |
"""Utilities supporting dummy mode. |
| 17 |
|
| 18 |
Dummy mode shares settings with simulation mode. |
| 19 |
""" |
| 20 |
|
| 21 |
from typing import ( |
| 22 |
TYPE_CHECKING, |
| 23 |
Any, |
| 24 |
Dict, |
| 25 |
Literal, |
| 26 |
Tuple, |
| 27 |
) |
| 28 |
|
| 29 |
from cylc.flow.platforms import get_platform |
| 30 |
from cylc.flow.run_modes import RunMode |
| 31 |
from cylc.flow.run_modes.simulation import ( |
| 32 |
ModeSettings, |
| 33 |
disable_platforms, |
| 34 |
get_simulated_run_len, |
| 35 |
parse_fail_cycle_points, |
| 36 |
) |
| 37 |
|
| 38 |
|
| 39 |
if TYPE_CHECKING: |
| 40 |
from cylc.flow.task_job_mgr import TaskJobManager |
| 41 |
from cylc.flow.task_proxy import TaskProxy |
| 42 |
|
| 43 |
|
| 44 |
CLEAR_THESE_SCRIPTS = [ |
| 45 |
'init-script', |
| 46 |
'env-script', |
| 47 |
'pre-script', |
| 48 |
'post-script', |
| 49 |
'err-script', |
| 50 |
'exit-script', |
| 51 |
] |
| 52 |
|
| 53 |
|
| 54 |
def submit_task_job( |
| 55 |
task_job_mgr: 'TaskJobManager', |
| 56 |
itask: 'TaskProxy', |
| 57 |
rtconfig: Dict[str, Any], |
| 58 |
now: Tuple[float, str] |
| 59 |
) -> 'Literal[False]': |
| 60 |
"""Submit a task in dummy mode. |
| 61 |
|
| 62 |
Returns: |
| 63 |
False indicating that TaskJobManager needs to continue running the |
| 64 |
live mode path. |
| 65 |
""" |
| 66 |
configure_dummy_mode( |
| 67 |
rtconfig, itask.tdef.rtconfig['simulation']['fail cycle points']) |
| 68 |
|
| 69 |
itask.summary['started_time'] = now[0] |
| 70 |
task_job_mgr._set_retry_timers(itask, rtconfig) |
| 71 |
|
| 72 |
itask.mode_settings = ModeSettings( |
| 73 |
itask, |
| 74 |
task_job_mgr.workflow_db_mgr, |
| 75 |
rtconfig |
| 76 |
) |
| 77 |
|
| 78 |
itask.platform = get_platform() |
| 79 |
itask.platform['name'] = RunMode.DUMMY.value |
| 80 |
itask.summary['job_runner_name'] = RunMode.DUMMY.value |
| 81 |
itask.summary[task_job_mgr.KEY_EXECUTE_TIME_LIMIT] = ( |
| 82 |
itask.mode_settings.simulated_run_length) |
| 83 |
itask.jobs.append( |
| 84 |
task_job_mgr.get_simulation_job_conf(itask)) |
| 85 |
task_job_mgr.workflow_db_mgr.put_insert_task_jobs( |
| 86 |
itask, { |
| 87 |
'time_submit': now[1], |
| 88 |
'try_num': itask.get_try_num(), |
| 89 |
} |
| 90 |
) |
| 91 |
|
| 92 |
return False |
| 93 |
|
| 94 |
|
| 95 |
def configure_dummy_mode(rtc: Dict[str, Any], fallback: str) -> None: |
| 96 |
"""Adjust task defs for dummy mode. |
| 97 |
""" |
| 98 |
rtc['submission retry delays'] = [1] |
| 99 |
# Generate dummy scripting. |
| 100 |
|
| 101 |
for script in CLEAR_THESE_SCRIPTS: |
| 102 |
rtc[script] = '' |
| 103 |
|
| 104 |
rtc['script'] = build_dummy_script( |
| 105 |
rtc, get_simulated_run_len(rtc)) |
| 106 |
disable_platforms(rtc) |
| 107 |
# Disable environment, in case it depends on env-script. |
| 108 |
rtc['environment'] = {} |
| 109 |
rtc["simulation"][ |
| 110 |
"fail cycle points" |
| 111 |
] = parse_fail_cycle_points( |
| 112 |
rtc["simulation"]["fail cycle points"], fallback |
| 113 |
) |
| 114 |
|
| 115 |
|
| 116 |
def build_dummy_script(rtc: Dict[str, Any], sleep_sec: int) -> str: |
| 117 |
"""Create fake scripting for dummy mode script. |
| 118 |
""" |
| 119 |
script = "sleep %d" % sleep_sec |
| 120 |
# Dummy message outputs. |
| 121 |
for msg in rtc['outputs'].values(): |
| 122 |
script += "\ncylc message '%s'" % msg |
| 123 |
if rtc['simulation']['fail try 1 only']: |
| 124 |
arg1 = "true" |
| 125 |
else: |
| 126 |
arg1 = "false" |
| 127 |
arg2 = " ".join(rtc['simulation']['fail cycle points']) |
| 128 |
script += "\ncylc__job__dummy_result %s %s || exit 1" % (arg1, arg2) |
| 129 |
return script |