#!/usr/bin/env python3 """Сторож каналов modeloc: возвращает status=1 и abilities.enabled=true. Гарды new-api (и ручные чистки) ставят каналам status=0 — при этом abilities выпадают из роутинга и модель отвечает 503. Сторож возвращает их каждые 5 мин и печатает строку только когда что-то реально восстановил. """ import subprocess import sys SQL = """ WITH fixed AS ( UPDATE channels SET status=1 WHERE tag='modeloc' AND status=0 RETURNING id ), ab AS ( UPDATE abilities a SET enabled=true FROM channels c WHERE c.id=a.channel_id AND c.tag='modeloc' AND c.status=1 AND NOT a.enabled RETURNING a.channel_id ) SELECT (SELECT count(*) FROM fixed) || ' ' || (SELECT count(*) FROM ab); """ def run(sql): p = subprocess.run(["su", "postgres", "-c", "psql -d newapi -At -c \"%s\"" % sql.replace('"', '\\"')], capture_output=True, text=True, timeout=120) return (p.stdout or '').strip(), p.returncode def main(): out, rc = run(SQL) if rc != 0: print('MODELOC GUARD FAIL rc=%d' % rc, file=sys.stderr) sys.exit(1) parts = out.split() if len(parts) >= 2: ch, ab = int(parts[0] or 0), int(parts[1] or 0) if ch or ab: print('MODELOC GUARD: restored channels=%d abilities=%d' % (ch, ab)) subprocess.run(["systemctl", "restart", "new-api"], check=False, timeout=120) if __name__ == '__main__': main()