-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
84 lines (65 loc) · 2.2 KB
/
Copy path__init__.py
File metadata and controls
84 lines (65 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import asyncio
from collections.abc import Callable, Coroutine
from fastapi import APIRouter
from loguru import logger
try:
from lnbits.task_manager import task_manager as _task_manager
except ImportError:
# LNbits 1.5.x ships the task API as `lnbits.tasks` instead of
# `lnbits.task_manager`.
from lnbits import tasks as _legacy_tasks
_task_manager = None
from .crud import db
from .node import LISTENER_TASK_NAME, clink_listener
from .subscriptions import SUBSCRIPTIONS_TASK_NAME, clink_subscriptions
from .views import clink_ext_generic
from .views_api import clink_ext_api
from .wallet import ClinkWallet
try:
import lnbits.wallets as _lnbits_wallets
if not hasattr(_lnbits_wallets, "ClinkWallet"):
_lnbits_wallets.ClinkWallet = ClinkWallet
logger.info("clink: registered ClinkWallet as an LNbits funding source")
except Exception as exc: # pragma: no cover - defensive
logger.warning(f"clink: could not register ClinkWallet funding source: {exc}")
clink_ext: APIRouter = APIRouter(prefix="/clink", tags=["clink"])
clink_ext.include_router(clink_ext_generic)
clink_ext.include_router(clink_ext_api)
clink_static_files = [
{
"path": "/clink/static",
"name": "clink_static",
}
]
_started_tasks: dict[str, asyncio.Task] = {}
def _start_task(name: str, func: Callable[[], Coroutine]) -> None:
if _task_manager is not None:
_task_manager.create_permanent_task(func, name=name)
return
_started_tasks[name] = _legacy_tasks.create_permanent_task(func)
def _stop_task(name: str) -> None:
if _task_manager is not None:
task = _task_manager.get_task(name)
if task:
_task_manager.cancel_task(task)
return
task = _started_tasks.pop(name, None)
if task:
task.cancel()
def clink_stop():
for name in (LISTENER_TASK_NAME, SUBSCRIPTIONS_TASK_NAME):
_stop_task(name)
def clink_start():
for name, func in (
(LISTENER_TASK_NAME, clink_listener),
(SUBSCRIPTIONS_TASK_NAME, clink_subscriptions),
):
_stop_task(name)
_start_task(name, func)
__all__ = [
"clink_ext",
"clink_start",
"clink_static_files",
"clink_stop",
"db",
]