provozni zaloha
This commit is contained in:
@@ -34,12 +34,10 @@ from datetime import datetime
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).parent))
|
||||
from tasks_common import LOG, TASKS, log, parse_frontmatter
|
||||
from tasks_common import CONFIG, LOG, TASKS, ensure_queue_dirs, log, parse_frontmatter
|
||||
|
||||
from nanobot import Nanobot
|
||||
|
||||
CONFIG = Path.home() / ".nanobot" / "config.json"
|
||||
|
||||
TIMEOUT_SECONDS = 20 * 60
|
||||
|
||||
|
||||
@@ -74,6 +72,49 @@ async def run_agent(goal: str, session_key: str, preset: str | None = None) -> s
|
||||
return result.content or ""
|
||||
|
||||
|
||||
def finalize_task(
|
||||
running_path: Path,
|
||||
content: str,
|
||||
fm: dict[str, str],
|
||||
slug: str,
|
||||
result_text: str,
|
||||
status: str,
|
||||
outcome: str,
|
||||
started: datetime | None,
|
||||
) -> None:
|
||||
completed = datetime.now().astimezone()
|
||||
duration_s = int((completed - started).total_seconds()) if started else 0
|
||||
appended = (
|
||||
f"{content}\n\n# Result\n\n{result_text}\n\n"
|
||||
f"---\ncompleted: {completed.isoformat()}\n"
|
||||
f"duration_seconds: {duration_s}\nstatus: {status}\n"
|
||||
)
|
||||
running_path.write_text(appended)
|
||||
shutil.move(running_path, TASKS / status / running_path.name)
|
||||
|
||||
try:
|
||||
notify_chat_id, notify_source = resolve_telegram_chat_id(fm)
|
||||
except Exception as e:
|
||||
log(f"NOTIFY-RESOLVE-FAILED {running_path.name}: {e}")
|
||||
notify_chat_id = None
|
||||
|
||||
lines = result_text.strip().splitlines()
|
||||
summary_line = lines[0][:200] if lines else "(prázdný výstup)"
|
||||
msg = (
|
||||
f"{outcome}: {slug}\n\n"
|
||||
f"{summary_line}\n\n"
|
||||
f"V chatu si vyžádej plný report: výsledek {slug}"
|
||||
)
|
||||
if notify_chat_id:
|
||||
try:
|
||||
telegram_send(notify_chat_id, msg)
|
||||
log(f"NOTIFY {running_path.name} chat={notify_chat_id} source={notify_source}")
|
||||
except Exception as e:
|
||||
log(f"NOTIFY-FAILED {running_path.name}: {e}")
|
||||
|
||||
log(f"END {running_path.name} status={status} duration={duration_s}s")
|
||||
|
||||
|
||||
def process_task(path: Path) -> None:
|
||||
try:
|
||||
content = path.read_text()
|
||||
@@ -88,7 +129,6 @@ def process_task(path: Path) -> None:
|
||||
shutil.move(path, TASKS / "failed" / path.name)
|
||||
return
|
||||
|
||||
notify_chat_id, notify_source = resolve_telegram_chat_id(fm)
|
||||
slug = fm.get("slug", path.stem)
|
||||
preset = fm.get("model")
|
||||
running = TASKS / "running" / path.name
|
||||
@@ -116,40 +156,34 @@ def process_task(path: Path) -> None:
|
||||
outcome = "❌ Selhalo"
|
||||
log(f"EXCEPTION {path.name}: {e}")
|
||||
|
||||
completed = datetime.now().astimezone()
|
||||
duration_s = int((completed - started).total_seconds())
|
||||
appended = (
|
||||
f"{content}\n\n# Result\n\n{result_text}\n\n"
|
||||
f"---\ncompleted: {completed.isoformat()}\n"
|
||||
f"duration_seconds: {duration_s}\nstatus: {status}\n"
|
||||
)
|
||||
running.write_text(appended)
|
||||
finalize_task(running, content, fm, slug, result_text, status, outcome, started)
|
||||
|
||||
target_dir = TASKS / status
|
||||
shutil.move(running, target_dir / path.name)
|
||||
|
||||
# Telegram notifikace — vždy přes Telegram, chat_id buď z frontmatteru
|
||||
# (Telegram session) nebo z fallback configu (WebUI / CLI / atd.).
|
||||
lines = result_text.strip().splitlines()
|
||||
summary_line = lines[0][:200] if lines else "(prázdný výstup)"
|
||||
msg = (
|
||||
f"{outcome}: `{slug}`\n\n"
|
||||
f"{summary_line}\n\n"
|
||||
f"V chatu si vyžádej plný report: `výsledek {slug}`"
|
||||
)
|
||||
try:
|
||||
telegram_send(notify_chat_id, msg)
|
||||
log(f"NOTIFY {path.name} chat={notify_chat_id} source={notify_source}")
|
||||
except Exception as e:
|
||||
log(f"NOTIFY-FAILED {path.name}: {e}")
|
||||
|
||||
log(f"END {path.name} status={status} duration={duration_s}s")
|
||||
def reclaim_orphans() -> None:
|
||||
running = TASKS / "running"
|
||||
if not running.exists():
|
||||
return
|
||||
for path in sorted(running.glob("*.md")):
|
||||
try:
|
||||
content = path.read_text()
|
||||
except Exception as e:
|
||||
log(f"RECLAIM-READ-FAILED {path.name}: {e}")
|
||||
shutil.move(path, TASKS / "failed" / path.name)
|
||||
continue
|
||||
fm, _ = parse_frontmatter(content)
|
||||
slug = fm.get("slug", path.stem)
|
||||
finalize_task(
|
||||
path, content, fm, slug,
|
||||
"(INTERRUPTED: daemon restarted while task was running)",
|
||||
"failed", "⚠️ Přerušeno", None,
|
||||
)
|
||||
log(f"RECLAIM {path.name}")
|
||||
|
||||
|
||||
def main() -> int:
|
||||
for d in ("new", "inbox", "running", "done", "failed"):
|
||||
(TASKS / d).mkdir(parents=True, exist_ok=True)
|
||||
ensure_queue_dirs()
|
||||
LOG.parent.mkdir(parents=True, exist_ok=True)
|
||||
reclaim_orphans()
|
||||
|
||||
inbox = TASKS / "inbox"
|
||||
tasks = sorted(inbox.glob("*.md"))
|
||||
|
||||
Reference in New Issue
Block a user