Added tasks example
This commit is contained in:
parent
a367fcdbda
commit
7b3bab096e
2 changed files with 31 additions and 2 deletions
|
|
@ -1,8 +1,10 @@
|
||||||
|
import asyncio
|
||||||
from fastapi import APIRouter
|
from fastapi import APIRouter
|
||||||
from fastapi.staticfiles import StaticFiles
|
from fastapi.staticfiles import StaticFiles
|
||||||
|
|
||||||
from lnbits.db import Database
|
from lnbits.db import Database
|
||||||
from lnbits.helpers import template_renderer
|
from lnbits.helpers import template_renderer
|
||||||
|
from lnbits.tasks import catch_everything_and_restart
|
||||||
|
|
||||||
db = Database("ext_example")
|
db = Database("ext_example")
|
||||||
|
|
||||||
|
|
@ -20,6 +22,10 @@ example_static_files = [
|
||||||
def example_renderer():
|
def example_renderer():
|
||||||
return template_renderer(["lnbits/extensions/example/templates"])
|
return template_renderer(["lnbits/extensions/example/templates"])
|
||||||
|
|
||||||
|
from .tasks import wait_for_paid_invoices
|
||||||
|
from .views import *
|
||||||
|
from .views_api import *
|
||||||
|
|
||||||
from .views import * # noqa
|
def tpos_start():
|
||||||
from .views_api import * # noqa
|
loop = asyncio.get_event_loop()
|
||||||
|
loop.create_task(catch_everything_and_restart(wait_for_paid_invoices))
|
||||||
23
lnbits/extensions/example/tasks.py
Normal file
23
lnbits/extensions/example/tasks.py
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
# views_api.py is for you API endpoints that could be hit by another service
|
||||||
|
|
||||||
|
# add your dependencies here
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
from loguru import logger
|
||||||
|
from lnbits.core.models import Payment
|
||||||
|
from lnbits.helpers import get_current_extension_name
|
||||||
|
from lnbits.tasks import register_invoice_listener
|
||||||
|
|
||||||
|
async def wait_for_paid_invoices():
|
||||||
|
invoice_queue = asyncio.Queue()
|
||||||
|
register_invoice_listener(invoice_queue, get_current_extension_name())
|
||||||
|
|
||||||
|
while True:
|
||||||
|
payment = await invoice_queue.get()
|
||||||
|
await on_invoice_paid(payment)
|
||||||
|
|
||||||
|
async def on_invoice_paid(payment: Payment) -> None:
|
||||||
|
if payment.extra.get("tag") != "example":
|
||||||
|
logger.debug(payment)
|
||||||
|
# Do something
|
||||||
|
return
|
||||||
Loading…
Add table
Add a link
Reference in a new issue