Files
leocrm/app/plugins/builtins/automation/jobs.py
T
Agent Zero abbe7a18fc fix(audit): P0-P3 audit fixes — 838 ruff errors → 0, 30 F821 bugs fixed, 118 files changed
- P0: hooks.py 3-tuple fix, trigger_dispatcher Contract, contacts/plugin unregister_actions_by_owner
- P0: 5 test files — check_permission mocks removed, hardcoded DB credential → env var
- P1: attachment_service DmsFile via Contract helper, restore_registry/history_hooks dedup
- P1: mail/plugin restore unregister, mcp_client datetime.now(UTC), saved_views/filters patterns
- P1: ProtectedRoute fail-closed, 13 test assertion fixes (bcrypt, DB-URLs, SECRET_KEYs)
- P2: deprecated notifications → post_system_message (3 files), forgejo Base, report_generator lazy import
- P2: webhooks permissions, deps.py/roles.py plugin perms removed, import_export default
- P2: address/tags/entity_links patterns removed, worker.py Contract-Umgehungen fixed
- P2: 28 frontend TODOs (hardcoded constants, deprecated notification API)
- P3: dead code, duplicates, deprecated imports, private attr, __import__ inline
- P3: 8 frontend TODOs (LucideIcons, inline styles, XSS, i18n)
- ruff: 838 → 0 (612 auto-fix + 246 manual + 27 F821 regression fix)
- F821: 30 → 0 (AutomationDefinition, DmsFile, user_id, Path, Any, String)
- Contract-Umgehungen: 2 neue gefunden (worker.py:169, worker.py:280) und gefixt
2026-08-16 01:17:18 +02:00

110 lines
3.9 KiB
Python

"""ARQ background jobs for the Automation plugin."""
from __future__ import annotations
import logging
from typing import Any
from app.core.db import get_session_factory
logger = logging.getLogger(__name__)
async def backup_check(ctx: dict[str, Any]) -> None:
"""Check if backups are current and publish backup events.
Runs daily at 2:00. Checks the last backup timestamp from system settings
and publishes backup.completed or backup.failed events accordingly.
"""
from sqlalchemy import text
from app.core.event_bus import get_event_bus
event_bus = get_event_bus()
factory = get_session_factory()
async with factory() as db:
try:
# Check last backup timestamp from system settings
result = await db.execute(
text("SELECT value FROM system_settings WHERE key = 'last_backup_at' LIMIT 1")
)
row = result.mappings().first()
if row and row["value"]:
logger.info("Backup check: last backup at %s", row["value"])
# Publish backup.completed event
await event_bus.publish('backup.completed', {
'tenant_id': None,
'user_id': None,
'timestamp': row["value"],
'title': 'Backup erfolgreich',
'body': f'Letztes Backup: {row["value"]}',
})
else:
logger.warning("Backup check: no backup timestamp found")
await event_bus.publish('backup.failed', {
'tenant_id': None,
'user_id': None,
'title': 'Backup fehlgeschlagen',
'body': 'Kein Backup-Zeitstempel gefunden',
})
except Exception:
logger.exception("Backup check failed")
await event_bus.publish('backup.failed', {
'tenant_id': None,
'user_id': None,
'title': 'Backup fehlgeschlagen',
'body': 'Backup-Check fehlgeschlagen',
})
logger.info("Backup check complete")
async def search_index_check(ctx: dict[str, Any]) -> None:
"""Check if search index is current and log status.
Runs daily at 3:00. Checks for entities without embeddings
and logs the count. Publishes status via logging only.
"""
from sqlalchemy import text
factory = get_session_factory()
async with factory() as db:
try:
# Check for contacts without embeddings
result = await db.execute(
text("SELECT COUNT(*) as cnt FROM contacts WHERE deleted_at IS NULL AND embedding IS NULL")
)
row = result.mappings().first()
contacts_missing = row["cnt"] if row else 0
# Check for mails without embeddings
result = await db.execute(
text("SELECT COUNT(*) as cnt FROM mails WHERE embedding IS NULL")
)
row = result.mappings().first()
mails_missing = row["cnt"] if row else 0
# Check for files without embeddings
result = await db.execute(
text("SELECT COUNT(*) as cnt FROM files WHERE deleted_at IS NULL AND embedding IS NULL")
)
row = result.mappings().first()
files_missing = row["cnt"] if row else 0
total_missing = contacts_missing + mails_missing + files_missing
if total_missing > 0:
logger.warning(
"Search index check: %d entities missing embeddings (contacts=%d, mails=%d, files=%d)",
total_missing, contacts_missing, mails_missing, files_missing,
)
else:
logger.info("Search index check: all entities have embeddings")
except Exception:
logger.exception("Search index check failed")
logger.info("Search index check complete")