Skip to content

Commit 4734b96

Browse files
authored
Merge pull request #1 from danfimov/feat-redis-clone
2 parents 5c89fc7 + 92fc917 commit 4734b96

22 files changed

+4112
-271
lines changed

.github/workflows/test.yml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
name: Testing package
1+
name: Testing taskiq-valkey
22

3-
on: push
3+
on:
4+
push:
5+
branches:
6+
- main
7+
- develop
8+
pull_request:
49

510
jobs:
611
lint:
@@ -32,10 +37,11 @@ jobs:
3237
strategy:
3338
matrix:
3439
py_version: ["3.9", "3.10", "3.11", "3.12", "3.13"]
35-
os: [ubuntu-latest, windows-latest]
36-
runs-on: "${{ matrix.os }}"
40+
runs-on: "ubuntu-latest"
3741
steps:
3842
- uses: actions/checkout@v4
43+
- name: Set up Valkey instance and Valkey cluster
44+
run: docker compose up -d
3945
- name: Install poetry
4046
run: pipx install poetry
4147
- name: Set up Python
@@ -51,7 +57,7 @@ jobs:
5157
run: poetry run coverage xml
5258
- name: Upload coverage reports to Codecov with GitHub Action
5359
uses: codecov/codecov-action@v3
54-
if: matrix.os == 'ubuntu-latest' && matrix.py_version == '3.11'
60+
if: matrix.py_version == '3.11'
5561
with:
5662
token: ${{ secrets.CODECOV_TOKEN }}
5763
fail_ci_if_error: false

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# See https://pre-commit.com/hooks.html for more hooks
33
repos:
44
- repo: https://github.com/pre-commit/pre-commit-hooks
5-
rev: v2.4.0
5+
rev: v5.0.0
66
hooks:
77
- id: check-ast
88
- id: trailing-whitespace

README.md

Lines changed: 134 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,134 @@
1-
# taskiq_valkey
1+
# TaskIQ-Valkey
2+
3+
Taskiq-valkey is a plugin for taskiq that adds a new broker and result backend based on valkey.
4+
5+
# Installation
6+
7+
To use this project you must have installed core taskiq library:
8+
```bash
9+
pip install taskiq
10+
```
11+
This project can be installed using pip:
12+
```bash
13+
pip install taskiq-valkey
14+
```
15+
16+
# Usage
17+
18+
Let's see the example with the valkey broker and valkey async result:
19+
20+
```python
21+
# broker.py
22+
import asyncio
23+
24+
from taskiq_valkey import ValkeyAsyncResultBackend, ValkeyStreamBroker
25+
26+
result_backend = ValkeyAsyncResultBackend(
27+
valkey_url="valkey://localhost:6379",
28+
)
29+
30+
# Or you can use PubSubBroker if you need broadcasting
31+
broker = ValkeyStreamBroker(
32+
valkey_url="valkey://localhost:6379",
33+
).with_result_backend(result_backend)
34+
35+
36+
@broker.task
37+
async def best_task_ever() -> None:
38+
"""Solve all problems in the world."""
39+
await asyncio.sleep(5.5)
40+
print("All problems are solved!")
41+
42+
43+
async def main():
44+
task = await best_task_ever.kiq()
45+
print(await task.wait_result())
46+
47+
48+
if __name__ == "__main__":
49+
asyncio.run(main())
50+
```
51+
52+
Launch the workers:
53+
`taskiq worker broker:broker`
54+
Then run the main code:
55+
`python3 broker.py`
56+
57+
58+
## Brokers
59+
60+
This package contains 6 broker implementations. We have two broker types: `PubSub` and `Stream`.
61+
62+
Each of type is implemented for each valkey architecture:
63+
* Single node
64+
* Cluster
65+
* Sentinel
66+
67+
Here's a small breakdown of how they differ from eachother.
68+
69+
70+
### PubSub
71+
72+
By default on old valkey versions PUBSUB was the way of making valkey into a queue.
73+
But using PUBSUB means that all messages delivered to all subscribed consumers.
74+
75+
> [!WARNING]
76+
> This broker doesn't support acknowledgements. If during message processing
77+
> Worker was suddenly killed the message is going to be lost.
78+
79+
### Stream
80+
81+
Stream brokers use valkey [stream type](https://valkey.io/topics/streams-intro/) to store and fetch messages.
82+
83+
> [!TIP]
84+
> This broker **supports** acknowledgements and therefore is fine to use in cases when data durability is
85+
> required.
86+
87+
## ValkeyAsyncResultBackend configuration
88+
89+
ValkeyAsyncResultBackend parameters:
90+
* `valkey_url` - url to valkey.
91+
* `keep_results` - flag to not remove results from Valkey after reading.
92+
* `result_ex_time` - expire time in seconds (by default - not specified)
93+
* `result_px_time` - expire time in milliseconds (by default - not specified)
94+
* Any other keyword arguments are passed to `valkey.asyncio.BlockingConnectionPool`.
95+
Notably, you can use `timeout` to set custom timeout in seconds for reconnects
96+
(or set it to `None` to try reconnects indefinitely).
97+
98+
> [!WARNING]
99+
> **It is highly recommended to use expire time in ValkeyAsyncResultBackend**
100+
> If you want to add expiration, either `result_ex_time` or `result_px_time` must be set.
101+
> ```python
102+
> # First variant
103+
> valkey_async_result = ValkeyAsyncResultBackend(
104+
> valkey_url="valkey://localhost:6379",
105+
> result_ex_time=1000,
106+
> )
107+
>
108+
> # Second variant
109+
> valkey_async_result = ValkeyAsyncResultBackend(
110+
> valkey_url="valkey://localhost:6379",
111+
> result_px_time=1000000,
112+
> )
113+
> ```
114+
115+
116+
## Schedule sources
117+
118+
119+
You can use this package to add dynamic schedule sources. They are used to store
120+
schedules for taskiq scheduler.
121+
122+
The advantage of using schedule sources from this package over default `LabelBased` source is that you can
123+
dynamically add schedules in it.
124+
125+
For now we have only one type of schedules - `ListValkeyScheduleSource`.
126+
127+
### ListValkeyScheduleSource
128+
129+
This source holds values in lists.
130+
131+
* For cron tasks it uses key `{prefix}:cron`.
132+
* For timed schedules it uses key `{prefix}:time:{time}` where `{time}` is actually time where schedules should run.
133+
134+
The main advantage of this approach is that we only fetch tasks we need to run at a given time and do not perform any excesive calls to valkey.

docker-compose.yml

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
services:
2+
valkey:
3+
image: bitnami/valkey:7.2.7
4+
environment:
5+
ALLOW_EMPTY_PASSWORD: "yes"
6+
healthcheck:
7+
test: ["CMD", "valkey-cli", "ping"]
8+
interval: 5s
9+
timeout: 5s
10+
retries: 3
11+
start_period: 10s
12+
ports:
13+
- 7000:6379
14+
valkey-node-0: &valkey-node
15+
image: docker.io/bitnami/valkey-cluster:7.2.7
16+
environment:
17+
ALLOW_EMPTY_PASSWORD: "yes"
18+
VALKEY_NODES: "valkey-node-0 valkey-node-1 valkey-node-2 valkey-node-3 valkey-node-4 valkey-node-5"
19+
healthcheck:
20+
test: ["CMD", "valkey-cli", "ping"]
21+
interval: 5s
22+
timeout: 5s
23+
retries: 3
24+
start_period: 10s
25+
26+
valkey-node-1:
27+
<<: *valkey-node
28+
29+
valkey-node-2:
30+
<<: *valkey-node
31+
32+
valkey-node-3:
33+
<<: *valkey-node
34+
35+
valkey-node-4:
36+
<<: *valkey-node
37+
38+
valkey-node-5:
39+
image: docker.io/bitnami/valkey-cluster:7.2.7
40+
depends_on:
41+
- valkey-node-0
42+
- valkey-node-1
43+
- valkey-node-2
44+
- valkey-node-3
45+
- valkey-node-4
46+
environment:
47+
ALLOW_EMPTY_PASSWORD: "yes"
48+
VALKEY_NODES: "valkey-node-0 valkey-node-1 valkey-node-2 valkey-node-3 valkey-node-4 valkey-node-5"
49+
VALKEY_CLUSTER_REPLICAS: 1
50+
VALKEY_CLUSTER_CREATOR: "yes"
51+
healthcheck:
52+
test: ["CMD", "valkey-cli", "ping"]
53+
interval: 5s
54+
timeout: 5s
55+
retries: 3
56+
start_period: 10s
57+
ports:
58+
- 7001:6379
59+
60+
valkey-master:
61+
image: bitnami/valkey:7.2.7
62+
environment:
63+
ALLOW_EMPTY_PASSWORD: "yes"
64+
healthcheck:
65+
test: ["CMD", "valkey-cli", "ping"]
66+
interval: 5s
67+
timeout: 5s
68+
retries: 3
69+
start_period: 10s
70+
71+
valkey-sentinel:
72+
image: bitnami/valkey-sentinel:7.2.7
73+
depends_on:
74+
- valkey-master
75+
environment:
76+
ALLOW_EMPTY_PASSWORD: "yes"
77+
VALKEY_MASTER_HOST: "valkey-master"
78+
healthcheck:
79+
test: ["CMD", "valkey-cli", "-p", "26379", "ping"]
80+
interval: 5s
81+
timeout: 5s
82+
retries: 3
83+
start_period: 10s
84+
ports:
85+
- 7002:26379

0 commit comments

Comments
 (0)