| 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 subscribe [OPTIONS] ARGS |
| 20 |
|
| 21 |
(This command is for internal use.) |
| 22 |
|
| 23 |
Invoke workflow subscriber to receive published workflow output. |
| 24 |
""" |
| 25 |
|
| 26 |
import json |
| 27 |
import sys |
| 28 |
import time |
| 29 |
|
| 30 |
from google.protobuf.json_format import MessageToDict |
| 31 |
|
| 32 |
from cylc.flow.exceptions import ClientError |
| 33 |
from cylc.flow.option_parsers import ( |
| 34 |
WORKFLOW_ID_ARG_DOC, |
| 35 |
CylcOptionParser as COP, |
| 36 |
) |
| 37 |
from cylc.flow.network import get_location |
| 38 |
from cylc.flow.network.subscriber import WorkflowSubscriber, process_delta_msg |
| 39 |
from cylc.flow.terminal import cli_function |
| 40 |
from cylc.flow.data_store_mgr import DELTAS_MAP |
| 41 |
|
| 42 |
INTERNAL = True |
| 43 |
|
| 44 |
|
| 45 |
def print_message(topic, data, subscriber=None, once=False): |
| 46 |
"""Print protobuf message.""" |
| 47 |
print(f'Received: {topic}') |
| 48 |
if topic == 'shutdown': |
| 49 |
print(data.decode('utf-8')) |
| 50 |
subscriber.stop() |
| 51 |
return |
| 52 |
sys.stdout.write( |
| 53 |
json.dumps(MessageToDict(data), indent=4) + '\n') |
| 54 |
if once and subscriber is not None: |
| 55 |
subscriber.stop() |
| 56 |
|
| 57 |
|
| 58 |
def get_option_parser() -> COP: |
| 59 |
"""Augment options parser to current context.""" |
| 60 |
parser = COP( |
| 61 |
__doc__, |
| 62 |
argdoc=[WORKFLOW_ID_ARG_DOC], |
| 63 |
comms=True |
| 64 |
) |
| 65 |
|
| 66 |
delta_keys = list(DELTAS_MAP) |
| 67 |
pb_topics = ("Directly published data-store topics include: '" + |
| 68 |
("', '").join(delta_keys[:-1]) + |
| 69 |
"' and '" + delta_keys[-1] + "'.") |
| 70 |
parser.add_option( |
| 71 |
"-T", "--topics", |
| 72 |
help="Specify a comma delimited list of subscription topics. " |
| 73 |
+ pb_topics, |
| 74 |
action="store", dest="topics", default='workflow') |
| 75 |
|
| 76 |
parser.add_option( |
| 77 |
"-o", "--once", |
| 78 |
help="Show a single publish then exit.", |
| 79 |
action="store_true", default=False, dest="once") |
| 80 |
|
| 81 |
return parser |
| 82 |
|
| 83 |
|
| 84 |
@cli_function(get_option_parser) |
| 85 |
def main(_, options, *args): |
| 86 |
workflow_id = args[0] |
| 87 |
|
| 88 |
try: |
| 89 |
while True: |
| 90 |
try: |
| 91 |
host, _, port, _ = get_location(workflow_id) |
| 92 |
except (ClientError, IOError, TypeError, ValueError) as exc: |
| 93 |
print(exc) |
| 94 |
time.sleep(3) |
| 95 |
continue |
| 96 |
break |
| 97 |
except KeyboardInterrupt: |
| 98 |
sys.exit() |
| 99 |
|
| 100 |
print(f'Connecting to tcp://{host}:{port}') |
| 101 |
topic_set = set() |
| 102 |
topic_set.add(b'shutdown') |
| 103 |
for topic in options.topics.split(','): |
| 104 |
topic_set.add(topic.encode('utf-8')) |
| 105 |
|
| 106 |
subscriber = WorkflowSubscriber( |
| 107 |
workflow_id, |
| 108 |
host=host, |
| 109 |
port=port, |
| 110 |
topics=topic_set |
| 111 |
) |
| 112 |
|
| 113 |
subscriber.loop.create_task( |
| 114 |
subscriber.subscribe( |
| 115 |
process_delta_msg, |
| 116 |
func=print_message, |
| 117 |
subscriber=subscriber, |
| 118 |
once=options.once |
| 119 |
) |
| 120 |
) |
| 121 |
|
| 122 |
# run Python run |
| 123 |
try: |
| 124 |
subscriber.loop.run_forever() |
| 125 |
except (KeyboardInterrupt, SystemExit): |
| 126 |
print('\nDisconnecting') |
| 127 |
subscriber.stop() |
| 128 |
sys.exit() |