fixed injection watcher
This commit is contained in:
@@ -1,21 +1,5 @@
|
||||
from sqlalchemy import select
|
||||
|
||||
from app.db.base import SessionLocal
|
||||
from app.db.models import ChatSession, Message
|
||||
from app.chat.notice_inbox import post_notice_to_latest_chat
|
||||
|
||||
|
||||
def post_chat_notice(content: str) -> None:
|
||||
db = SessionLocal()
|
||||
try:
|
||||
session = db.scalar(
|
||||
select(ChatSession).order_by(ChatSession.updated_at.desc()).limit(1)
|
||||
)
|
||||
if not session:
|
||||
session = ChatSession(title="Уведомления")
|
||||
db.add(session)
|
||||
db.commit()
|
||||
db.refresh(session)
|
||||
db.add(Message(session_id=session.id, role="notice", content=content))
|
||||
db.commit()
|
||||
finally:
|
||||
db.close()
|
||||
post_notice_to_latest_chat(content)
|
||||
|
||||
@@ -4,6 +4,8 @@ import random
|
||||
from datetime import datetime
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
import httpx
|
||||
|
||||
from app.config import get_settings
|
||||
from app.db.base import SessionLocal
|
||||
from app.homelab.comfyui import ComfyUIClient
|
||||
@@ -76,6 +78,15 @@ async def _tick_netdata() -> None:
|
||||
db.close()
|
||||
|
||||
|
||||
async def _comfyui_reachable(base_url: str) -> bool:
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=httpx.Timeout(3.0, connect=2.0)) as client:
|
||||
response = await client.get(f"{base_url.rstrip('/')}/system_stats")
|
||||
return response.status_code < 500
|
||||
except (httpx.TimeoutException, httpx.ConnectError, httpx.NetworkError):
|
||||
return False
|
||||
|
||||
|
||||
async def _tick_rofl() -> None:
|
||||
settings = get_settings()
|
||||
if not settings.comfyui_enabled or not settings.comfyui_rofl_enabled:
|
||||
@@ -114,8 +125,18 @@ async def _tick_rofl() -> None:
|
||||
return
|
||||
|
||||
client = ComfyUIClient()
|
||||
if not await _comfyui_reachable(client.base_url):
|
||||
return
|
||||
|
||||
prompt = client.random_rofl_prompt()
|
||||
result = await client.generate_image(prompt)
|
||||
try:
|
||||
result = await asyncio.wait_for(
|
||||
client.generate_image(prompt),
|
||||
timeout=settings.comfyui_timeout_sec + 15,
|
||||
)
|
||||
except (asyncio.TimeoutError, httpx.TimeoutException, httpx.ConnectError) as exc:
|
||||
logger.warning("Rofl image skipped (ComfyUI): %s", exc)
|
||||
return
|
||||
if not result.get("ok"):
|
||||
logger.warning("Rofl image failed: %s", result.get("error"))
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user