| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE. |
| 4 |
# Copyright (C) NIWA & British Crown (Met Office) & Contributors. |
| 5 |
# |
| 6 |
# This program is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation, either version 3 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# This program is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
r"""cylc message [OPTIONS] -- ARGS |
| 19 |
|
| 20 |
Command to record and send messages from task jobs back to the scheduler. |
| 21 |
|
| 22 |
Messages are sent to: |
| 23 |
- job stdout/stderr |
| 24 |
- the job status file, if there is one |
| 25 |
- the scheduler, if communication is possible |
| 26 |
|
| 27 |
Jobs automatically use this command to record and report job status: started |
| 28 |
(running) and success or failure. |
| 29 |
|
| 30 |
Applications run by jobs can use this command to report custom messages and |
| 31 |
and registered task output messages. |
| 32 |
|
| 33 |
For custom outputs, use the task message not the associated output name: |
| 34 |
[runtime] |
| 35 |
[[my-task]] |
| 36 |
# ... |
| 37 |
[[[outputs]]] |
| 38 |
# <output-name> = <task-message> |
| 39 |
x = "file x completed and archived" |
| 40 |
|
| 41 |
Messages can be specified as arguments. A '-' indicates that the command should |
| 42 |
read messages from STDIN. When reading from STDIN, multiple messages are |
| 43 |
separated by empty lines. |
| 44 |
|
| 45 |
Examples: |
| 46 |
# Single message as an argument: |
| 47 |
$ cylc message -- "${CYLC_WORKFLOW_ID}" "${CYLC_TASK_JOB}" 'Hello world!' |
| 48 |
|
| 49 |
# Multiple messages as arguments: |
| 50 |
$ cylc message -- "${CYLC_WORKFLOW_ID}" "${CYLC_TASK_JOB}" \ |
| 51 |
> 'Hello world!' 'Hi' 'WARNING:Hey!' |
| 52 |
|
| 53 |
# Multiple messages on STDIN: |
| 54 |
$ cylc message -- "${CYLC_WORKFLOW_ID}" "${CYLC_TASK_JOB}" - <<'__STDIN__' |
| 55 |
> Hello |
| 56 |
> world! |
| 57 |
> |
| 58 |
> Hi |
| 59 |
> |
| 60 |
> WARNING:Hey! |
| 61 |
>__STDIN__ |
| 62 |
|
| 63 |
Note "${CYLC_WORKFLOW_ID}" and "${CYLC_TASK_JOB}" are available in job |
| 64 |
environments - you do not need to write their actual values in task scripting. |
| 65 |
|
| 66 |
Each message can be prefixed with a severity level using the syntax |
| 67 |
'SEVERITY:MESSAGE' (colons cannot be used unless such a prefix is provided). |
| 68 |
|
| 69 |
The default message severity is INFO. The --severity=SEVERITY option can be |
| 70 |
used to set the default severity level for all unprefixed messages. |
| 71 |
|
| 72 |
Increased severity will make messages more visible in workflow logs, using |
| 73 |
colour and format changes. DEBUG messages will not be shown in logs by default. |
| 74 |
|
| 75 |
The severity levels are those of the Python Logging Library |
| 76 |
https://docs.python.org/3/library/logging.html#logging-levels: |
| 77 |
|
| 78 |
- CRITICAL |
| 79 |
- ERROR |
| 80 |
- WARNING |
| 81 |
- INFO |
| 82 |
- DEBUG |
| 83 |
|
| 84 |
Note: |
| 85 |
To abort a job script with a custom error message, use cylc__job_abort: |
| 86 |
cylc__job_abort 'message...' |
| 87 |
(For technical reasons this is a shell function, not a cylc sub-command). |
| 88 |
|
| 89 |
For backward compatibility, if number of arguments is less than or equal to 2, |
| 90 |
the command assumes the classic interface, where all arguments are messages. |
| 91 |
Otherwise, the first 2 arguments are assumed to be workflow ID and job |
| 92 |
identifier. |
| 93 |
""" |
| 94 |
|
| 95 |
|
| 96 |
from logging import getLevelName, INFO |
| 97 |
import os |
| 98 |
import sys |
| 99 |
from typing import TYPE_CHECKING |
| 100 |
|
| 101 |
from cylc.flow.id_cli import parse_id |
| 102 |
from cylc.flow.option_parsers import ( |
| 103 |
WORKFLOW_ID_ARG_DOC, |
| 104 |
CylcOptionParser as COP |
| 105 |
) |
| 106 |
from cylc.flow.task_message import record_messages |
| 107 |
from cylc.flow.terminal import cli_function |
| 108 |
from cylc.flow.exceptions import InputError |
| 109 |
from cylc.flow.unicode_rules import TaskMessageValidator |
| 110 |
|
| 111 |
if TYPE_CHECKING: |
| 112 |
from optparse import Values |
| 113 |
|
| 114 |
|
| 115 |
def get_option_parser() -> COP: |
| 116 |
parser = COP( |
| 117 |
__doc__, |
| 118 |
comms=True, |
| 119 |
argdoc=[ |
| 120 |
COP.optional(WORKFLOW_ID_ARG_DOC), |
| 121 |
COP.optional( |
| 122 |
('JOB', 'Job ID - CYCLE/TASK_NAME/SUBMIT_NUM') |
| 123 |
), |
| 124 |
COP.optional( |
| 125 |
('[SEVERITY:]MESSAGE ...', 'Messages') |
| 126 |
) |
| 127 |
] |
| 128 |
) |
| 129 |
|
| 130 |
parser.add_option( |
| 131 |
'-s', '--severity', '-p', '--priority', |
| 132 |
metavar='SEVERITY', |
| 133 |
help='Set severity levels for messages that do not have one', |
| 134 |
action='store', dest='severity') |
| 135 |
|
| 136 |
return parser |
| 137 |
|
| 138 |
|
| 139 |
@cli_function(get_option_parser) |
| 140 |
def main(parser: COP, options: 'Values', *args: str) -> None: |
| 141 |
"""CLI.""" |
| 142 |
if not args: |
| 143 |
parser.error('No message supplied') |
| 144 |
return |
| 145 |
if len(args) <= 2: |
| 146 |
# BACK COMPAT: args <= 2 |
| 147 |
# from: |
| 148 |
# 7.6? |
| 149 |
# remove at: |
| 150 |
# 9.0? |
| 151 |
# (As of Dec 2020 some functional tests still use the classic |
| 152 |
# two arg interface) |
| 153 |
workflow_id = os.getenv('CYLC_WORKFLOW_ID') |
| 154 |
job_id = os.getenv('CYLC_TASK_JOB') |
| 155 |
if not workflow_id or not job_id: |
| 156 |
raise InputError( |
| 157 |
"Must set $CYLC_WORKFLOW_ID and $CYLC_TASK_JOB if not " |
| 158 |
"specified as arguments" |
| 159 |
) |
| 160 |
message_strs = list(args) |
| 161 |
else: |
| 162 |
workflow_id, job_id, *message_strs = args |
| 163 |
workflow_id, *_ = parse_id( |
| 164 |
workflow_id, |
| 165 |
constraint='workflows', |
| 166 |
) |
| 167 |
# Read messages from STDIN |
| 168 |
if '-' in message_strs: |
| 169 |
current_message_str = '' |
| 170 |
while True: # Note: `for line in sys.stdin:` can hang |
| 171 |
message_str = sys.stdin.readline() |
| 172 |
if message_str.strip(): |
| 173 |
# non-empty line |
| 174 |
current_message_str += message_str |
| 175 |
elif message_str: |
| 176 |
# empty line, start next message |
| 177 |
if current_message_str: |
| 178 |
message_strs.append(current_message_str) |
| 179 |
current_message_str = '' # reset |
| 180 |
else: |
| 181 |
# end of file |
| 182 |
if current_message_str: |
| 183 |
message_strs.append(current_message_str) |
| 184 |
break |
| 185 |
# Separate "severity: message" |
| 186 |
messages = [] # [(severity, message_str), ...] |
| 187 |
for message_str in message_strs: |
| 188 |
if message_str == '-': |
| 189 |
pass |
| 190 |
elif ':' in message_str: |
| 191 |
valid, err_msg = TaskMessageValidator.validate(message_str) |
| 192 |
if not valid: |
| 193 |
raise InputError( |
| 194 |
f'Invalid task message "{message_str}" - {err_msg}') |
| 195 |
messages.append( |
| 196 |
[item.strip() for item in message_str.split(':', 1)]) |
| 197 |
elif options.severity: |
| 198 |
messages.append([options.severity, message_str.strip()]) |
| 199 |
else: |
| 200 |
messages.append([getLevelName(INFO), message_str.strip()]) |
| 201 |
record_messages(workflow_id, job_id, messages) |