Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change examples to use lifespan instead of OnStartUp/OnShutDown #32

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ See [documentation](https://ManiMozaffar.github.io/aioclock/) for more details.
## A Sample Example

```python
from collections.abc import AsyncGenerator
from contextlib import asynccontextmanager
import asyncio

from aioclock import AioClock, At, Depends, Every, Forever, Once, OnShutDown, OnStartUp
from aioclock import AioClock, At, Depends, Every, Forever, Once
from aioclock.group import Group

# groups.py
Expand Down Expand Up @@ -79,21 +81,20 @@ async def once():
print("Just once, I get to say something. Here it goes... I love lamp.")


# app.py
app = AioClock()
app.include_group(group)


@app.task(trigger=OnStartUp())
async def startup():
@asynccontextmanager
async def lifespan(aio_clock: AioClock) -> AsyncGenerator[AioClock]:
# starting up
print(
"Welcome to the Async Chronicles! Did you know a group of unicorns is called a blessing? Well, now you do!"
)
yield aio_clock
# shuting down
print("Going offline. Remember, if your code is running, you better go catch it!")


@app.task(trigger=OnShutDown())
async def shutdown():
print("Going offline. Remember, if your code is running, you better go catch it!")
# app.py
app = AioClock(lifespan=lifespan)
app.include_group(group)


# main.py
Expand Down
18 changes: 11 additions & 7 deletions docs/examples/brokers.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,33 @@ You can basically run any tasks on aioclock, it could be your redis broker or ot
AioClock offer you a unique easy way to spin up new services, without any overhead or perfomance issue!

```python
from aioclock import AioClock, Forever, OnShutDown, Depends
from collections.abc import AsyncGenerator
from contextlib import asynccontextmanager
from aioclock import AioClock, Forever, Depends
from functools import lru_cache
from typing import NewType

BrokerType = NewType("BrokerType", ...) # your broker type ...

app = AioClock()

# your singleton redis instance
@lru_cache
def get_redis():
...

@asynccontextmanager
async def lifespan(aio_clock: AioClock, redis: BrokerType = Depends(get_redis)) -> AsyncGenerator[AioClock]:
yield aio_clock
await redis.disconnect()


app = AioClock(lifespan=lifespan)


@app.task(trigger=Forever())
async def read_message_queue(redis: BrokerType = Depends(get_redis)):
async for message in redis.listen("..."):
...


@app.task(trigger=OnShutDown())
async def shutdown_event(redis: BrokerType = Depends(get_redis)):
await redis.disconnect()
```

One other way to do this, is to implement a trigger that automatically execute the function.
Expand Down
20 changes: 9 additions & 11 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ AioClock is very user friendly and easy to use, it's type stated library to use
AioClock always have a trigger, that trigger the events.

```python
from collections.abc import AsyncGenerator
from contextlib import asynccontextmanager
import asyncio

from aioclock import AioClock, At, Depends, Every, Forever, Once, OnShutDown, OnStartUp
from aioclock import AioClock, At, Depends, Every, Forever, Once
from aioclock.group import Group

# groups.py
Expand Down Expand Up @@ -69,23 +71,19 @@ async def once():
print("Just once, I get to say something. Here it goes... I love lamp.")


# app.py
app = AioClock()
app.include_group(group)


@app.task(trigger=OnStartUp())
async def startup():
@asynccontextmanager
async def lifespan(aio_clock: AioClock) -> AsyncGenerator[AioClock]:
print(
"Welcome to the Async Chronicles! Did you know a group of unicorns is called a blessing? Well, now you do!"
)


@app.task(trigger=OnShutDown())
async def shutdown():
yield aio_clock
print("Going offline. Remember, if your code is running, you better go catch it!")


app = AioClock(lifespan=lifespan)
app.include_group(group)

# main.py
if __name__ == "__main__":
asyncio.run(app.serve())
Expand Down
21 changes: 10 additions & 11 deletions examples/app.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import asyncio
from collections.abc import AsyncGenerator
from contextlib import asynccontextmanager
import threading
from time import sleep
from typing import Annotated

from aioclock import AioClock, Depends, Every, Group, OnShutDown, OnStartUp
from aioclock import AioClock, Depends, Every, Group

# service1.py
group = Group()
Expand Down Expand Up @@ -34,19 +36,16 @@ async def async_task(val: str = Depends(dependency)):
print(f"{val} `async_task` {threading.current_thread().ident}")


# app.py
app = AioClock()
app.include_group(group)


@app.task(trigger=OnStartUp())
def startup(val: str = Depends(dependency)):
@asynccontextmanager
async def lifespan(aio_clock: AioClock) -> AsyncGenerator[AioClock]:
print("Welcome!")
yield aio_clock
print("Bye!")


@app.task(trigger=OnShutDown())
def shutdown(val: str = Depends(dependency)):
print("Bye!")
# app.py
app = AioClock(lifespan=lifespan)
app.include_group(group)


if __name__ == "__main__":
Expand Down
21 changes: 10 additions & 11 deletions examples/awesome_triggers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import asyncio
from collections.abc import AsyncGenerator
from contextlib import asynccontextmanager

from aioclock import AioClock, At, Depends, Every, Forever, Once, OnShutDown, OnStartUp
from aioclock import AioClock, At, Depends, Every, Forever, Once
from aioclock.group import Group

# groups.py
Expand Down Expand Up @@ -40,21 +42,18 @@ async def once():
print("Just once, I get to say something. Here it goes... I love lamp.")


# app.py
app = AioClock()
app.include_group(group)


@app.task(trigger=OnStartUp())
async def startup():
@asynccontextmanager
async def lifespan(aio_clock: AioClock) -> AsyncGenerator[AioClock]:
print(
"Welcome to the Async Chronicles! Did you know a group of unicorns is called a blessing? Well, now you do!"
)
yield aio_clock
print("Going offline. Remember, if your code is running, you better go catch it!")


@app.task(trigger=OnShutDown())
async def shutdown():
print("Going offline. Remember, if your code is running, you better go catch it!")
# app.py
app = AioClock(lifespan=lifespan)
app.include_group(group)


# main.py
Expand Down
14 changes: 9 additions & 5 deletions examples/with_fast_api.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import asyncio
from collections.abc import AsyncGenerator
from contextlib import asynccontextmanager

from fastapi import FastAPI

from aioclock import AioClock
from aioclock.ext.fast import make_fastapi_router
from aioclock.triggers import Every, OnStartUp
from aioclock.triggers import Every

clock_app = AioClock()

@asynccontextmanager
async def aioclock_lifespan(aio_clock: AioClock) -> AsyncGenerator[AioClock]:
print("Starting aiolcok app...")
yield aio_clock
print("Closing aiolcok app...")


@clock_app.task(trigger=OnStartUp())
async def startup():
print("Starting...")
clock_app = AioClock(lifespan=aioclock_lifespan)


@clock_app.task(trigger=Every(seconds=3600))
Expand Down
Loading