Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f674f2ed6c |
File diff suppressed because one or more lines are too long
@@ -18,7 +18,6 @@ NOTIFICATION_TYPES = [
|
|||||||
{"key": "reminder", "label": "Reminders", "description": "A reminder or scheduled task you asked Devii to run fires"},
|
{"key": "reminder", "label": "Reminders", "description": "A reminder or scheduled task you asked Devii to run fires"},
|
||||||
{"key": "harvest_stolen", "label": "Farm raids", "description": "Someone steals a ready build from your Code Farm"},
|
{"key": "harvest_stolen", "label": "Farm raids", "description": "Someone steals a ready build from your Code Farm"},
|
||||||
{"key": "award", "label": "Awards", "description": "Someone gives you an award on your profile"},
|
{"key": "award", "label": "Awards", "description": "Someone gives you an award on your profile"},
|
||||||
{"key": "admin_alert", "label": "Admin alerts", "description": "System alerts requiring admin attention"},
|
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -23,7 +23,6 @@ from devplacepy.utils import (
|
|||||||
require_user,
|
require_user,
|
||||||
)
|
)
|
||||||
from devplacepy.services.audit import record as audit
|
from devplacepy.services.audit import record as audit
|
||||||
from devplacepy.services.devii.quota import notify_admins_quota_blocked
|
|
||||||
|
|
||||||
logger = logging.getLogger("devii.router")
|
logger = logging.getLogger("devii.router")
|
||||||
|
|
||||||
@@ -280,7 +279,6 @@ async def devii_ws(websocket: WebSocket):
|
|||||||
summary=f"AI request by {username or owner_id} blocked - 24h quota reached",
|
summary=f"AI request by {username or owner_id} blocked - 24h quota reached",
|
||||||
metadata={"limit_usd": limit, "owner_kind": owner_kind},
|
metadata={"limit_usd": limit, "owner_kind": owner_kind},
|
||||||
)
|
)
|
||||||
notify_admins_quota_blocked(owner_kind, owner_id, username)
|
|
||||||
await websocket.send_json(
|
await websocket.send_json(
|
||||||
{
|
{
|
||||||
"type": "error",
|
"type": "error",
|
||||||
|
|||||||
@@ -1,26 +0,0 @@
|
|||||||
# retoor <retoor@molodetz.nl>
|
|
||||||
|
|
||||||
import logging
|
|
||||||
|
|
||||||
from devplacepy.database import get_table
|
|
||||||
from devplacepy.utils.notifications import create_notification
|
|
||||||
|
|
||||||
logger = logging.getLogger("devii.quota")
|
|
||||||
|
|
||||||
ADMIN_ALERT_TYPE = "admin_alert"
|
|
||||||
|
|
||||||
|
|
||||||
def notify_admins_quota_blocked(owner_kind: str, owner_id: str, username: str) -> None:
|
|
||||||
if owner_kind != "user":
|
|
||||||
return
|
|
||||||
admins = get_table("users").find(role="Admin")
|
|
||||||
for admin in admins:
|
|
||||||
if admin["uid"] == owner_id:
|
|
||||||
continue
|
|
||||||
create_notification(
|
|
||||||
admin["uid"],
|
|
||||||
ADMIN_ALERT_TYPE,
|
|
||||||
f"User {username} blocked - 24h AI quota reached",
|
|
||||||
owner_id,
|
|
||||||
"/admin/users",
|
|
||||||
)
|
|
||||||
@@ -10,7 +10,6 @@ from typing import Any
|
|||||||
|
|
||||||
from devplacepy.database import get_table
|
from devplacepy.database import get_table
|
||||||
from devplacepy.services.audit import record as audit
|
from devplacepy.services.audit import record as audit
|
||||||
from devplacepy.services.devii.quota import notify_admins_quota_blocked
|
|
||||||
from devplacepy.services.manager import service_manager
|
from devplacepy.services.manager import service_manager
|
||||||
from devplacepy.utils import is_admin, is_primary_admin
|
from devplacepy.utils import is_admin, is_primary_admin
|
||||||
|
|
||||||
@@ -401,7 +400,6 @@ class TelegramBridge:
|
|||||||
summary=f"Telegram AI request by {user.get('username', owner_id)} blocked - 24h quota reached",
|
summary=f"Telegram AI request by {user.get('username', owner_id)} blocked - 24h quota reached",
|
||||||
metadata={"limit_usd": limit},
|
metadata={"limit_usd": limit},
|
||||||
)
|
)
|
||||||
notify_admins_quota_blocked("user", owner_id, user.get("username", ""))
|
|
||||||
await self._service.send(
|
await self._service.send(
|
||||||
chat_id, "Your daily AI quota is reached (100%). Please try again later."
|
chat_id, "Your daily AI quota is reached (100%). Please try again later."
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -2,8 +2,10 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import socket
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
|
import time
|
||||||
|
|
||||||
from devplacepy.config import XMLRPC_BIND, XMLRPC_PORT
|
from devplacepy.config import XMLRPC_BIND, XMLRPC_PORT
|
||||||
from devplacepy.services.base import BaseService
|
from devplacepy.services.base import BaseService
|
||||||
@@ -25,20 +27,48 @@ class XmlrpcService(BaseService):
|
|||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
super().__init__("xmlrpc", interval_seconds=XMLRPC_INTERVAL_SECONDS)
|
super().__init__("xmlrpc", interval_seconds=XMLRPC_INTERVAL_SECONDS)
|
||||||
self._process: subprocess.Popen | None = None
|
self._process: subprocess.Popen | None = None
|
||||||
|
self._last_stderr: str | None = None
|
||||||
|
|
||||||
def _alive(self) -> bool:
|
def _alive(self) -> bool:
|
||||||
return self._process is not None and self._process.poll() is None
|
return self._process is not None and self._process.poll() is None
|
||||||
|
|
||||||
def _spawn(self) -> None:
|
def _spawn(self) -> None:
|
||||||
|
self._last_stderr = None
|
||||||
self._process = subprocess.Popen(
|
self._process = subprocess.Popen(
|
||||||
[sys.executable, "-m", SERVER_MODULE],
|
[sys.executable, "-m", SERVER_MODULE],
|
||||||
stdout=subprocess.DEVNULL,
|
stdout=subprocess.DEVNULL,
|
||||||
stderr=subprocess.DEVNULL,
|
stderr=subprocess.PIPE,
|
||||||
)
|
)
|
||||||
self.log(
|
self.log(
|
||||||
f"Forking XML-RPC server started (pid {self._process.pid}) on "
|
f"Forking XML-RPC server spawning (pid {self._process.pid}) on "
|
||||||
f"{XMLRPC_BIND}:{XMLRPC_PORT}"
|
f"{XMLRPC_BIND}:{XMLRPC_PORT}"
|
||||||
)
|
)
|
||||||
|
time.sleep(0.5)
|
||||||
|
if self._process.poll() is not None:
|
||||||
|
stderr_data = self._process.communicate()[1]
|
||||||
|
if stderr_data:
|
||||||
|
self._last_stderr = stderr_data.decode("utf-8", errors="replace")
|
||||||
|
self.log(
|
||||||
|
f"Forking XML-RPC server died after spawn (pid {self._process.pid}, "
|
||||||
|
f"exit code {self._process.returncode})"
|
||||||
|
)
|
||||||
|
if self._last_stderr:
|
||||||
|
for line in self._last_stderr.strip().split("\n"):
|
||||||
|
self.log(f" stderr: {line}")
|
||||||
|
return
|
||||||
|
try:
|
||||||
|
sock = socket.create_connection((XMLRPC_BIND, XMLRPC_PORT), timeout=1)
|
||||||
|
sock.close()
|
||||||
|
except (OSError, socket.timeout):
|
||||||
|
self.log(
|
||||||
|
f"Forking XML-RPC server not yet ready (pid {self._process.pid}) "
|
||||||
|
f"on {XMLRPC_BIND}:{XMLRPC_PORT}"
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
self.log(
|
||||||
|
f"Forking XML-RPC server started (pid {self._process.pid}) on "
|
||||||
|
f"{XMLRPC_BIND}:{XMLRPC_PORT}"
|
||||||
|
)
|
||||||
|
|
||||||
def _terminate(self) -> None:
|
def _terminate(self) -> None:
|
||||||
if not self._alive():
|
if not self._alive():
|
||||||
@@ -65,6 +95,13 @@ class XmlrpcService(BaseService):
|
|||||||
self.log(f"XML-RPC server healthy (pid {self._process.pid})")
|
self.log(f"XML-RPC server healthy (pid {self._process.pid})")
|
||||||
return
|
return
|
||||||
self.log("XML-RPC server not running, starting it")
|
self.log("XML-RPC server not running, starting it")
|
||||||
|
if self._process is not None:
|
||||||
|
self.log(
|
||||||
|
f"Previous process exited with code {self._process.returncode}"
|
||||||
|
)
|
||||||
|
if self._last_stderr:
|
||||||
|
for line in self._last_stderr.strip().split("\n"):
|
||||||
|
self.log(f" last stderr: {line}")
|
||||||
self._spawn()
|
self._spawn()
|
||||||
|
|
||||||
def collect_metrics(self) -> dict:
|
def collect_metrics(self) -> dict:
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
import sys
|
||||||
from socketserver import ForkingMixIn
|
from socketserver import ForkingMixIn
|
||||||
from xmlrpc.server import SimpleXMLRPCDispatcher, SimpleXMLRPCRequestHandler, SimpleXMLRPCServer
|
from xmlrpc.server import SimpleXMLRPCDispatcher, SimpleXMLRPCRequestHandler, SimpleXMLRPCServer
|
||||||
|
|
||||||
@@ -116,6 +117,9 @@ def main() -> None:
|
|||||||
server.serve_forever()
|
server.serve_forever()
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
logger.info("XML-RPC server interrupted")
|
logger.info("XML-RPC server interrupted")
|
||||||
|
except Exception:
|
||||||
|
logging.exception("XML-RPC server crashed with unhandled exception")
|
||||||
|
sys.exit(1)
|
||||||
finally:
|
finally:
|
||||||
server.server_close()
|
server.server_close()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user