Merge in RR/pyinfra from 2.0.0-documentation to 2.0.0
Squashed commit of the following:
commit 7a794bdcc987631cdc4d89b5620359464e2e018e
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Mon Jul 4 13:05:26 2022 +0200
removed obsolete imports
commit 3fc6a7ef5d0172dbce1c4292d245eced2f378b5a
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Mon Jul 4 11:47:12 2022 +0200
enable docker-compose fixture
commit 36d8d3bc851b06d94cf12a73048a00a67ef79c42
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Mon Jul 4 11:46:53 2022 +0200
renaming
commit 3bf00d11cd041dff325b66f13fcd00d3ce96b8b5
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Thu Jun 30 12:47:57 2022 +0200
refactoring: added cached pipeline factory
commit 90e735852af2f86e35be845fabf28494de952edb
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Wed Jun 29 13:47:08 2022 +0200
renaming
commit 93b3d4b202b41183ed8cabe193a4bfa03f520787
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Wed Jun 29 13:25:03 2022 +0200
further refactored server setup code: moving and decomplecting
commit 8b2ed83c7ade5bd811cb045d56fbfb0353fa385e
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Wed Jun 29 12:53:09 2022 +0200
refactored server setup code: factored out and decoupled operation registry and prometheus summary registry
commit da2dce762bdd6889165fbb320dc9ee8a0bd089b2
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Tue Jun 28 19:40:04 2022 +0200
adjusted test target
commit 70df7911b9b92f4b72afd7d4b33ca2bbf136295e
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Tue Jun 28 19:32:38 2022 +0200
minor refactoring
commit 0937b63dc000346559bde353381304b273244109
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Mon Jun 27 13:59:59 2022 +0200
support for empty operation suffix
commit 5e56917970962a2e69bbd66a324bdb4618c040bd
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Mon Jun 27 12:52:36 2022 +0200
minor refactoring
commit 40665a7815ae5927b3877bda14fb77deef37d667
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Mon Jun 27 10:57:04 2022 +0200
optimization: prefix filtering via storage API for get_all_object_names
commit af0892a899d09023eb0e61eecb63e03dc2fd3b60
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Mon Jun 27 10:55:47 2022 +0200
topological sorting of definitions by caller hierarchy
101 lines
3.4 KiB
Python
101 lines
3.4 KiB
Python
from functools import singledispatch
|
|
from typing import Dict, Callable, Union
|
|
|
|
from flask import Flask, jsonify, request
|
|
from prometheus_client import generate_latest
|
|
|
|
from pyinfra.config import CONFIG
|
|
from pyinfra.server.buffering.stream import FlatStreamBuffer
|
|
from pyinfra.server.monitoring import OperationDispatcherMonitoringDecorator
|
|
from pyinfra.server.operation_dispatcher import OperationDispatcher
|
|
from pyinfra.server.stream.queued_stream_function import QueuedStreamFunction
|
|
|
|
|
|
@singledispatch
|
|
def set_up_processing_server(arg: Union[dict, Callable], buffer_size=1):
|
|
"""Produces a processing server given a streamable function or a mapping from operations to streamable functions.
|
|
Streamable functions are constructed by calling pyinfra.server.utils.make_streamable_and_wrap_in_packing_logic on a
|
|
function taking a tuple of data and metadata and also returning a tuple or yielding tuples of data and metadata.
|
|
If the function doesn't produce data, data should be an empty byte string.
|
|
If the function doesn't produce metadata, metadata should be an empty dictionary.
|
|
|
|
Args:
|
|
arg: streamable function or mapping of operations: str to streamable functions
|
|
buffer_size: If your function operates on batches this parameter controls how many items are aggregated before
|
|
your function is applied.
|
|
|
|
TODO: buffer_size has to be controllable on per function basis.
|
|
|
|
Returns:
|
|
Processing server: flask app
|
|
"""
|
|
pass
|
|
|
|
|
|
@set_up_processing_server.register
|
|
def _(operation2stream_fn: dict, buffer_size=1):
|
|
return __stream_fn_to_processing_server(operation2stream_fn, buffer_size)
|
|
|
|
|
|
@set_up_processing_server.register
|
|
def _(stream_fn: object, buffer_size=1):
|
|
operation2stream_fn = {None: stream_fn}
|
|
return __stream_fn_to_processing_server(operation2stream_fn, buffer_size)
|
|
|
|
|
|
def __stream_fn_to_processing_server(operation2stream_fn: dict, buffer_size):
|
|
operation2stream_fn = {
|
|
op: QueuedStreamFunction(FlatStreamBuffer(fn, buffer_size)) for op, fn in operation2stream_fn.items()
|
|
}
|
|
return __set_up_processing_server(operation2stream_fn)
|
|
|
|
|
|
def __set_up_processing_server(operation2function: Dict[str, QueuedStreamFunction]):
|
|
app = Flask(__name__)
|
|
|
|
dispatcher = OperationDispatcherMonitoringDecorator(
|
|
OperationDispatcher(operation2function),
|
|
naming_policy=naming_policy,
|
|
)
|
|
|
|
def ok():
|
|
resp = jsonify("OK")
|
|
resp.status_code = 200
|
|
return resp
|
|
|
|
@app.route("/ready", methods=["GET"])
|
|
def ready():
|
|
return ok()
|
|
|
|
@app.route("/health", methods=["GET"])
|
|
def healthy():
|
|
return ok()
|
|
|
|
@app.route("/prometheus", methods=["GET"])
|
|
def prometheus():
|
|
return generate_latest(registry=dispatcher.registry)
|
|
|
|
@app.route("/<operation>", methods=["POST", "PATCH"])
|
|
def submit(operation):
|
|
return dispatcher.submit(operation, request)
|
|
|
|
@app.route("/", methods=["POST", "PATCH"])
|
|
def submit_default():
|
|
return dispatcher.submit("", request)
|
|
|
|
@app.route("/<operation>", methods=["GET"])
|
|
def pickup(operation):
|
|
return dispatcher.pickup(operation)
|
|
|
|
return app
|
|
|
|
|
|
def naming_policy(op_name: str):
|
|
pop_suffix = OperationDispatcher.pickup_suffix
|
|
prefix = f"redactmanager_{CONFIG.service.name}"
|
|
|
|
op_display_name = op_name.replace(f"_{pop_suffix}", "") if op_name != pop_suffix else "default"
|
|
complete_display_name = f"{prefix}_{op_display_name}"
|
|
|
|
return complete_display_name
|