Files
buryan-infra/ops/events/adapter_plugins/gitea.py
T
architect 506ae58410
lint / hygiene (pull_request) Failing after 17s
standard / check (pull_request) Successful in 7s
lint / hygiene (push) Failing after 1m11s
standard / check (push) Successful in 8s
[STAGE4] Adapter SDK, 4 adapters, contract tests, preview
2026-11-27 20:40:19 +03:00

39 lines
1.3 KiB
Python

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