-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathgateway.py
More file actions
512 lines (416 loc) · 20.3 KB
/
Copy pathgateway.py
File metadata and controls
512 lines (416 loc) · 20.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
import logging
import multiprocessing.pool
import os
import signal
import warnings
from datetime import datetime, timedelta
from socket import gethostname
from time import sleep
from typing import Any, Callable, List, Optional, Type, Union
import csp
from csp import ts
from pydantic import Field, PrivateAttr, create_model, model_validator
from csp_gateway.server.settings import Settings
from .csp import Channels, ChannelsFactory, ChannelsType, Module
__all__ = (
"Gateway",
"GatewayChannels",
"GatewayModule",
)
MAX_END_TIME = datetime(2261, 12, 31, 23, 59, 50, 999999)
log = logging.getLogger(__name__)
class GatewayChannels(Channels):
pass
class GatewayModule(Module[GatewayChannels]):
model_config = {
"ignored_types": (
csp.impl.wiring.GraphDefMeta,
csp.impl.wiring.NodeDefMeta,
),
}
def __hash__(self):
# So that csp doesn't complain about inability to memoize
return id(self)
def shutdown(self):
"""Perform cleanup when the module is shutting down"""
return
class Gateway(ChannelsFactory[GatewayChannels]):
"""The Gateway defines a combination of channels and a collection of modules that get and send data to the channels.
Furthermore, it builds the underling csp event processing graph, as well as the web application (for the REST, web sockets and graphical interfaces).
It also contains settings to define the behavior of the web application and how the csp graph should be run.
"""
# For pydantic 2
model_config = {"ignored_types": (csp.impl.wiring.GraphDefMeta, csp.impl.wiring.NodeDefMeta), "arbitrary_types_allowed": True}
modules: List[GatewayModule] = Field([], description="The list of modules that will operate on the channels.")
# Substructures
settings: Settings = Field(default_factory=Settings, description="Generic settings for the gateway")
web_app: Any = Field(None, description="The gateway will populate this field with the web application handle once started.")
channels_model: Type[GatewayChannels] = Field(
default=GatewayChannels,
description="The type of the channels. Users of a `Gateway` are expected to pass `channels`, and `channels_model` will"
"be automatically inferred from the type. Developers can subclass `Gateway` and set the default value of"
"`channels_model` to be the specific type of channels that users must provide.",
)
# Running attributes
output: Any = Field(None, description="(Running attribute). The gateway will save the output of the csp.graph run once complete to this field")
graph_built: bool = Field(
False,
description="(Running attribute). The gateway will set this field to True when the application has detected the csp graph build is complete",
)
graph_build_failed: bool = Field(
False, description="(Running attribute). The gateway will set this field to True if the csp graph build has failed"
)
running: bool = Field(False, description="(Running attribute). The gateway will set this field to True once the csp graph is running")
_in_test: bool = PrivateAttr(False)
_module_shutdown_timeout: int = PrivateAttr()
_dynamic_channels_instantiated: bool = PrivateAttr(False)
def __init__(
self,
modules: List[Module[GatewayChannels]] = None,
channels: ChannelsType = None,
*args: str,
**kwargs: str,
):
log.info(f"Initializing Gateway - pid[{os.getpid()}]")
channels = channels or GatewayChannels()
# Note that the channels object passed into the init function here is not necessarily the channels object
# that is used to build the graph. Things like dynamic channels can be added.
super().__init__(modules=modules, channels=channels, *args, **kwargs)
self.graph_built = False
self.graph_build_failed = False
self._in_test = False
self._module_shutdown_timeout = 60
def _instantiate_dynamic_channel(self, modules: List[Module[GatewayChannels]], channels: ChannelsType) -> GatewayChannels:
if self._dynamic_channels_instantiated:
return channels
if modules is None:
modules = []
dynamic_channels = {}
for m in modules:
module_dynamic_channels = m.dynamic_channels() if hasattr(m, "dynamic_channels") else None
if module_dynamic_channels:
for n, t in module_dynamic_channels.items():
existing_type = dynamic_channels.get(n, None)
if existing_type is not None:
if t is not existing_type:
raise ValueError(f"Conflicting types for dynamic channel {n}.")
dynamic_channels[n] = t
if dynamic_channels:
dynamic_channel_kwargs = {n: (ts[t], None) for n, t in dynamic_channels.items()}
base_class = type(channels)
new_channel_name = f"{base_class.__name__}WithDynamicChannels"
channels_type = create_model(new_channel_name, __base__=base_class, **dynamic_channel_kwargs)
channels = channels_type(**{f: getattr(channels, f) for f in channels.model_dump(exclude_defaults=True)})
self._dynamic_channels_instantiated = True
return channels
@model_validator(mode="before")
@classmethod
def _model_validate(cls, values):
"""Root validator to append "user_modules" to list of modules."""
values["modules"] = values.get("modules") or []
values["modules"].extend(values.pop("user_modules", []))
return values
def __getattribute__(self, attr: str) -> Any:
if attr in ("channels", "state"):
if not self.running:
raise Exception("Can only access `{}` when engine is running".format(attr))
return object.__getattribute__(self, attr)
@csp.graph
def graph(self, user_graph: Callable[[GatewayChannels], Any] = None): # type: ignore[no-untyped-def]
"""Generates the csp graph corresponding to the gateway application.
This is the graph that will be called by the `start` method
This function can be passed to a csp.run call directly, which is especially useful if you want to control the graph running
yourself, perhaps to save the output values from csp.add_graph_output, or to customize the arguments passed
to csp.run further (for profiling, etc).
Args:
user_graph: A function that will be called with the channels to augment the existing application graph.
"""
# Detect csp stopped, call first so it executes its stop block first
self._stop_csp_detector()
try:
self.channels = self.build(channels=self._instantiate_dynamic_channel(self.modules, object.__getattribute__(self, "channels")))
self.graph_built = True
if user_graph:
user_graph_result = user_graph(object.__getattribute__(self, "channels"))
if user_graph_result is None:
# add a placeholder to ensure exit does not use os._exit
user_graph_result = csp.const(True)
csp.add_graph_output("user_graph", user_graph_result)
except Exception:
self.graph_build_failed = True
raise
log.info("Launching CSP")
csp.log(logging.INFO, "CSP Running", csp.const(True), logger=log)
# Detect csp started, call last so it executes its start block last
self._start_csp_detector()
@csp.node
def _start_csp_detector(self):
with csp.start():
self.running = True
@csp.node
def _stop_csp_detector(self):
with csp.stop():
self.running = False
def start(
self,
user_graph: Optional[Any] = None,
realtime: bool = True,
block: bool = True,
show: bool = False,
rest: bool = False,
ui: bool = False,
_in_test: bool = False,
starttime: Optional[datetime] = None,
endtime: Optional[Union[datetime, timedelta]] = None,
build_timeout: int = 30,
module_shutdown_timeout: int = 60,
**uvicorn_kwargs: Any,
) -> None:
"""Starts the application corresponding to the Gateway.
Depending on the provided settings, this will start the web application as well as run the csp graph.
To return the csp graph independent of the web application, look at the `graph` method.
Args:
user_graph: A function that will be called with the channels to augment the existing application graph.
realtime: Whether to run the csp graph in realtime mode
block: Whether the csp graph should be run in the foreground or on a background thread. Selecting `rest=True`
will force this setting to False.
show: Whether to write the csp graph to file (tmp.png) and return without running.
rest: Whether to launch the web application as part of the Gateway (i.e. for the REST endpoints). If `True`, will
force `block=False`.
ui: Whether to equip the web application with the pieces necessary to run the Perspective-based UI.
starttime: Start time of the csp graph run.
endtime: End time of the csp graph run.
build_timeout: Timeout that the web application uses to wait for the csp graph to start running successfully in the background.
If the csp graph is not running by the timeout, the web application will shut down.
module_shutdown_timeout: Timeout that the shutdown method uses to wait for all the modules to shutdown
uvicorn_kwargs: Additional kwargs to pass to the [uvicorn server config](https://www.uvicorn.org/settings/). Also see `GatewayWebApp`.
"""
# to avoid hard shutdown, starting webserver, etc
self._in_test = _in_test
self._module_shutdown_timeout = module_shutdown_timeout
# Back-compat: in csp-gateway <2.5 auth was configured via
# `Settings.AUTHENTICATE` / `Settings.API_KEY`. Apply those onto the
# `MountAPIKeyMiddleware` instance (if present) with a deprecation warning.
self._apply_legacy_auth_settings()
try:
if show:
# Show graph and return without running
os.makedirs("outputs", exist_ok=True)
csp.show_graph(self.graph, user_graph, graph_filename="outputs/gateway.png")
return
if rest:
# Run csp blocking on thread, run app in foreground
block = False
if block:
# If blocking, run csp in foreground
self.output = csp.run(
self.graph,
user_graph,
realtime=realtime,
starttime=starttime,
endtime=endtime or MAX_END_TIME,
)
else:
# If not blocking, run csp on thread
self.output = csp.run_on_thread(
self.graph,
user_graph,
realtime=realtime,
starttime=starttime,
endtime=endtime or MAX_END_TIME,
)
if rest:
# these are temporary until a pydantic model is in place
if isinstance(build_timeout, timedelta):
build_timeout = build_timeout.total_seconds()
if isinstance(build_timeout, str):
build_timeout = int(build_timeout)
# if graph shuts down or breaks, we need to shut down the web app
# as well otherwise it "looks" like its running but its now
self.web_app = self._build_web(ui=ui, timeout=build_timeout, _in_test=_in_test)
log.info("Launching web server on:")
url = f"http://{gethostname()}:{self.settings.PORT}"
# Allow module sto log information at statup
for module in self.modules:
_info = module.info(self.settings)
if _info:
log.info(_info)
log.info(f"\tDocs: {url}/docs")
log.info(f"\tDocs: {url}/redoc")
# Run the web app
# NOTE: this will block, except in test mode
self.web_app.run(**uvicorn_kwargs)
if _in_test:
# return here, dont bother waiting
return
if rest:
# send shutdown to csp if alive
return self._shutdown()
elif block:
# wait for csp thread to be done
return self._shutdown(wait=True)
return self.output
except KeyboardInterrupt:
log.critical("Shutting down...(Keyboard Interrupt)")
self._shutdown(user_initiated=True)
except Exception:
log.exception("Shutting down...")
self._shutdown(user_initiated=False)
# If we're here, we hit some form of error,
# so re-raise it back to the caller
raise
def _apply_legacy_auth_settings(self) -> None:
"""Bridge old-style `Settings.AUTHENTICATE` / `Settings.API_KEY` onto the middleware.
These used to live on `Settings`; they now live on `MountAPIKeyMiddleware`.
Old configs still parse but do nothing, so we read them off `Settings`
here and apply them with a `DeprecationWarning`. `AUTHENTICATE=False`
drops the middleware. `API_KEY` gets copied onto the middleware's
`api_key`. `AUTHENTICATE=True` with no middleware configured is an
invalid configuration because startup would otherwise continue without
enforcing the requested auth.
Rip out when we stop supporting the old config shape.
"""
authenticate = getattr(self.settings, "AUTHENTICATE", None)
api_key = getattr(self.settings, "API_KEY", None)
if authenticate is None and api_key is None:
# Nothing to migrate — user is on the new layout (or simply didn't
# set these), so don't nag them.
return
# Local import to avoid tightening coupling at module load.
from csp_gateway.server.middleware.api_key import MountAPIKeyMiddleware
middleware = next(
(module for module in self.modules if isinstance(module, MountAPIKeyMiddleware)),
None,
)
if authenticate is False:
warnings.warn(
"`Settings.AUTHENTICATE=False` is deprecated. To disable auth, omit `MountAPIKeyMiddleware` from your `modules` list instead.",
DeprecationWarning,
stacklevel=3,
)
if middleware is not None:
self.modules = [module for module in self.modules if module is not middleware]
# authenticate=False wins — don't also try to set api_key.
return
if api_key is not None:
warnings.warn(
"`Settings.API_KEY` is deprecated. Set `api_key` on your `MountAPIKeyMiddleware` instance directly.",
DeprecationWarning,
stacklevel=3,
)
if middleware is not None:
middleware.api_key = api_key
if authenticate is True and middleware is None:
# User explicitly opted-in via Settings but didn't add the middleware
# -- do not silently start without the auth they asked for.
raise ValueError(
"`Settings.AUTHENTICATE=True` requires a `MountAPIKeyMiddleware` in `modules`. "
"Add one to enforce auth, or omit `Settings.AUTHENTICATE` to use the 2.5+ middleware config shape."
)
def _get_web_app_class(self) -> Any:
# FIXME ugly
from csp_gateway.server import GatewayWebApp
return GatewayWebApp
def _build_web(self, ui: bool, timeout: int, _in_test: bool = False) -> Any:
log.info(f"Building server with: {self.settings}")
web_app_class = self._get_web_app_class()
web_app = web_app_class(
self,
csp_thread=self.output,
ui=ui,
settings=self.settings,
logger=log,
_in_test=_in_test,
)
# Wait until graph is built by csp thread
elapsed = 0
while not self.running and not self.graph_build_failed:
# FIXME ugly
sleep(0.5)
elapsed += 0.5
if elapsed >= timeout:
log.critical("Timeout during startup of graph, shutting down")
raise RuntimeError("Graph start timeout")
if not self.output.is_alive():
log.critical("Graph start failure")
raise RuntimeError("Graph start failure")
if self.graph_build_failed:
log.critical("Startup of graph failed, shutting down")
raise RuntimeError("Graph build failure")
# Revisit each module and connect to rest, if necessary
for module in self.modules:
if not module.disable:
module.rest(web_app)
web_app._finalize()
return web_app
def stop(self, **kwargs) -> Any:
log.warning("Shutting down gateway...")
if isinstance(self.output, csp.impl.wiring.threaded_runtime.ThreadRunner):
# stop csp
kwargs["user_initiated"] = True
return self._shutdown(**kwargs)
else:
raise Exception("Gateway can only be stopped if started with `block=False`")
def _shutdown(self, user_initiated: bool = True, wait: bool = False):
# Now run through shutdown routine
log.warning("Initiating shutdown...")
if not isinstance(self.output, csp.impl.wiring.threaded_runtime.ThreadRunner):
# nothing more to do
return self.output
if not wait:
if self.output.is_alive():
# First, try to stop the CSP thread if its alive
try:
self.output.stop_engine()
except Exception:
# If there was an error stopping it, we have an unclean shutdown
log.exception("CSP `stop_engine` exception")
raise
# Now join the csp thread, allowing it to raise
try:
ret = self.output.join(suppress=False)
except Exception:
log.exception("CSP exception detected:")
raise
# Invoke shutdown for each of the modules in parallel
# NOTE: Modules should shutdown independently of each other
log.warning("Shutting down modules...")
pool = multiprocessing.pool.ThreadPool()
result = pool.map_async(lambda mod: mod.shutdown(), self.modules)
# Wait for modules to shutdown safely, all modules should shutdown within this time
try:
result.get(timeout=self._module_shutdown_timeout)
except multiprocessing.TimeoutError:
log.warning(f"Shutting down modules took more than {self._module_shutdown_timeout}, forcefully shutting down...")
# NOTE: Avoid having long running jobs in shutdown since python does not allow
# forcefull termination of running threads. Therefore the threads will keep runnning
# until they complete or the parent process dies
pool.terminate()
# Now run through shutdown routine
log.warning("Shutting down webserver...")
if ret is not None:
# return value to caller
log.warning("Returning value to user")
return ret
# No output from graph
log.warning("No graph outputs detected, initiating hard shutdown")
# else exit
if not self._in_test:
if user_initiated:
log.warning("Shutting down webserver - CLEAN")
# try clean
os._exit(0)
# should not get here
sleep(5)
else:
log.warning("Shutting down webserver - UNCLEAN")
# try exit
os._exit(1)
sleep(5)
# NOTE: should be unreachable
log.warning("Shutting down webserver - SIGTERM")
# force sigkill to self
os.kill(os.getpid(), signal.SIGKILL)
def __hash__(self):
# So that csp doesn't complain about inability to memoize
return id(self)