Skip to content

Commit 20e72ac

Browse files
committed
Merge branch 'release/0.3.0'
2 parents e01adbb + 1aafee5 commit 20e72ac

File tree

11 files changed

+818
-623
lines changed

11 files changed

+818
-623
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ repos:
4949
language: system
5050
types: [python]
5151
pass_filenames: false
52-
args: [taskiq_aiogram]
52+
args: [taskiq_aiogram, example]
5353

5454
- id: yesqa
5555
name: Remove usless noqa

README.md

Lines changed: 51 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,41 +22,47 @@ startup and shutdown events, you have to manually define your executor.
2222
For example:
2323

2424
```python
25-
from aiogram import Bot, Dispatcher, executor
26-
# Your taskiq broker
27-
from your_project.tkq import broker
25+
import asyncio
26+
import logging
27+
import sys
2828

29+
from aiogram import Bot, Dispatcher
2930

30-
bot = Bot(token="API_TOKEN")
31+
# Your taskiq broker
32+
from your_project.tkq import broker
3133

32-
dispatcher = Dispatcher(bot)
34+
dp = Dispatcher()
35+
bot = Bot(token="TOKEN")
3336

3437

35-
async def setup_taskiq(_: Dispatcher):
38+
@dp.startup()
39+
async def setup_taskiq(bot: Bot, *_args, **_kwargs):
3640
# Here we check if it's a clien-side,
3741
# Becuase otherwise you're going to
3842
# create infinite loop of startup events.
3943
if not broker.is_worker_process:
40-
print("Setting up taskiq")
44+
logging.info("Setting up taskiq")
4145
await broker.startup()
4246

4347

44-
async def shutdown_taskiq(_: Dispatcher):
48+
@dp.shutdown()
49+
async def shutdown_taskiq(bot: Bot, *_args, **_kwargs):
4550
if not broker.is_worker_process:
46-
print("Shutting down taskiq")
51+
logging.info("Shutting down taskiq")
4752
await broker.shutdown()
4853

49-
# Here we defined our executor.
50-
bot_executor = executor.Executor(dispatcher=dispatcher)
51-
bot_executor.on_startup([setup_taskiq])
52-
bot_executor.on_shutdown([shutdown_taskiq])
54+
55+
async def main():
56+
await dp.start_polling(bot)
57+
5358

5459
if __name__ == "__main__":
55-
bot_executor.start_polling()
60+
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
61+
asyncio.run(main())
5662

5763
```
5864

59-
The only thing that left is to add one line to your broker definition.
65+
The only thing that left is to add few lines to your broker definition.
6066

6167

6268
```python
@@ -69,39 +75,53 @@ broker = MyBroker()
6975
# This line is going to initialize everything.
7076
taskiq_aiogram.init(
7177
broker,
72-
# Here we define path to your executor.
73-
# This format is similar to uvicorn or gunicorn.
74-
"your_project.__main__:bot_executor",
75-
pooling=True,
76-
webhook=False,
78+
"your_project.__main__:dp",
79+
"your_project.__main__:bot",
7780
)
7881
```
7982

80-
Aiogram defines startup events for pooling and webhooks separately. So you need to
81-
explicitly specify which mode suites you. BTW, by default aiogram adds handler events to
82-
both, so setting only pooling to True should be enought for almost any case.
83-
8483
That's it.
8584

86-
Let's create some tasks!
85+
Let's create some tasks! I created task in a separate module,
86+
named `tasks.py`.
8787

8888
```python
89-
# Sometimes python imports wrong task names.
90-
# If that happens, please set a task_name explicitly.
89+
from aiogram import Bot
90+
from your_project.tkq import broker
91+
9192
@broker.task(task_name="my_task")
9293
async def my_task(chat_id: int, bot: Bot = TaskiqDepends()) -> None:
9394
print("I'm a task")
9495
await asyncio.sleep(4)
9596
await bot.send_message(chat_id, "task completed")
9697

98+
```
99+
100+
Now let's call our new task somewhere in bot commands.
97101

98-
@dispatcher.message_handler(commands=["task"])
99-
async def send_task(message: types.Message):
100-
await message.reply("Sending a task")
102+
```python
103+
from aiogram import types
104+
from aiogram.filters import Command
105+
106+
from tasks import my_task
107+
108+
109+
@dp.message(Command("task"))
110+
async def message(message: types.Message):
101111
await my_task.kiq(message.chat.id)
102112

103113
```
104114

105-
And it works!
115+
To start the worker, please type:
116+
117+
```
118+
taskiq worker your_project.tkq:broker --fs-discover
119+
```
120+
121+
We use `--fs-discover` to find all tasks.py modules recursively
122+
and import all tasks into broker.
123+
124+
125+
Now we can fire the task and see everything in action.
106126

107127
![Showcase.jpg](https://raw.githubusercontent.com/taskiq-python/taskiq-aiogram/master/imgs/showcase.jpg)

example/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Simple example
2+
3+
This is an example from README.md. It runs a bot with a simple command `/task`.
4+
To run the example:
5+
6+
1. Install requirements, using command `pip install -r example/requirements.txt`;
7+
1. Replace `TOKEN` in `__main__.py` with your actual token;
8+
2. Start taskiq workers, by running `taskiq worker example.tkq:broker --fs-discover`;
9+
3. Start the bot, by running `python example`.

example/__init__.py

Whitespace-only changes.

example/__main__.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import asyncio
2+
import logging
3+
import sys
4+
from typing import Any
5+
6+
from aiogram import Bot, Dispatcher, types
7+
from aiogram.filters import Command
8+
9+
from example.tasks import my_task
10+
from example.tkq import broker
11+
12+
dp = Dispatcher()
13+
bot = Bot(token="TOKEN")
14+
15+
16+
@dp.startup()
17+
async def setup_taskiq(bot: Bot, *_args: Any, **_kwargs: Any) -> None:
18+
# Here we check if it's a clien-side,
19+
# Becuase otherwise you're going to
20+
# create infinite loop of startup events.
21+
if not broker.is_worker_process:
22+
logging.info("Setting up taskiq")
23+
await broker.startup()
24+
25+
26+
@dp.shutdown()
27+
async def shutdown_taskiq(bot: Bot, *_args: Any, **_kwargs: Any) -> None:
28+
if not broker.is_worker_process:
29+
logging.info("Shutting down taskiq")
30+
await broker.shutdown()
31+
32+
33+
@dp.message(Command("task"))
34+
async def message(message: types.Message) -> None:
35+
await my_task.kiq(message.chat.id)
36+
37+
38+
async def main() -> None:
39+
await dp.start_polling(bot)
40+
41+
42+
if __name__ == "__main__":
43+
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
44+
asyncio.run(main())

example/requirements.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
aiogram
2+
3+
taskiq
4+
taskiq_redis
5+
taskiq_aiogram

example/tasks.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import asyncio
2+
3+
from aiogram import Bot
4+
from taskiq import TaskiqDepends
5+
6+
from example.tkq import broker
7+
8+
9+
@broker.task(task_name="my_task")
10+
async def my_task(chat_id: int, bot: Bot = TaskiqDepends()) -> None:
11+
print("I'm a task")
12+
await asyncio.sleep(4)
13+
await bot.send_message(chat_id, "task completed")

example/tkq.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from taskiq_redis import ListQueueBroker
2+
3+
from taskiq_aiogram import init
4+
5+
broker = ListQueueBroker("redis://localhost")
6+
7+
init(
8+
broker,
9+
"example.__main__:dp",
10+
"example.__main__:bot",
11+
)

0 commit comments

Comments
 (0)