Scheduling
Okay, this might be a bit confusing: The first extension we are talking about
is no real FluidExtension. It's just an AsyncIOScheduler from
APScheduler
that gets started if you have EXT_SCHEDULING = 1 in your app config
and your app is run.
A short example
However, to provide you a short example on how to use it and to slightly introduce you in enabling and working with extensions, let's wire up a short example:
def heart(): print("Beat!")
from webfluid import Fluid
from webfluid.core.config import register_config
from webfluid.core.ext import scheduler
from fastapi.responses import HTMLResponse
from apscheduler.triggers.interval import IntervalTrigger
from extension.main import MyExtension
from fluid.jobs import heart
@register_config(10)
class MyConfig:
SESSION_COOKIE_SECURE = True
MYEXT_BAR = "crazy"
fluid = Fluid(__name__)
my_ext = MyExtension(fluid)
scheduler.add_job(heart, IntervalTrigger(seconds=5))
@fluid.get("/", response_class=HTMLResponse)
async def home():
return await fluid.render(
"index.html",
title="Hello World!",
name="my friend"
)
@fluid.get("/health")
async def health():
return {"status": "ok"}
if __name__ == "__main__":
fluid.mix()
[general]
SECRET_KEY = supersecret
[extensions]
EXT_SCHEDULING = 1
Now you can run your app using wf run app like we've shown,
and then you'll see the heart beat in your terminal.
Continue reading
From here you can continue straight with SQLAlchemy.