Skip to content

Commit 06444ac

Browse files
authored
Docs update (#285)
1 parent 00c7082 commit 06444ac

File tree

11 files changed

+1189
-3896
lines changed

11 files changed

+1189
-3896
lines changed

docs/.vuepress/config.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { defineUserConfig } from "vuepress";
2-
import { searchProPlugin } from "vuepress-plugin-search-pro";
32
import { hopeTheme } from "vuepress-theme-hope";
3+
import { viteBundler } from '@vuepress/bundler-vite'
44

55
export default defineUserConfig({
66
lang: "en-US",
@@ -16,6 +16,8 @@ export default defineUserConfig({
1616
],
1717
],
1818

19+
bundler: viteBundler(),
20+
1921
theme: hopeTheme({
2022
hostname: "https://taskiq-python.github.io",
2123
logo: "/logo.svg",
@@ -28,7 +30,6 @@ export default defineUserConfig({
2830
sidebar: "structure",
2931

3032
pure: true,
31-
backToTop: false,
3233

3334
plugins: {
3435
readingTime: false,
@@ -45,8 +46,10 @@ export default defineUserConfig({
4546
changefreq: "daily",
4647
sitemapFilename: "sitemap.xml",
4748
},
49+
searchPro: {
50+
indexContent: true,
51+
autoSuggestions: false,
52+
}
4853
},
49-
}),
50-
51-
plugins: [searchProPlugin({ indexContent: true })],
54+
})
5255
});

docs/available-components/schedule-sources.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,23 @@ order: 4
77
These objects are used to fetch current schedule for tasks.
88
Currently we have only one schedule source.
99

10+
## RedisScheduleSource
11+
12+
This source is capable of adding new schedules in runtime. It uses Redis as a storage for schedules.
13+
To use this source you need to install `taskiq-redids` package.
14+
15+
```python
16+
from taskiq_redis import RedisScheduleSource
17+
18+
from taskiq import TaskiqScheduler
19+
20+
redis_source = RedisScheduleSource("redis://localhost:6379/0")
21+
scheduler = TaskiqScheduler(broker, sources=[redis_source])
22+
```
23+
24+
For more information on how to use dynamic schedule sources read [Dynamic scheduling section](../guide/scheduling-tasks.md#dynamic-scheduling).
25+
26+
1027
## LabelScheduleSource
1128

1229
This source parses labels of tasks, and if it finds a `schedule` label, it considers this task as scheduled.

docs/contrib.md

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,6 @@ order: 5
44

55
# Contribution guide
66

7-
We love contributions. This guide is for all folks who want to make taskiq
8-
better together.
9-
10-
We have several rules for contributors:
11-
* Please do not add malware.
12-
* Please make sure that your request solves problem.
13-
14-
If you struggle with something or feel frustrating, you either create an issue, create a discussion on page or publish draft PR and ask your question in description.
15-
16-
We have lots of tests in CI. But since runs from first-time contributors should be approved you better test locally, it just takes less time.
17-
187
We love contributions. This guide is for all folks who want to make taskiq better together.
198
We have several rules for contributors:
209
* Please do not add malware.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from taskiq_redis import ListQueueBroker, RedisScheduleSource
2+
3+
from taskiq import TaskiqScheduler
4+
5+
# Here's the broker that is going to execute tasks
6+
broker = ListQueueBroker("redis://localhost:6379/0")
7+
8+
# Here's the source that is used to store scheduled tasks
9+
redis_source = RedisScheduleSource("redis://localhost:6379/0")
10+
11+
# And here's the scheduler that is used to query scheduled sources
12+
scheduler = TaskiqScheduler(broker, sources=[redis_source])
13+
14+
15+
@broker.task
16+
async def my_task(arg1: int, arg2: str) -> None:
17+
"""Example task."""
18+
print("Hello from my_task!", arg1, arg2) # noqa: T201
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
order: 3
3+
---
4+
5+
# Taskiq + FastStream
6+
7+
[FastStream](https://faststream.airt.ai/latest/) is a library that allows you to write consumers and producers for different message brokers almost like taskiq. But the differense is that taskiq is more focused on tasks for a specific project and more like celery but async, while FastStream is more focused on events and defining how different systems communicate with each other using distributed brokers.
8+
9+
If you want to declare communication between different projects you can use taskiq, but it might be a bit more complex than using FastStream.
10+
11+
Although these libraries solve different problems, they have integration between each other, so you can use FastStream as a broker for taskiq. It allows FastStream to use taskiq's scheduler along with its own features.
12+
13+
To use FastStream as a broker for taskiq you need to install the `taskiq-faststream` library:
14+
15+
```bash:no-line-numbers
16+
pip install "taskiq-faststream"
17+
```
18+
19+
And you can use it like this:
20+
21+
```python
22+
from faststream import FastStream
23+
from faststream.kafka import KafkaBroker
24+
from taskiq_faststream import BrokerWrapper
25+
26+
broker = KafkaBroker("localhost:9092")
27+
app = FastStream(broker)
28+
29+
taskiq_broker = BrokerWrapper(broker)
30+
```
31+
32+
You can read more about scheduling tasks for FastStream in the [FastStream documentation](https://faststream.airt.ai/latest/scheduling/?h=schedule).
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# Taskiq + Aiogram
2+
3+
[Taskiq-Aiogram](https://github.com/taskiq-python/taskiq-aiogram) is a nice integration with one of the best telegram bot libraries - [aiogram](https://docs.aiogram.dev/en/latest/).
4+
5+
This integration allows you to easily send delayed messages or run intensive functions without blocking the message handing.
6+
7+
This integration adds three main dependencies which you can use in your taskiq functions:
8+
9+
- `aiogram.Bot` - the bot instance that you can use to send messages or perform other actions. If multiple bots listen to the same dispatcher, this dependency will be resolved to the latest bot passed in the `taskiq_aiogram.init` function.
10+
- `aiogram.Dispatcher` - current dispatcher instance.
11+
- `List[aiogram.Bot]` - list of all bots that were passed to the `taskiq_aiogram.init` function.
12+
13+
To enable the integration, please install the `taskiq-aiogram` library:
14+
15+
```bash:no-line-numbers
16+
pip install "taskiq-aiogram"
17+
```
18+
19+
After the installation is complete, add an initialization function call to your broker's main file so it becomes something like this:
20+
21+
```python title="tkq.py"
22+
import asyncio
23+
24+
import taskiq_aiogram
25+
from aiogram import Bot
26+
from taskiq import TaskiqDepends
27+
from taskiq_redis import ListQueueBroker
28+
29+
broker = ListQueueBroker("redis://localhost")
30+
31+
# This line is going to initialize everything.
32+
taskiq_aiogram.init(
33+
broker,
34+
# This is path to the dispatcher.
35+
"bot:dp",
36+
# This is path to the bot instance.
37+
"bot:bot",
38+
# You can specify more bots here.
39+
)
40+
41+
42+
@broker.task(task_name="my_task")
43+
async def my_task(chat_id: int, bot: Bot = TaskiqDepends()) -> None:
44+
print("I'm a task")
45+
await asyncio.sleep(4)
46+
await bot.send_message(chat_id, "task completed")
47+
48+
```
49+
50+
Let's see how to use this integration.
51+
52+
```python title="bot.py"
53+
import asyncio
54+
import logging
55+
import sys
56+
57+
from aiogram import Bot, Dispatcher, types
58+
from aiogram.filters import Command
59+
60+
from tkq import broker, my_task
61+
62+
dp = Dispatcher()
63+
bot = Bot(token="TOKEN")
64+
65+
66+
# Taskiq calls this function when starting the worker.
67+
@dp.startup()
68+
async def setup_taskiq(bot: Bot, *_args, **_kwargs):
69+
# Here we check if it's a clien-side,
70+
# Becuase otherwise you're going to
71+
# create infinite loop of startup events.
72+
if not broker.is_worker_process:
73+
logging.info("Setting up taskiq")
74+
await broker.startup()
75+
76+
77+
# Taskiq calls this function when shutting down the worker.
78+
@dp.shutdown()
79+
async def shutdown_taskiq(bot: Bot, *_args, **_kwargs):
80+
if not broker.is_worker_process:
81+
logging.info("Shutting down taskiq")
82+
await broker.shutdown()
83+
84+
85+
## Simple command to handle
86+
@dp.message(Command("task"))
87+
async def message(message: types.Message):
88+
await my_task.kiq(message.chat.id)
89+
90+
91+
## Main function that starts the bot.
92+
async def main():
93+
await dp.start_polling(bot)
94+
95+
96+
if __name__ == "__main__":
97+
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
98+
asyncio.run(main())
99+
100+
```
101+
102+
That's it. Now you can easily call tasks from your bots and access bots from within your tasks.

docs/guide/architecture-overview.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ If you use dark theme and cannot see words on diagram,
1515
try switching to light theme and back to dark.
1616
:::
1717

18-
```mermaid
18+
```sequence
1919
rect rgb(191, 223, 255)
2020
note right of Your code: Client side.
2121
Your code ->> Kicker: assemble message

docs/guide/scheduling-tasks.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,79 @@ To avoid this behaviour, you can pass the `--skip-first-run` flag to the `taskiq
9292
```bash:no-line-numbers
9393
taskiq scheduler module:scheduler --skip-first-run
9494
```
95+
96+
97+
## Dynamic scheduling
98+
99+
Sometimes you may want to add new schedules to the scheduler on the fly. For example, you may want to run a specific function in several minutes from now. You can easily do it with ScheduleSources that support dynamic scheduling. Currently we suggest to use the `RedisScheduleSource` for that purpose. List of schedulers with dynamic task addition will be extended in the future.
100+
For list of available schedule sources see [Available schedule sources](../available-components/schedule-sources.md).
101+
102+
Here's an example of using redis schedule source:
103+
104+
@[code python](../examples/schedule/redis_schedule.py)
105+
106+
Now we can use this source to add new schedules in runtime. Here's an example:
107+
108+
```python
109+
await redis_source.startup()
110+
111+
await my_task.schedule_by_time(
112+
redis_source,
113+
# It's better to use UTC time, or add tzinfo to datetime.
114+
datetime.datetime.utcnow() + datetime.timedelta(minutes=1, seconds=5),
115+
# You can pass args and kwargs here as usual
116+
11,
117+
arg2="arg2",
118+
)
119+
```
120+
121+
Or if you want ot use cron schedules instead, just use `schedule_by_cron` method.
122+
123+
```python
124+
await my_task.schedule_by_cron(
125+
redis_source,
126+
"*/5 * * * *",
127+
11,
128+
arg2="arg2",
129+
)
130+
```
131+
132+
If you want to pass additional labels, you can call these methods on the `Kicker` instance.
133+
134+
```python
135+
schedule = (
136+
await my_task.kicker()
137+
.with_labels(label1="value")
138+
.schedule_by_time(
139+
redis_source,
140+
datetime.datetime.utcnow() + datetime.timedelta(seconds=10),
141+
11,
142+
arg2="arg2",
143+
)
144+
)
145+
```
146+
147+
::: warning Cool warning!
148+
149+
The `with_broker` method won't do anything in this case, since we have a broker assigned to each scheduler.
150+
151+
:::
152+
153+
Each of these methods return you an instance of the `CreatedSchedule` class. This object has unique schedule ID and some helper methods. For example, you can use the `unschedule` method to remove the schedule from the source.
154+
155+
```python
156+
schedule = await my_task.schedule_by_time(
157+
redis_source,
158+
datetime.datetime.utcnow() + datetime.timedelta(minutes=1, seconds=5),
159+
11,
160+
arg2="arg2",
161+
)
162+
163+
await schedule.unschedule()
164+
```
165+
166+
Or it can be done manually, by calling `delete_schedule` on schedule source providing it whith `schedule_id`.
167+
168+
```python
169+
await redis_source.delete_schedule(schedule.schedule_id)
170+
```

package.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111
},
1212
"packageManager": "[email protected]",
1313
"devDependencies": {
14-
"@vuepress/client": "2.0.0-rc.0",
15-
"mermaid": "^10.6.1",
16-
"vue": "^3.3.9",
17-
"vuepress": "2.0.0-rc.0",
18-
"vuepress-plugin-search-pro": "2.0.0-rc.1",
19-
"vuepress-theme-hope": "2.0.0-rc.1"
14+
"@vuepress/bundler-vite": "2.0.0-rc.6",
15+
"mermaid": "^10.8.0",
16+
"sass-loader": "^14.1.0",
17+
"vue": "^3.4.15",
18+
"vuepress": "2.0.0-rc.6",
19+
"vuepress-plugin-search-pro": "2.0.0-rc.22",
20+
"vuepress-theme-hope": "2.0.0-rc.22"
2021
}
2122
}

0 commit comments

Comments
 (0)