| 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 |
"""Publisher for workflow runtime API.""" |
| 17 |
|
| 18 |
import asyncio |
| 19 |
from typing import Callable, Optional, Set, TYPE_CHECKING, Union |
| 20 |
|
| 21 |
import zmq |
| 22 |
|
| 23 |
from cylc.flow import LOG |
| 24 |
from cylc.flow.network import ZMQSocketBase |
| 25 |
|
| 26 |
|
| 27 |
if TYPE_CHECKING: |
| 28 |
from zmq.asyncio import Context |
| 29 |
|
| 30 |
|
| 31 |
def serialize_data( |
| 32 |
data: object, serializer: Union[Callable, str, None], *args, **kwargs |
| 33 |
): |
| 34 |
"""Serialize by specified method.""" |
| 35 |
if callable(serializer): |
| 36 |
return serializer(data, *args, **kwargs) |
| 37 |
if isinstance(serializer, str): |
| 38 |
return getattr(data, serializer)(*args, **kwargs) |
| 39 |
return data |
| 40 |
|
| 41 |
|
| 42 |
class WorkflowPublisher(ZMQSocketBase): |
| 43 |
"""Initiate the PUB part of a ZMQ PUB-SUB pair. |
| 44 |
|
| 45 |
This class contains the logic for the ZMQ message Publisher. |
| 46 |
|
| 47 |
Usage: |
| 48 |
* Call publish to send items to subscribers. |
| 49 |
|
| 50 |
""" |
| 51 |
|
| 52 |
def __init__(self, workflow: str, context: 'Optional[Context]' = None): |
| 53 |
super().__init__(zmq.PUB, workflow, bind=True, context=context) |
| 54 |
self.topics: Set[bytes] = set() |
| 55 |
|
| 56 |
def _socket_options(self): |
| 57 |
"""Set socket options after socket instantiation and before bind. |
| 58 |
|
| 59 |
Overwrites Base method. |
| 60 |
|
| 61 |
""" |
| 62 |
# this limit on messages in queue is more than enough, |
| 63 |
# as messages correspond to scheduler loops (*messages/loop): |
| 64 |
self.socket.sndhwm = 1000 |
| 65 |
|
| 66 |
def _bespoke_stop(self): |
| 67 |
"""Bespoke stop items.""" |
| 68 |
LOG.debug('stopping zmq publisher...') |
| 69 |
self.stopping = True |
| 70 |
|
| 71 |
async def send_multi( |
| 72 |
self, |
| 73 |
topic: bytes, |
| 74 |
data: object, |
| 75 |
serializer: Union[Callable, str, None] = None |
| 76 |
) -> None: |
| 77 |
"""Send multi part message. |
| 78 |
|
| 79 |
Args: |
| 80 |
topic: The topic of the message. |
| 81 |
data: Data element/message to serialise and send. |
| 82 |
serializer: string/func for encoding. |
| 83 |
|
| 84 |
""" |
| 85 |
if self.socket: |
| 86 |
self.topics.add(topic) |
| 87 |
self.socket.send_multipart( |
| 88 |
[topic, serialize_data(data, serializer)] |
| 89 |
) |
| 90 |
# else we are in the process of shutting down - don't send anything |
| 91 |
|
| 92 |
async def publish(self, *items: tuple) -> None: |
| 93 |
"""Publish topics. |
| 94 |
|
| 95 |
Args: |
| 96 |
items (iterable): [(topic, data, serializer)] |
| 97 |
|
| 98 |
""" |
| 99 |
try: |
| 100 |
await asyncio.gather( |
| 101 |
*(self.send_multi(*item) for item in items) |
| 102 |
) |
| 103 |
except Exception as exc: |
| 104 |
LOG.exception(f"publish: {exc}") |