Fix runtime issues found during live testing
Some checks failed
Validate / Hassfest (push) Has been cancelled

- Fix jinja2.sandbox import: use `from jinja2.sandbox import
  SandboxedEnvironment` (dotted attribute access doesn't work)
  in templates.py, sync.py, and notifier.py
- Fix greenlet crash in tracker trigger: SQLAlchemy async sessions
  can't survive across aiohttp.ClientSession context managers.
  Eagerly load all tracker/server data before entering HTTP context.
  Split check_tracker into check_tracker (scheduler, own session)
  and check_tracker_with_session (API, reuses route session).
- Fix _check_album to accept pre-loaded params instead of tracker
  object (avoids lazy-load access after greenlet context break)

Tested end-to-end against live Immich server:
- Server connection + album browsing: OK (39 albums)
- Template creation + preview: OK
- Webhook target creation: OK
- Tracker creation + trigger: OK (initialized 4 assets)
- Second trigger: OK (no_changes detected)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-19 15:17:12 +03:00
parent 62bf15dce3
commit 3a516d6d58
6 changed files with 60 additions and 34 deletions

View File

@@ -7,6 +7,7 @@ from typing import Any
import aiohttp
import jinja2
from jinja2.sandbox import SandboxedEnvironment
from immich_watcher_core.telegram.client import TelegramClient
@@ -24,7 +25,7 @@ DEFAULT_TEMPLATE = (
def render_template(template_body: str, context: dict[str, Any]) -> str:
"""Render a Jinja2 template with the given context."""
env = jinja2.sandbox.SandboxedEnvironment(autoescape=False)
env = SandboxedEnvironment(autoescape=False)
tmpl = env.from_string(template_body)
return tmpl.render(**context)