| 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 |
|
| 19 |
"""cylc ext-trigger [OPTIONS] ARGS |
| 20 |
|
| 21 |
Report an external event message to a scheduler. |
| 22 |
|
| 23 |
External triggers allow any program to send a message to the Cylc scheduler. |
| 24 |
Cylc can use this message as a signal that an external prerequisite has been |
| 25 |
satisfied and trigger the task accordingly. |
| 26 |
|
| 27 |
The WORKFLOW argument should be unique to each external trigger event. When |
| 28 |
an incoming message satisfies a task's external trigger the message TRIGGER_ID |
| 29 |
is broadcast to all downstream tasks in the cycle point as |
| 30 |
``$CYLC_EXT_TRIGGER_ID``. Tasks can use ``$CYLC_EXT_TRIGGER_ID``, for example, |
| 31 |
to identify a new data file that the external triggering system is responding |
| 32 |
to. |
| 33 |
|
| 34 |
Use the retry options in case the target workflow is down or out of contact. |
| 35 |
|
| 36 |
Note: to manually trigger a task use 'cylc trigger', not this command. |
| 37 |
""" |
| 38 |
|
| 39 |
from time import sleep |
| 40 |
from typing import TYPE_CHECKING |
| 41 |
|
| 42 |
from cylc.flow import LOG |
| 43 |
from cylc.flow.exceptions import CylcError, ClientError |
| 44 |
from cylc.flow.id_cli import parse_id |
| 45 |
from cylc.flow.network.client_factory import get_client |
| 46 |
from cylc.flow.option_parsers import ( |
| 47 |
WORKFLOW_ID_ARG_DOC, |
| 48 |
CylcOptionParser as COP, |
| 49 |
) |
| 50 |
from cylc.flow.terminal import cli_function |
| 51 |
|
| 52 |
if TYPE_CHECKING: |
| 53 |
from optparse import Values |
| 54 |
|
| 55 |
|
| 56 |
MAX_N_TRIES = 5 |
| 57 |
RETRY_INTVL_SECS = 10.0 |
| 58 |
|
| 59 |
MSG_SEND_FAILED = "Send message: try %s of %s failed" |
| 60 |
MSG_SEND_RETRY = "Retrying in %s seconds, timeout is %s" |
| 61 |
MSG_SEND_SUCCEED = "Send message: try %s of %s succeeded" |
| 62 |
|
| 63 |
MUTATION = ''' |
| 64 |
mutation ( |
| 65 |
$wFlows: [WorkflowID]!, |
| 66 |
$eventMsg: String!, |
| 67 |
$eventId: String! |
| 68 |
) { |
| 69 |
extTrigger ( |
| 70 |
workflows: $wFlows, |
| 71 |
message: $eventMsg, |
| 72 |
id: $eventId |
| 73 |
) { |
| 74 |
result |
| 75 |
} |
| 76 |
} |
| 77 |
''' |
| 78 |
|
| 79 |
|
| 80 |
def get_option_parser() -> COP: |
| 81 |
parser = COP( |
| 82 |
__doc__, |
| 83 |
comms=True, |
| 84 |
argdoc=[ |
| 85 |
WORKFLOW_ID_ARG_DOC, |
| 86 |
("MSG", "External trigger message"), |
| 87 |
("TRIGGER_ID", "Unique trigger ID"), |
| 88 |
], |
| 89 |
) |
| 90 |
|
| 91 |
parser.add_option( |
| 92 |
"--max-tries", |
| 93 |
help=r"Maximum number of send attempts (default: %default).", |
| 94 |
metavar="INT", |
| 95 |
action="store", |
| 96 |
default=MAX_N_TRIES, |
| 97 |
dest="max_n_tries" |
| 98 |
) |
| 99 |
|
| 100 |
parser.add_option( |
| 101 |
"--retry-interval", |
| 102 |
help=r"Delay in seconds before retrying (default: %default).", |
| 103 |
metavar="SEC", |
| 104 |
action="store", |
| 105 |
default=RETRY_INTVL_SECS, |
| 106 |
dest="retry_intvl_secs" |
| 107 |
) |
| 108 |
|
| 109 |
return parser |
| 110 |
|
| 111 |
|
| 112 |
@cli_function(get_option_parser) |
| 113 |
def main( |
| 114 |
parser: COP, |
| 115 |
options: 'Values', |
| 116 |
workflow_id: str, |
| 117 |
event_msg: str, |
| 118 |
event_id: str |
| 119 |
) -> None: |
| 120 |
workflow_id, *_ = parse_id( |
| 121 |
workflow_id, |
| 122 |
constraint='workflows', |
| 123 |
) |
| 124 |
LOG.info( |
| 125 |
'Send to workflow %s: "%s" (%s)', workflow_id, event_msg, event_id |
| 126 |
) |
| 127 |
pclient = get_client(workflow_id, timeout=options.comms_timeout) |
| 128 |
|
| 129 |
max_n_tries = int(options.max_n_tries) |
| 130 |
retry_intvl_secs = float(options.retry_intvl_secs) |
| 131 |
|
| 132 |
mutation_kwargs = { |
| 133 |
'request_string': MUTATION, |
| 134 |
'variables': { |
| 135 |
'wFlows': [workflow_id], |
| 136 |
'eventMsg': event_msg, |
| 137 |
'eventId': event_id, |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
for i_try in range(max_n_tries): |
| 142 |
try: |
| 143 |
pclient('graphql', mutation_kwargs) |
| 144 |
except ClientError as exc: |
| 145 |
LOG.exception(exc) |
| 146 |
LOG.info(MSG_SEND_FAILED, i_try + 1, max_n_tries) |
| 147 |
if i_try == max_n_tries - 1: # final attempt |
| 148 |
raise CylcError('send failed') from None |
| 149 |
LOG.info(MSG_SEND_RETRY, retry_intvl_secs, options.comms_timeout) |
| 150 |
sleep(retry_intvl_secs) |
| 151 |
else: |
| 152 |
if i_try > 0: |
| 153 |
LOG.info(MSG_SEND_SUCCEED, i_try + 1, max_n_tries) |
| 154 |
break |