mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-07-19 18:21:03 +02:00
- Added config reload handler mechanism, calls a Python method on config
load - Set config namespace subscription expiry to 30 mins, to clean up, since config subscriptions are per-instance, there is many of them and they get a unique ID on instance restart
This commit is contained in:
parent
88eae0a9f0
commit
f1e9bee56a
3 changed files with 60 additions and 5 deletions
|
|
@ -3,12 +3,18 @@ import asyncio
|
||||||
import os
|
import os
|
||||||
import argparse
|
import argparse
|
||||||
import pulsar
|
import pulsar
|
||||||
|
from pulsar.schema import JsonSchema
|
||||||
import _pulsar
|
import _pulsar
|
||||||
import time
|
import time
|
||||||
|
import uuid
|
||||||
from prometheus_client import start_http_server, Info
|
from prometheus_client import start_http_server, Info
|
||||||
|
|
||||||
|
from .. schema import ConfigPush, config_push_queue
|
||||||
from .. log_level import LogLevel
|
from .. log_level import LogLevel
|
||||||
|
|
||||||
|
default_config_queue = config_push_queue
|
||||||
|
config_subscriber_id = str(uuid.uuid4())
|
||||||
|
|
||||||
class BaseProcessor:
|
class BaseProcessor:
|
||||||
|
|
||||||
default_pulsar_host = os.getenv("PULSAR_HOST", 'pulsar://pulsar:6650')
|
default_pulsar_host = os.getenv("PULSAR_HOST", 'pulsar://pulsar:6650')
|
||||||
|
|
@ -34,6 +40,11 @@ class BaseProcessor:
|
||||||
pulsar_api_key = params.get("pulsar_api_key", None)
|
pulsar_api_key = params.get("pulsar_api_key", None)
|
||||||
log_level = params.get("log_level", LogLevel.INFO)
|
log_level = params.get("log_level", LogLevel.INFO)
|
||||||
|
|
||||||
|
self.config_push_queue = params.get(
|
||||||
|
"config_push_queue",
|
||||||
|
default_config_queue
|
||||||
|
)
|
||||||
|
|
||||||
self.pulsar_host = pulsar_host
|
self.pulsar_host = pulsar_host
|
||||||
self.pulsar_api_key = pulsar_api_key
|
self.pulsar_api_key = pulsar_api_key
|
||||||
|
|
||||||
|
|
@ -53,6 +64,12 @@ class BaseProcessor:
|
||||||
|
|
||||||
self.pulsar_listener = pulsar_listener
|
self.pulsar_listener = pulsar_listener
|
||||||
|
|
||||||
|
self.config_subscriber = self.client.subscribe(
|
||||||
|
self.config_push_queue, config_subscriber_id,
|
||||||
|
consumer_type=pulsar.ConsumerType.Shared,
|
||||||
|
schema=JsonSchema(ConfigPush),
|
||||||
|
)
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
|
|
||||||
if hasattr(self, "client"):
|
if hasattr(self, "client"):
|
||||||
|
|
@ -74,6 +91,12 @@ class BaseProcessor:
|
||||||
help=f'Pulsar API key',
|
help=f'Pulsar API key',
|
||||||
)
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
'--config-push-queue',
|
||||||
|
default=default_config_queue,
|
||||||
|
help=f'Config push queue {default_config_queue}',
|
||||||
|
)
|
||||||
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--pulsar-listener',
|
'--pulsar-listener',
|
||||||
help=f'Pulsar listener (default: none)',
|
help=f'Pulsar listener (default: none)',
|
||||||
|
|
@ -104,14 +127,44 @@ class BaseProcessor:
|
||||||
async def start(self):
|
async def start(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
async def run_config_queue(self):
|
||||||
|
|
||||||
|
if self.module == "config.service":
|
||||||
|
print("I am config-svc, not looking at config queue", flush=True)
|
||||||
|
return
|
||||||
|
|
||||||
|
print("Config thread running", flush=True)
|
||||||
|
|
||||||
|
while True:
|
||||||
|
|
||||||
|
try:
|
||||||
|
msg = await asyncio.to_thread(
|
||||||
|
self.config_subscriber.receive, timeout_millis=2000
|
||||||
|
)
|
||||||
|
except pulsar.Timeout:
|
||||||
|
continue
|
||||||
|
|
||||||
|
v = msg.value()
|
||||||
|
print("Got config version", v.version, flush=True)
|
||||||
|
|
||||||
|
await self.on_config(v.version, v.config)
|
||||||
|
|
||||||
|
async def on_config(self, version, config):
|
||||||
|
pass
|
||||||
|
|
||||||
async def run(self):
|
async def run(self):
|
||||||
raise RuntimeError("Something should have implemented the run method")
|
raise RuntimeError("Something should have implemented the run method")
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def launch_async(cls, args):
|
async def launch_async(cls, args, prog):
|
||||||
p = cls(**args)
|
p = cls(**args)
|
||||||
|
p.module = prog
|
||||||
await p.start()
|
await p.start()
|
||||||
await p.run()
|
|
||||||
|
task1 = asyncio.create_task(p.run_config_queue())
|
||||||
|
task2 = asyncio.create_task(p.run())
|
||||||
|
|
||||||
|
await asyncio.gather(task1, task2)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def launch(cls, prog, doc):
|
def launch(cls, prog, doc):
|
||||||
|
|
@ -135,7 +188,7 @@ class BaseProcessor:
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
||||||
asyncio.run(cls.launch_async(args))
|
asyncio.run(cls.launch_async(args, prog))
|
||||||
|
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print("Keyboard interrupt.")
|
print("Keyboard interrupt.")
|
||||||
|
|
|
||||||
|
|
@ -82,7 +82,7 @@ class Consumer(BaseProcessor):
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
|
|
||||||
msg = self.consumer.receive()
|
msg = await asyncio.to_thread(self.consumer.receive)
|
||||||
|
|
||||||
expiry = time.time() + self.rate_limit_timeout
|
expiry = time.time() + self.rate_limit_timeout
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -76,13 +76,15 @@ def init(url, tenant="tg"):
|
||||||
"retention_policies": {
|
"retention_policies": {
|
||||||
"retentionSizeInMB": -1,
|
"retentionSizeInMB": -1,
|
||||||
"retentionTimeInMinutes": 3,
|
"retentionTimeInMinutes": 3,
|
||||||
|
"subscription_expiration_time_minutes": 30,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
ensure_namespace(url, tenant, "config", {
|
ensure_namespace(url, tenant, "config", {
|
||||||
"retention_policies": {
|
"retention_policies": {
|
||||||
"retentionSizeInMB": 50,
|
"retentionSizeInMB": 10,
|
||||||
"retentionTimeInMinutes": -1,
|
"retentionTimeInMinutes": -1,
|
||||||
|
"subscription_expiration_time_minutes": 5,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue