render-lab-tasks-render-kv 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- render_lab_tasks_render_kv-0.1.0/.gitignore +11 -0
- render_lab_tasks_render_kv-0.1.0/LICENSE +21 -0
- render_lab_tasks_render_kv-0.1.0/PKG-INFO +43 -0
- render_lab_tasks_render_kv-0.1.0/README.md +30 -0
- render_lab_tasks_render_kv-0.1.0/pyproject.toml +22 -0
- render_lab_tasks_render_kv-0.1.0/src/render_lab_tasks_render_kv/__init__.py +1 -0
- render_lab_tasks_render_kv-0.1.0/src/render_lab_tasks_render_kv/_app.py +3 -0
- render_lab_tasks_render_kv-0.1.0/src/render_lab_tasks_render_kv/client.py +146 -0
- render_lab_tasks_render_kv-0.1.0/src/render_lab_tasks_render_kv/delete.py +18 -0
- render_lab_tasks_render_kv-0.1.0/src/render_lab_tasks_render_kv/expire.py +18 -0
- render_lab_tasks_render_kv-0.1.0/src/render_lab_tasks_render_kv/get.py +16 -0
- render_lab_tasks_render_kv-0.1.0/src/render_lab_tasks_render_kv/incr.py +16 -0
- render_lab_tasks_render_kv-0.1.0/src/render_lab_tasks_render_kv/list_push.py +18 -0
- render_lab_tasks_render_kv-0.1.0/src/render_lab_tasks_render_kv/list_range.py +18 -0
- render_lab_tasks_render_kv-0.1.0/src/render_lab_tasks_render_kv/lock.py +16 -0
- render_lab_tasks_render_kv-0.1.0/src/render_lab_tasks_render_kv/py.typed +0 -0
- render_lab_tasks_render_kv-0.1.0/src/render_lab_tasks_render_kv/retry.py +3 -0
- render_lab_tasks_render_kv-0.1.0/src/render_lab_tasks_render_kv/set.py +16 -0
- render_lab_tasks_render_kv-0.1.0/src/render_lab_tasks_render_kv/tasks.py +32 -0
- render_lab_tasks_render_kv-0.1.0/src/render_lab_tasks_render_kv/types.py +88 -0
- render_lab_tasks_render_kv-0.1.0/src/render_lab_tasks_render_kv/unlock.py +18 -0
- render_lab_tasks_render_kv-0.1.0/tests/test_contract.py +99 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Render Lab
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: render-lab-tasks-render-kv
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: render-kv tasks for Render Workflows
|
|
5
|
+
Project-URL: Repository, https://github.com/render-lab/render-tasks-python
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Requires-Python: >=3.12
|
|
9
|
+
Requires-Dist: redis<7,>=6.4
|
|
10
|
+
Requires-Dist: render-lab-tasks-core<0.2,>=0.1.1
|
|
11
|
+
Requires-Dist: render==1.0.1
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
|
|
14
|
+
# render-lab-tasks-render-kv
|
|
15
|
+
|
|
16
|
+
Nine Render Key Value tasks: `kv.get`, `kv.set`, `kv.incr`, `kv.expire`,
|
|
17
|
+
`kv.delete`, `kv.lock`, `kv.unlock`, `kv.listPush`, and `kv.listRange`.
|
|
18
|
+
Import `render_lab_tasks_render_kv.tasks` for explicit registration.
|
|
19
|
+
Each task also exports an injectable async `*_impl(ctx, input, *, deps=...)`.
|
|
20
|
+
|
|
21
|
+
`REDIS_URL` is read at first use. A linked Render Key Value instance provides
|
|
22
|
+
its private connection URL. There are no other credential requirements.
|
|
23
|
+
|
|
24
|
+
```python
|
|
25
|
+
from render_lab_tasks_render_kv.tasks import app, get_impl
|
|
26
|
+
# await get_impl(ctx, {"key": "example"})
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Connections are closed after each raw invocation. Redis transport retries are
|
|
30
|
+
explicitly disabled. The durable policy allows three retries at 1s, 2s, 4s.
|
|
31
|
+
Increment and list append can execute twice if a connection fails after the
|
|
32
|
+
write. Lock acquisition uses SET NX EX; unlock uses one atomic compare-and-delete
|
|
33
|
+
Lua command. List append followed by TTL refresh is two Redis commands, matching
|
|
34
|
+
TypeScript; partial failure may leave a list without the requested TTL.
|
|
35
|
+
|
|
36
|
+
Hermetic tests verify command arguments and failure behavior. Live connection,
|
|
37
|
+
failover, contention, and expiry checks remain in the repository testing backlog.
|
|
38
|
+
|
|
39
|
+
## Installation
|
|
40
|
+
|
|
41
|
+
```sh
|
|
42
|
+
pip install render-lab-tasks-render-kv==0.1.0
|
|
43
|
+
```
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# render-lab-tasks-render-kv
|
|
2
|
+
|
|
3
|
+
Nine Render Key Value tasks: `kv.get`, `kv.set`, `kv.incr`, `kv.expire`,
|
|
4
|
+
`kv.delete`, `kv.lock`, `kv.unlock`, `kv.listPush`, and `kv.listRange`.
|
|
5
|
+
Import `render_lab_tasks_render_kv.tasks` for explicit registration.
|
|
6
|
+
Each task also exports an injectable async `*_impl(ctx, input, *, deps=...)`.
|
|
7
|
+
|
|
8
|
+
`REDIS_URL` is read at first use. A linked Render Key Value instance provides
|
|
9
|
+
its private connection URL. There are no other credential requirements.
|
|
10
|
+
|
|
11
|
+
```python
|
|
12
|
+
from render_lab_tasks_render_kv.tasks import app, get_impl
|
|
13
|
+
# await get_impl(ctx, {"key": "example"})
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Connections are closed after each raw invocation. Redis transport retries are
|
|
17
|
+
explicitly disabled. The durable policy allows three retries at 1s, 2s, 4s.
|
|
18
|
+
Increment and list append can execute twice if a connection fails after the
|
|
19
|
+
write. Lock acquisition uses SET NX EX; unlock uses one atomic compare-and-delete
|
|
20
|
+
Lua command. List append followed by TTL refresh is two Redis commands, matching
|
|
21
|
+
TypeScript; partial failure may leave a list without the requested TTL.
|
|
22
|
+
|
|
23
|
+
Hermetic tests verify command arguments and failure behavior. Live connection,
|
|
24
|
+
failover, contention, and expiry checks remain in the repository testing backlog.
|
|
25
|
+
|
|
26
|
+
## Installation
|
|
27
|
+
|
|
28
|
+
```sh
|
|
29
|
+
pip install render-lab-tasks-render-kv==0.1.0
|
|
30
|
+
```
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling>=1.27,<2"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "render-lab-tasks-render-kv"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "render-kv tasks for Render Workflows"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
license-files = ["LICENSE"]
|
|
12
|
+
requires-python = ">=3.12"
|
|
13
|
+
dependencies = ["render==1.0.1", "redis>=6.4,<7", "render-lab-tasks-core>=0.1.1,<0.2"]
|
|
14
|
+
|
|
15
|
+
[project.urls]
|
|
16
|
+
Repository = "https://github.com/render-lab/render-tasks-python"
|
|
17
|
+
|
|
18
|
+
[tool.uv.sources]
|
|
19
|
+
render-lab-tasks-core = { workspace = true }
|
|
20
|
+
|
|
21
|
+
[tool.hatch.build.targets.wheel]
|
|
22
|
+
packages = ["src/render_lab_tasks_render_kv"]
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Import .tasks explicitly to register durable tasks."""
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
"""Injected vendor interface and one-attempt async HTTP adapter."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
from collections.abc import AsyncIterator
|
|
7
|
+
from contextlib import asynccontextmanager
|
|
8
|
+
from dataclasses import dataclass
|
|
9
|
+
from typing import Any, Protocol
|
|
10
|
+
|
|
11
|
+
from redis.asyncio import Redis
|
|
12
|
+
from redis.asyncio.retry import Retry
|
|
13
|
+
from redis.backoff import NoBackoff
|
|
14
|
+
from render_lab_tasks_core.http import nullish
|
|
15
|
+
|
|
16
|
+
from . import types as t
|
|
17
|
+
|
|
18
|
+
UNLOCK_SCRIPT = (
|
|
19
|
+
"if redis.call('get', KEYS[1]) == ARGV[1] then "
|
|
20
|
+
"return redis.call('del', KEYS[1]) else return 0 end"
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class CommandPort(Protocol):
|
|
25
|
+
async def execute_command(self, *args: Any) -> Any: ...
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class Port(Protocol):
|
|
29
|
+
async def delete(self, input: t.DeleteInput) -> t.DeleteResult: ...
|
|
30
|
+
async def expire(self, input: t.ExpireInput) -> t.ExpireResult: ...
|
|
31
|
+
async def get(self, input: t.GetInput) -> t.GetResult: ...
|
|
32
|
+
async def incr(self, input: t.IncrInput) -> t.IncrResult: ...
|
|
33
|
+
async def list_push(self, input: t.ListPushInput) -> t.ListPushResult: ...
|
|
34
|
+
async def list_range(self, input: t.ListRangeInput) -> t.ListRangeResult: ...
|
|
35
|
+
async def lock(self, input: t.LockInput) -> t.LockResult: ...
|
|
36
|
+
async def unlock(self, input: t.UnlockInput) -> t.UnlockResult: ...
|
|
37
|
+
async def set(self, input: t.SetInput) -> t.SetResult: ...
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
@dataclass(frozen=True)
|
|
41
|
+
class Deps:
|
|
42
|
+
render_kv: Port
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def redis_number(value: float) -> int | float:
|
|
46
|
+
"""JS numbers stringify integral floats without a decimal suffix."""
|
|
47
|
+
return int(value) if isinstance(value, float) and value.is_integer() else value
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class Client:
|
|
51
|
+
def __init__(self, redis: CommandPort) -> None:
|
|
52
|
+
self.redis = redis
|
|
53
|
+
|
|
54
|
+
async def get(self, input: t.GetInput) -> t.GetResult:
|
|
55
|
+
return {"value": await self.redis.execute_command("GET", input["key"])}
|
|
56
|
+
|
|
57
|
+
async def set(self, input: t.SetInput) -> t.SetResult:
|
|
58
|
+
args: list[Any] = ["SET", input["key"], input["value"]]
|
|
59
|
+
if input.get("ttlSeconds") is not None:
|
|
60
|
+
args.extend(["EX", redis_number(input["ttlSeconds"])])
|
|
61
|
+
await self.redis.execute_command(*args)
|
|
62
|
+
return {"ok": True}
|
|
63
|
+
|
|
64
|
+
async def incr(self, input: t.IncrInput) -> t.IncrResult:
|
|
65
|
+
return {
|
|
66
|
+
"value": await self.redis.execute_command(
|
|
67
|
+
"INCRBY", input["key"], redis_number(nullish(input.get("by"), 1))
|
|
68
|
+
)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async def expire(self, input: t.ExpireInput) -> t.ExpireResult:
|
|
72
|
+
return {
|
|
73
|
+
"applied": await self.redis.execute_command(
|
|
74
|
+
"EXPIRE", input["key"], redis_number(input["ttlSeconds"])
|
|
75
|
+
)
|
|
76
|
+
== 1
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async def delete(self, input: t.DeleteInput) -> t.DeleteResult:
|
|
80
|
+
return {
|
|
81
|
+
"deleted": await self.redis.execute_command("DEL", *input["keys"])
|
|
82
|
+
if input["keys"]
|
|
83
|
+
else 0
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async def lock(self, input: t.LockInput) -> t.LockResult:
|
|
87
|
+
result = await self.redis.execute_command(
|
|
88
|
+
"SET", input["key"], input["token"], "EX", redis_number(input["ttlSeconds"]), "NX"
|
|
89
|
+
)
|
|
90
|
+
# redis-py maps OK to True, while ioredis returns the string.
|
|
91
|
+
return {"acquired": result is True or result == "OK"}
|
|
92
|
+
|
|
93
|
+
async def unlock(self, input: t.UnlockInput) -> t.UnlockResult:
|
|
94
|
+
result = await self.redis.execute_command(
|
|
95
|
+
"EVAL", UNLOCK_SCRIPT, 1, input["key"], input["token"]
|
|
96
|
+
)
|
|
97
|
+
return {"released": result == 1}
|
|
98
|
+
|
|
99
|
+
async def list_push(self, input: t.ListPushInput) -> t.ListPushResult:
|
|
100
|
+
if not input["values"]:
|
|
101
|
+
raise ValueError("kv.listPush requires at least one value")
|
|
102
|
+
length = await self.redis.execute_command("RPUSH", input["key"], *input["values"])
|
|
103
|
+
if input.get("ttlSeconds") is not None:
|
|
104
|
+
await self.redis.execute_command(
|
|
105
|
+
"EXPIRE", input["key"], redis_number(input["ttlSeconds"])
|
|
106
|
+
)
|
|
107
|
+
return {"length": length}
|
|
108
|
+
|
|
109
|
+
async def list_range(self, input: t.ListRangeInput) -> t.ListRangeResult:
|
|
110
|
+
return {
|
|
111
|
+
"values": await self.redis.execute_command(
|
|
112
|
+
"LRANGE",
|
|
113
|
+
input["key"],
|
|
114
|
+
redis_number(nullish(input.get("start"), 0)),
|
|
115
|
+
redis_number(nullish(input.get("stop"), -1)),
|
|
116
|
+
)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
@asynccontextmanager
|
|
121
|
+
async def dependencies(deps: Deps | None) -> AsyncIterator[Deps]:
|
|
122
|
+
if deps is not None:
|
|
123
|
+
yield deps
|
|
124
|
+
else:
|
|
125
|
+
url = os.environ.get("REDIS_URL")
|
|
126
|
+
if not url:
|
|
127
|
+
raise ValueError(
|
|
128
|
+
"REDIS_URL is required for the render-kv tasks "
|
|
129
|
+
"(kv.get, set, incr, expire, delete, lock, unlock). "
|
|
130
|
+
"Render injects it for a linked Key Value instance, "
|
|
131
|
+
"reachable over the private network."
|
|
132
|
+
)
|
|
133
|
+
async with Redis.from_url(
|
|
134
|
+
url,
|
|
135
|
+
decode_responses=True,
|
|
136
|
+
retry=Retry(NoBackoff(), 0),
|
|
137
|
+
retry_on_error=[],
|
|
138
|
+
socket_connect_timeout=30,
|
|
139
|
+
socket_timeout=30,
|
|
140
|
+
) as redis:
|
|
141
|
+
yield Deps(render_kv=Client(redis))
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
RenderKvPort = Port
|
|
145
|
+
RenderKvClient = Client
|
|
146
|
+
RenderKvDeps = Deps
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from render import TaskContext
|
|
2
|
+
|
|
3
|
+
from ._app import app
|
|
4
|
+
from .client import Deps, dependencies
|
|
5
|
+
from .retry import KV_RETRY
|
|
6
|
+
from .types import DeleteInput, DeleteResult
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
async def delete_impl(
|
|
10
|
+
ctx: TaskContext, input: DeleteInput, *, deps: Deps | None = None
|
|
11
|
+
) -> DeleteResult:
|
|
12
|
+
async with dependencies(deps) as resolved:
|
|
13
|
+
return await resolved.render_kv.delete(input)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@app.task(name="kv.delete", retry=KV_RETRY)
|
|
17
|
+
async def delete(ctx: TaskContext, input: DeleteInput) -> DeleteResult:
|
|
18
|
+
return await delete_impl(ctx, input)
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from render import TaskContext
|
|
2
|
+
|
|
3
|
+
from ._app import app
|
|
4
|
+
from .client import Deps, dependencies
|
|
5
|
+
from .retry import KV_RETRY
|
|
6
|
+
from .types import ExpireInput, ExpireResult
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
async def expire_impl(
|
|
10
|
+
ctx: TaskContext, input: ExpireInput, *, deps: Deps | None = None
|
|
11
|
+
) -> ExpireResult:
|
|
12
|
+
async with dependencies(deps) as resolved:
|
|
13
|
+
return await resolved.render_kv.expire(input)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@app.task(name="kv.expire", retry=KV_RETRY)
|
|
17
|
+
async def expire(ctx: TaskContext, input: ExpireInput) -> ExpireResult:
|
|
18
|
+
return await expire_impl(ctx, input)
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
from render import TaskContext
|
|
2
|
+
|
|
3
|
+
from ._app import app
|
|
4
|
+
from .client import Deps, dependencies
|
|
5
|
+
from .retry import KV_RETRY
|
|
6
|
+
from .types import GetInput, GetResult
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
async def get_impl(ctx: TaskContext, input: GetInput, *, deps: Deps | None = None) -> GetResult:
|
|
10
|
+
async with dependencies(deps) as resolved:
|
|
11
|
+
return await resolved.render_kv.get(input)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@app.task(name="kv.get", retry=KV_RETRY)
|
|
15
|
+
async def get(ctx: TaskContext, input: GetInput) -> GetResult:
|
|
16
|
+
return await get_impl(ctx, input)
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
from render import TaskContext
|
|
2
|
+
|
|
3
|
+
from ._app import app
|
|
4
|
+
from .client import Deps, dependencies
|
|
5
|
+
from .retry import KV_RETRY
|
|
6
|
+
from .types import IncrInput, IncrResult
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
async def incr_impl(ctx: TaskContext, input: IncrInput, *, deps: Deps | None = None) -> IncrResult:
|
|
10
|
+
async with dependencies(deps) as resolved:
|
|
11
|
+
return await resolved.render_kv.incr(input)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@app.task(name="kv.incr", retry=KV_RETRY)
|
|
15
|
+
async def incr(ctx: TaskContext, input: IncrInput) -> IncrResult:
|
|
16
|
+
return await incr_impl(ctx, input)
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from render import TaskContext
|
|
2
|
+
|
|
3
|
+
from ._app import app
|
|
4
|
+
from .client import Deps, dependencies
|
|
5
|
+
from .retry import KV_RETRY
|
|
6
|
+
from .types import ListPushInput, ListPushResult
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
async def list_push_impl(
|
|
10
|
+
ctx: TaskContext, input: ListPushInput, *, deps: Deps | None = None
|
|
11
|
+
) -> ListPushResult:
|
|
12
|
+
async with dependencies(deps) as resolved:
|
|
13
|
+
return await resolved.render_kv.list_push(input)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@app.task(name="kv.listPush", retry=KV_RETRY)
|
|
17
|
+
async def list_push(ctx: TaskContext, input: ListPushInput) -> ListPushResult:
|
|
18
|
+
return await list_push_impl(ctx, input)
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from render import TaskContext
|
|
2
|
+
|
|
3
|
+
from ._app import app
|
|
4
|
+
from .client import Deps, dependencies
|
|
5
|
+
from .retry import KV_RETRY
|
|
6
|
+
from .types import ListRangeInput, ListRangeResult
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
async def list_range_impl(
|
|
10
|
+
ctx: TaskContext, input: ListRangeInput, *, deps: Deps | None = None
|
|
11
|
+
) -> ListRangeResult:
|
|
12
|
+
async with dependencies(deps) as resolved:
|
|
13
|
+
return await resolved.render_kv.list_range(input)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@app.task(name="kv.listRange", retry=KV_RETRY)
|
|
17
|
+
async def list_range(ctx: TaskContext, input: ListRangeInput) -> ListRangeResult:
|
|
18
|
+
return await list_range_impl(ctx, input)
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
from render import TaskContext
|
|
2
|
+
|
|
3
|
+
from ._app import app
|
|
4
|
+
from .client import Deps, dependencies
|
|
5
|
+
from .retry import KV_RETRY
|
|
6
|
+
from .types import LockInput, LockResult
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
async def lock_impl(ctx: TaskContext, input: LockInput, *, deps: Deps | None = None) -> LockResult:
|
|
10
|
+
async with dependencies(deps) as resolved:
|
|
11
|
+
return await resolved.render_kv.lock(input)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@app.task(name="kv.lock", retry=KV_RETRY)
|
|
15
|
+
async def lock(ctx: TaskContext, input: LockInput) -> LockResult:
|
|
16
|
+
return await lock_impl(ctx, input)
|
|
File without changes
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
from render import TaskContext
|
|
2
|
+
|
|
3
|
+
from ._app import app
|
|
4
|
+
from .client import Deps, dependencies
|
|
5
|
+
from .retry import KV_RETRY
|
|
6
|
+
from .types import SetInput, SetResult
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
async def set_impl(ctx: TaskContext, input: SetInput, *, deps: Deps | None = None) -> SetResult:
|
|
10
|
+
async with dependencies(deps) as resolved:
|
|
11
|
+
return await resolved.render_kv.set(input)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@app.task(name="kv.set", retry=KV_RETRY)
|
|
15
|
+
async def set(ctx: TaskContext, input: SetInput) -> SetResult:
|
|
16
|
+
return await set_impl(ctx, input)
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
from ._app import app
|
|
2
|
+
from .delete import delete, delete_impl
|
|
3
|
+
from .expire import expire, expire_impl
|
|
4
|
+
from .get import get, get_impl
|
|
5
|
+
from .incr import incr, incr_impl
|
|
6
|
+
from .list_push import list_push, list_push_impl
|
|
7
|
+
from .list_range import list_range, list_range_impl
|
|
8
|
+
from .lock import lock, lock_impl
|
|
9
|
+
from .set import set, set_impl
|
|
10
|
+
from .unlock import unlock, unlock_impl
|
|
11
|
+
|
|
12
|
+
__all__ = [
|
|
13
|
+
"app",
|
|
14
|
+
"delete",
|
|
15
|
+
"delete_impl",
|
|
16
|
+
"expire",
|
|
17
|
+
"expire_impl",
|
|
18
|
+
"get",
|
|
19
|
+
"get_impl",
|
|
20
|
+
"incr",
|
|
21
|
+
"incr_impl",
|
|
22
|
+
"list_push",
|
|
23
|
+
"list_push_impl",
|
|
24
|
+
"list_range",
|
|
25
|
+
"list_range_impl",
|
|
26
|
+
"lock",
|
|
27
|
+
"lock_impl",
|
|
28
|
+
"unlock",
|
|
29
|
+
"unlock_impl",
|
|
30
|
+
"set",
|
|
31
|
+
"set_impl",
|
|
32
|
+
]
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"""JSON contracts ported from the pinned TypeScript pack."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import NotRequired, TypedDict
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class GetInput(TypedDict):
|
|
9
|
+
key: str
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class GetResult(TypedDict):
|
|
13
|
+
value: str | None
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class SetInput(TypedDict):
|
|
17
|
+
key: str
|
|
18
|
+
value: str
|
|
19
|
+
ttlSeconds: NotRequired[float]
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class SetResult(TypedDict):
|
|
23
|
+
ok: bool
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class IncrInput(TypedDict):
|
|
27
|
+
key: str
|
|
28
|
+
by: NotRequired[float]
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class IncrResult(TypedDict):
|
|
32
|
+
value: float
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class ExpireInput(TypedDict):
|
|
36
|
+
key: str
|
|
37
|
+
ttlSeconds: float
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class ExpireResult(TypedDict):
|
|
41
|
+
applied: bool
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class DeleteInput(TypedDict):
|
|
45
|
+
keys: list[str]
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class DeleteResult(TypedDict):
|
|
49
|
+
deleted: float
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
class LockInput(TypedDict):
|
|
53
|
+
key: str
|
|
54
|
+
token: str
|
|
55
|
+
ttlSeconds: float
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
class LockResult(TypedDict):
|
|
59
|
+
acquired: bool
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
class UnlockInput(TypedDict):
|
|
63
|
+
key: str
|
|
64
|
+
token: str
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
class UnlockResult(TypedDict):
|
|
68
|
+
released: bool
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
class ListPushInput(TypedDict):
|
|
72
|
+
key: str
|
|
73
|
+
values: list[str]
|
|
74
|
+
ttlSeconds: NotRequired[float]
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
class ListPushResult(TypedDict):
|
|
78
|
+
length: float
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
class ListRangeInput(TypedDict):
|
|
82
|
+
key: str
|
|
83
|
+
start: NotRequired[float]
|
|
84
|
+
stop: NotRequired[float]
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
class ListRangeResult(TypedDict):
|
|
88
|
+
values: list[str]
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from render import TaskContext
|
|
2
|
+
|
|
3
|
+
from ._app import app
|
|
4
|
+
from .client import Deps, dependencies
|
|
5
|
+
from .retry import KV_RETRY
|
|
6
|
+
from .types import UnlockInput, UnlockResult
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
async def unlock_impl(
|
|
10
|
+
ctx: TaskContext, input: UnlockInput, *, deps: Deps | None = None
|
|
11
|
+
) -> UnlockResult:
|
|
12
|
+
async with dependencies(deps) as resolved:
|
|
13
|
+
return await resolved.render_kv.unlock(input)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@app.task(name="kv.unlock", retry=KV_RETRY)
|
|
17
|
+
async def unlock(ctx: TaskContext, input: UnlockInput) -> UnlockResult:
|
|
18
|
+
return await unlock_impl(ctx, input)
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import json
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
from unittest.mock import AsyncMock
|
|
4
|
+
|
|
5
|
+
import pytest
|
|
6
|
+
from redis.exceptions import ConnectionError
|
|
7
|
+
from render_lab_tasks_render_kv import tasks
|
|
8
|
+
from render_lab_tasks_render_kv.client import Client, Deps, dependencies
|
|
9
|
+
from render_lab_tasks_render_kv.retry import KV_RETRY
|
|
10
|
+
|
|
11
|
+
CASES = json.loads((Path(__file__).parents[3] / "scripts/fixtures/batch4-kv.json").read_text())
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@pytest.mark.parametrize("case", CASES, ids=lambda c: c["id"])
|
|
15
|
+
async def test_pinned_ts_contract(case, ctx):
|
|
16
|
+
commands = AsyncMock()
|
|
17
|
+
commands.execute_command.side_effect = [s["result"] for s in case["sdk"]]
|
|
18
|
+
raw = getattr(tasks, case["pythonExport"] + "_impl")
|
|
19
|
+
expected = case["expected"]
|
|
20
|
+
if "error" in expected:
|
|
21
|
+
with pytest.raises(ValueError, match=expected["error"]):
|
|
22
|
+
await raw(ctx, case["input"], deps=Deps(Client(commands)))
|
|
23
|
+
else:
|
|
24
|
+
assert await raw(ctx, case["input"], deps=Deps(Client(commands))) == expected["output"]
|
|
25
|
+
assert [list(c.args) for c in commands.execute_command.call_args_list] == case["pythonCommands"]
|
|
26
|
+
assert KV_RETRY.max_retries == expected["retry"]["maxRetries"]
|
|
27
|
+
assert KV_RETRY.wait_duration_ms == expected["retry"]["waitDurationMs"]
|
|
28
|
+
assert KV_RETRY.backoff_scaling == expected["retry"]["backoffScaling"]
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
async def test_failed_append_does_not_retry(ctx):
|
|
32
|
+
commands = AsyncMock()
|
|
33
|
+
commands.execute_command.side_effect = [3, ConnectionError("lost after append")]
|
|
34
|
+
with pytest.raises(ConnectionError):
|
|
35
|
+
await tasks.list_push_impl(
|
|
36
|
+
ctx, {"key": "x", "values": ["a"], "ttlSeconds": 3}, deps=Deps(Client(commands))
|
|
37
|
+
)
|
|
38
|
+
assert commands.execute_command.call_count == 2
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
async def test_default_connection_is_lazy_and_no_retries(monkeypatch):
|
|
42
|
+
with pytest.raises(ValueError, match="REDIS_URL is required"):
|
|
43
|
+
async with dependencies(None):
|
|
44
|
+
pass
|
|
45
|
+
from render_lab_tasks_render_kv import client
|
|
46
|
+
|
|
47
|
+
created = []
|
|
48
|
+
|
|
49
|
+
def factory(url, **kwargs):
|
|
50
|
+
created.append((url, kwargs))
|
|
51
|
+
return AsyncMock()
|
|
52
|
+
|
|
53
|
+
monkeypatch.setenv("REDIS_URL", "redis://fixture.invalid")
|
|
54
|
+
monkeypatch.setattr(client.Redis, "from_url", factory)
|
|
55
|
+
async with dependencies(None):
|
|
56
|
+
pass
|
|
57
|
+
assert created[0][1]["retry"].get_retries() == 0
|
|
58
|
+
assert created[0][1]["retry_on_error"] == []
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
@pytest.mark.parametrize(
|
|
62
|
+
"method,input,result,expected",
|
|
63
|
+
[
|
|
64
|
+
("incr", {"key": "k", "by": 1.0}, 1, [b"INCRBY", b"k", b"1"]),
|
|
65
|
+
(
|
|
66
|
+
"set",
|
|
67
|
+
{"key": "k", "value": "v", "ttlSeconds": 30.0},
|
|
68
|
+
True,
|
|
69
|
+
[b"SET", b"k", b"v", b"EX", b"30"],
|
|
70
|
+
),
|
|
71
|
+
("expire", {"key": "k", "ttlSeconds": 30.0}, 1, [b"EXPIRE", b"k", b"30"]),
|
|
72
|
+
(
|
|
73
|
+
"lock",
|
|
74
|
+
{"key": "k", "token": "t", "ttlSeconds": 30.0},
|
|
75
|
+
True,
|
|
76
|
+
[b"SET", b"k", b"t", b"EX", b"30", b"NX"],
|
|
77
|
+
),
|
|
78
|
+
(
|
|
79
|
+
"list_range",
|
|
80
|
+
{"key": "k", "start": 0.0, "stop": -1.0},
|
|
81
|
+
[],
|
|
82
|
+
[b"LRANGE", b"k", b"0", b"-1"],
|
|
83
|
+
),
|
|
84
|
+
],
|
|
85
|
+
)
|
|
86
|
+
async def test_actual_redis_encoding(method, input, result, expected):
|
|
87
|
+
from redis._parsers.encoders import Encoder
|
|
88
|
+
|
|
89
|
+
encoder = Encoder("utf-8", "strict", False)
|
|
90
|
+
commands = AsyncMock()
|
|
91
|
+
commands.execute_command.return_value = result
|
|
92
|
+
await getattr(Client(commands), method)(input)
|
|
93
|
+
assert [encoder.encode(v) for v in commands.execute_command.call_args.args] == expected
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def test_fractional_values_are_not_silently_truncated():
|
|
97
|
+
from render_lab_tasks_render_kv.client import redis_number
|
|
98
|
+
|
|
99
|
+
assert redis_number(1.5) == 1.5
|