mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-07-18 01:31:02 +02:00
New API emerging
This commit is contained in:
parent
df6a11bb86
commit
e66c06f99a
4 changed files with 269 additions and 52 deletions
|
|
@ -5,13 +5,14 @@ using the API.
|
|||
"""
|
||||
|
||||
from pulsar.schema import JsonSchema
|
||||
from prometheus_client import Histogram, Counter
|
||||
|
||||
from trustgraph.schema import ConfigRequest, ConfigResponse, ConfigPush
|
||||
from trustgraph.schema import ConfigValue, Error
|
||||
from trustgraph.schema import config_request_queue, config_response_queue
|
||||
from trustgraph.schema import config_push_queue
|
||||
from trustgraph.log_level import LogLevel
|
||||
from trustgraph.base import ConsumerProducer
|
||||
from trustgraph.base import ConsumerProducer, BaseProcessor
|
||||
|
||||
module = "config-svc"
|
||||
|
||||
|
|
@ -33,30 +34,67 @@ class Configuration(dict):
|
|||
self[key] = ConfigurationItems()
|
||||
return dict.__getitem__(self, key)
|
||||
|
||||
class Processor(ConsumerProducer):
|
||||
class Processor(BaseProcessor):
|
||||
|
||||
def __init__(self, **params):
|
||||
|
||||
|
||||
input_queue = params.get("input_queue", default_input_queue)
|
||||
output_queue = params.get("output_queue", default_output_queue)
|
||||
push_queue = params.get("push_queue", default_push_queue)
|
||||
subscriber = params.get("subscriber", default_subscriber)
|
||||
|
||||
super(Processor, self).__init__(
|
||||
**params | {
|
||||
|
||||
input_schema = ConfigRequest
|
||||
output_schema = ConfigResponse
|
||||
push_schema = ConfigResponse
|
||||
|
||||
self.set_processor_state("FIXME", "starting")
|
||||
|
||||
self.set_pubsub_info(
|
||||
"FIXME",
|
||||
{
|
||||
"input_queue": input_queue,
|
||||
"output_queue": output_queue,
|
||||
"push_queue": output_queue,
|
||||
"subscriber": subscriber,
|
||||
"input_schema": ConfigRequest,
|
||||
"output_schema": ConfigResponse,
|
||||
"push_schema": ConfigPush,
|
||||
"input_schema": input_schema.__name__,
|
||||
# "rate_limit_retry": str(self.rate_limit_retry),
|
||||
# "rate_limit_timeout": str(self.rate_limit_timeout),
|
||||
}
|
||||
)
|
||||
|
||||
self.push_prod = self.client.create_producer(
|
||||
topic=push_queue,
|
||||
schema=JsonSchema(ConfigPush),
|
||||
super(Processor, self).__init__(
|
||||
**params | {
|
||||
"input_schema": input_schema.__name__,
|
||||
"output_schema": output_schema.__name__,
|
||||
"push_schema": push_schema.__name__,
|
||||
}
|
||||
)
|
||||
|
||||
self.subs = self.subscribe(
|
||||
input_queue=input_queue,
|
||||
subscriber=subscriber,
|
||||
schema=input_schema,
|
||||
handler=self.on_message,
|
||||
)
|
||||
|
||||
if not hasattr(__class__, "request_metric"):
|
||||
__class__.request_metric = Histogram(
|
||||
'request_latency', 'Request latency (seconds)'
|
||||
)
|
||||
|
||||
if not hasattr(__class__, "processing_metric"):
|
||||
__class__.processing_metric = Counter(
|
||||
'processing_count', 'Processing count',
|
||||
["status"]
|
||||
)
|
||||
|
||||
self.push_pub = self.publish(
|
||||
output_queue = push_queue,
|
||||
schema = ConfigPush
|
||||
)
|
||||
|
||||
self.out_pub = self.publish(
|
||||
output_queue = output_queue,
|
||||
schema = ConfigResponse
|
||||
)
|
||||
|
||||
# FIXME: The state is held internally. This only works if there's
|
||||
|
|
@ -67,8 +105,11 @@ class Processor(ConsumerProducer):
|
|||
# Version counter
|
||||
self.version = 0
|
||||
|
||||
print("Service initialised.")
|
||||
|
||||
async def start(self):
|
||||
await self.push()
|
||||
await self.subs.start()
|
||||
|
||||
async def handle_get(self, v, id):
|
||||
|
||||
|
|
@ -226,10 +267,12 @@ class Processor(ConsumerProducer):
|
|||
config = self.config,
|
||||
error = None,
|
||||
)
|
||||
self.push_prod.send(resp)
|
||||
|
||||
await self.push_pub.send(resp)
|
||||
|
||||
print("Pushed.")
|
||||
|
||||
async def handle(self, msg):
|
||||
async def on_message(self, msg, consumer):
|
||||
|
||||
v = msg.value()
|
||||
|
||||
|
|
@ -276,9 +319,9 @@ class Processor(ConsumerProducer):
|
|||
)
|
||||
)
|
||||
|
||||
await self.send(resp, properties={"id": id})
|
||||
await self.out_pub.send(resp, properties={"id": id})
|
||||
|
||||
self.consumer.acknowledge(msg)
|
||||
consumer.acknowledge(msg)
|
||||
|
||||
except Exception as e:
|
||||
|
||||
|
|
@ -289,8 +332,8 @@ class Processor(ConsumerProducer):
|
|||
),
|
||||
text=None,
|
||||
)
|
||||
await self.send(resp, properties={"id": id})
|
||||
self.consumer.acknowledge(msg)
|
||||
await self.out_pub.send(resp, properties={"id": id})
|
||||
consumer.acknowledge(msg)
|
||||
|
||||
@staticmethod
|
||||
def add_args(parser):
|
||||
|
|
@ -300,6 +343,12 @@ class Processor(ConsumerProducer):
|
|||
default_output_queue,
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
'-i', '--input-queue',
|
||||
default=default_input_queue,
|
||||
help=f'Input queue (default: {default_input_queue})'
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
'-q', '--push-queue',
|
||||
default=default_push_queue,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue