[STAGE4] Adapter SDK, 4 adapters, contract tests, preview
This commit is contained in:
1 parent
7ef5b02698
commit
506ae58410
10 files changed
+142
-2
No files matched your search
Whitespace-only changes.
@@ -0,0 +1,9 @@
|
||||
from adapters import audit360
|
||||
from .base import Adapter
|
||||
|
||||
|
||||
class Audit360Adapter(Adapter):
|
||||
name = "audit360"
|
||||
|
||||
def poll(self, conn):
|
||||
return audit360(conn)
|
||||
@@ -0,0 +1,10 @@
|
||||
"""Adapter contract for the Stage 4 event fabric."""
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
|
||||
class Adapter(ABC):
|
||||
name = "adapter"
|
||||
|
||||
@abstractmethod
|
||||
def poll(self, conn) -> int:
|
||||
"""Emit zero or more events; return number of new events."""
|
||||
@@ -0,0 +1,38 @@
|
||||
import urllib.request
|
||||
|
||||
from event_store import enqueue, event
|
||||
from .base import Adapter
|
||||
|
||||
URL = "http://192.168.1.140:3000/api/v1/version"
|
||||
|
||||
|
||||
class GiteaHealthAdapter(Adapter):
|
||||
name = "gitea-health"
|
||||
|
||||
def poll(self, conn):
|
||||
up = False
|
||||
try:
|
||||
with urllib.request.urlopen(URL, timeout=4) as r:
|
||||
up = r.status == 200
|
||||
except Exception:
|
||||
up = False
|
||||
state = "up" if up else "down"
|
||||
with conn.cursor() as cur:
|
||||
cur.execute("select cursor from events.adapter_state where adapter=%s", (self.name,))
|
||||
row = cur.fetchone()
|
||||
previous = row[0] if row and row[0] else None
|
||||
if previous == state:
|
||||
return 0
|
||||
ev = event(idempotency_key="gitea:health:%s:%d" % (state, int(__import__("time").time())),
|
||||
source="gitea", type_="system.service.%s" % state,
|
||||
subject="gitea", data={"service": "gitea", "state": state})
|
||||
enqueue(conn, ev)
|
||||
with conn.cursor() as cur:
|
||||
cur.execute("""
|
||||
insert into events.adapter_state(adapter,cursor,updated_at)
|
||||
values (%s,%s,now())
|
||||
on conflict (adapter) do update
|
||||
set cursor=excluded.cursor, updated_at=now()
|
||||
""", (self.name, state))
|
||||
conn.commit()
|
||||
return 1
|
||||
@@ -0,0 +1,9 @@
|
||||
from adapters import jellyfin_media
|
||||
from .base import Adapter
|
||||
|
||||
|
||||
class JellyfinAdapter(Adapter):
|
||||
name = "jellyfin"
|
||||
|
||||
def poll(self, conn):
|
||||
return jellyfin_media(conn)
|
||||
@@ -0,0 +1,29 @@
|
||||
"""Discover and run event adapters without editing the pipeline core."""
|
||||
import importlib
|
||||
import pkgutil
|
||||
from pathlib import Path
|
||||
|
||||
from .base import Adapter
|
||||
|
||||
|
||||
def load_adapters():
|
||||
adapters = []
|
||||
pkg_dir = Path(__file__).resolve().parent
|
||||
for mod in sorted(pkgutil.iter_modules([str(pkg_dir)])):
|
||||
if mod.name in ("base", "registry") or mod.name.startswith("_"):
|
||||
continue
|
||||
m = importlib.import_module("adapter_plugins." + mod.name)
|
||||
for obj in vars(m).values():
|
||||
if isinstance(obj, type) and issubclass(obj, Adapter) and obj is not Adapter:
|
||||
adapters.append(obj())
|
||||
return adapters
|
||||
|
||||
|
||||
def run_all(conn):
|
||||
out = {}
|
||||
for a in load_adapters():
|
||||
try:
|
||||
out[a.name] = a.poll(conn)
|
||||
except Exception as e:
|
||||
out[a.name] = "error:%s" % str(e)[:120]
|
||||
return out
|
||||
@@ -0,0 +1,9 @@
|
||||
from adapters import seerr_requests
|
||||
from .base import Adapter
|
||||
|
||||
|
||||
class SeerrAdapter(Adapter):
|
||||
name = "seerr"
|
||||
|
||||
def poll(self, conn):
|
||||
return seerr_requests(conn)
|
||||
Reference in new issue
Block a user