Debian Codegraph @

cylc/flow/network/client_factory.py

raw
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
17 from enum import Enum
18 import os
19 from typing import TYPE_CHECKING, Union
20
21 if TYPE_CHECKING:
22 from cylc.flow.network.client import WorkflowRuntimeClientBase
23
24
25 class CommsMeth(Enum):
26 """String literals used for identifying communication methods"""
27
28 POLL = 'poll'
29 SSH = 'ssh'
30 ZMQ = 'zmq'
31
32
33 def get_comms_method() -> CommsMeth:
34 """"Return Communication Method from environment variable, default zmq"""
35 return CommsMeth(
36 os.getenv('CYLC_TASK_COMMS_METHOD', CommsMeth.ZMQ.value)
37 )
38
39
40 def get_runtime_client(
41 comms_method: CommsMeth,
42 workflow: str,
43 timeout: Union[float, str, None] = None
44 ) -> 'WorkflowRuntimeClientBase':
45 """Return client for the provided communication method.
46
47 Args:
48 comm_method: communication method
49 workflow: workflow ID
50 """
51 if comms_method == CommsMeth.SSH:
52 from cylc.flow.network.ssh_client import WorkflowRuntimeClient
53 else:
54 from cylc.flow.network.client import ( # type: ignore[assignment]
55 WorkflowRuntimeClient
56 )
57 return WorkflowRuntimeClient(workflow, timeout=timeout)
58
59
60 def get_client(workflow, timeout=None):
61 """Get communication method and return correct WorkflowRuntimeClient"""
62
63 return get_runtime_client(get_comms_method(), workflow, timeout=timeout)