Empty skeleton mirroring the events extension layout: config/manifest, pyproject + Makefile + ruff/mypy config, FastAPI routers wired into tasks_ext, NostrClient bootstrap stubs, and an empty static/routes.json. Models, migrations, CRUD, Nostr publisher/sync, and the API/template layers land in follow-up commits. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
77 lines
2.3 KiB
Python
77 lines
2.3 KiB
Python
import asyncio
|
|
|
|
from fastapi import APIRouter
|
|
from loguru import logger
|
|
|
|
from .crud import db
|
|
from .views import tasks_generic_router
|
|
from .views_api import tasks_api_router
|
|
|
|
tasks_ext: APIRouter = APIRouter(prefix="/tasks", tags=["Tasks"])
|
|
tasks_ext.include_router(tasks_generic_router)
|
|
tasks_ext.include_router(tasks_api_router)
|
|
|
|
tasks_static_files = [
|
|
{
|
|
"path": "/tasks/static",
|
|
"name": "tasks_static",
|
|
}
|
|
]
|
|
|
|
scheduled_tasks: list[asyncio.Task] = []
|
|
|
|
# Module-level NostrClient — None when nostrclient is unavailable. Set by the
|
|
# bootstrap task in tasks_start() and read via dynamic attribute lookup from
|
|
# nostr_hooks.publish_or_delete_task_event.
|
|
nostr_client = None
|
|
|
|
|
|
def tasks_stop():
|
|
for task in scheduled_tasks:
|
|
try:
|
|
task.cancel()
|
|
except Exception as ex:
|
|
logger.warning(ex)
|
|
|
|
global nostr_client
|
|
if nostr_client:
|
|
asyncio.get_event_loop().create_task(nostr_client.stop())
|
|
|
|
|
|
def tasks_start():
|
|
from lnbits.tasks import create_permanent_unique_task
|
|
|
|
async def _start_nostr_client():
|
|
global nostr_client
|
|
await asyncio.sleep(10) # Wait for nostrclient to be ready
|
|
try:
|
|
from .nostr.nostr_client import NostrClient
|
|
|
|
nostr_client = NostrClient()
|
|
logger.info("[TASKS] Starting NostrClient for NIP-52 sync")
|
|
await nostr_client.run_forever()
|
|
except Exception as exc:
|
|
logger.warning(f"[TASKS] NostrClient failed to start: {exc}")
|
|
logger.info("[TASKS] Tasks will work without Nostr sync")
|
|
|
|
task1 = create_permanent_unique_task("ext_tasks_nostr", _start_nostr_client)
|
|
scheduled_tasks.append(task1)
|
|
|
|
async def _sync_nostr_events():
|
|
global nostr_client
|
|
await asyncio.sleep(15)
|
|
if not nostr_client:
|
|
logger.info("[TASKS] No NostrClient, skipping Nostr sync")
|
|
return
|
|
try:
|
|
from .nostr_sync import wait_for_nostr_events
|
|
|
|
await wait_for_nostr_events(nostr_client)
|
|
except Exception as exc:
|
|
logger.error(f"[TASKS] Nostr sync task failed: {exc}")
|
|
|
|
task2 = create_permanent_unique_task("ext_tasks_nostr_sync", _sync_nostr_events)
|
|
scheduled_tasks.append(task2)
|
|
|
|
|
|
__all__ = ["db", "tasks_ext", "tasks_start", "tasks_static_files", "tasks_stop"]
|