-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrrr_demo.py
executable file
·235 lines (178 loc) · 5.7 KB
/
brrr_demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/usr/bin/env python3
import asyncio
from contextlib import asynccontextmanager
from collections.abc import AsyncIterator
import logging
import logging.config
import json
import os
from pprint import pprint
import sys
from typing import Iterable
import aioboto3
from aiohttp import web
import redis.asyncio as redis
from types_aiobotocore_dynamodb import DynamoDBClient
from brrr.backends.redis import RedisStream
from brrr.backends.dynamo import DynamoDbMemStore
import brrr
from brrr import task
logger = logging.getLogger(__name__)
routes = web.RouteTableDef()
def table_name() -> str:
"""
Get table name from environment
"""
return os.environ.get("DYNAMODB_TABLE_NAME", "brrr")
def response(status: int, content: dict):
return web.Response(status=status, text=json.dumps(content))
@routes.get("/{task_name}")
async def get_task_result(request: web.BaseRequest):
# aiohttp uses a multidict but we don’t need that for this demo.
kwargs = dict(request.query)
task_name = request.match_info["task_name"]
if task_name not in brrr.tasks:
return response(404, {"error": "No such task"})
try:
result = await brrr.read(task_name, (), kwargs)
except KeyError:
return response(404, dict(error="No result for this task"))
return response(200, dict(status="ok", result=result))
@routes.post("/{task_name}")
async def schedule_task(request: web.BaseRequest):
kwargs = dict(request.query)
task_name = request.match_info["task_name"]
if task_name not in brrr.tasks:
return response(404, {"error": "No such task"})
await brrr.schedule(task_name, (), kwargs)
return response(202, {"status": "accepted"})
# ... where is the python contextmanager monad?
@asynccontextmanager
async def with_redis() -> AsyncIterator[redis.Redis]:
redurl = os.environ.get("BRRR_DEMO_REDIS_URL")
rkwargs = dict(
decode_responses=True,
health_check_interval=10,
socket_connect_timeout=5,
retry_on_timeout=True,
socket_keepalive=True,
protocol=3,
)
if redurl is None:
rc = redis.Redis(**rkwargs)
else:
rc = redis.from_url(redurl, **rkwargs)
await rc.ping()
try:
yield rc
finally:
await rc.aclose()
@asynccontextmanager
async def with_resources() -> AsyncIterator[tuple[redis.Redis, DynamoDBClient]]:
async with with_redis() as rc:
async with aioboto3.Session().client("dynamodb") as dync:
dync: DynamoDBClient
yield (rc, dync)
@asynccontextmanager
async def with_brrr_wrap() -> AsyncIterator[tuple[RedisStream, DynamoDbMemStore]]:
async with with_resources() as (rc, dync):
store = DynamoDbMemStore(dync, table_name())
queue = RedisStream(rc, os.environ.get("REDIS_QUEUE_KEY", "r1"))
yield (queue, store)
@asynccontextmanager
async def with_brrr(reset_backends):
async with with_brrr_wrap() as (queue, store):
if reset_backends:
await queue.setup()
await store.create_table()
brrr.setup(queue, store)
yield
@task
async def fib(n: int, salt=None):
match n:
case 0 | 1:
return n
case _:
return sum(await fib.map([[n - 2, salt], [n - 1, salt]]))
@task
async def fib_and_print(n: str, salt=None):
f = await fib(int(n), salt)
print(f"fib({n}) = {f}", flush=True)
return f
@task
async def hello(greetee: str):
greeting = f"Hello, {greetee}!"
print(greeting, flush=True)
return greeting
cmds = {}
def cmd(f):
cmds[f.__name__] = f
return f
@cmd
async def worker():
async with with_brrr(False):
await brrr.wrrrk()
@cmd
async def server():
bind_addr = os.environ.get("BRRR_DEMO_LISTEN_HOST", "127.0.0.1")
bind_port = int(os.environ.get("BRRR_DEMO_LISTEN_PORT", "8080"))
async with with_brrr(True):
app = web.Application()
app.add_routes(routes)
runner = web.AppRunner(app)
await runner.setup()
site = web.TCPSite(runner, bind_addr, bind_port)
await site.start()
logger.info(f"Listening on http://{bind_addr}:{bind_port}")
await asyncio.Event().wait()
def args2dict(args: Iterable[str]) -> dict[str, str]:
"""
Extremely rudimentary arbitrary argparser.
args2dict(["--foo", "bar", "--zim", "zom"])
=> {"foo": "bar", "zim": "zom"}
"""
it = iter(args)
return {k.lstrip("-"): v for k, v in zip(it, it)}
@cmd
async def schedule(job: str, *args: str):
"""
Put a single job onto the queue
"""
async with with_brrr(False):
await brrr.schedule(job, (), args2dict(args))
@cmd
async def monitor():
async with with_brrr_wrap() as (queue, _):
while True:
pprint(await queue.get_info())
await asyncio.sleep(1)
@cmd
async def reset():
async with with_resources() as (rc, dync):
try:
await dync.delete_table(TableName=table_name())
except Exception as e:
# Table does not exist
if "ResourceNotFoundException" not in str(e):
raise
await rc.flushall()
async def amain():
# To log _all_ messages at DEBGUG level (very noisy)
# logging.basicConfig(level=logging.DEBUG)
logging.basicConfig()
logger.setLevel(logging.DEBUG)
# To log all brrr messages at DEBUG level (quite noisy)
# logging.getLogger('brrr').setLevel(logging.DEBUG)
f = cmds.get(sys.argv[1]) if len(sys.argv) > 1 else None
if f:
await f(*sys.argv[2:])
else:
print(f"Usage: brrr_demo.py <{" | ".join(cmds.keys())}>")
sys.exit(1)
def main():
try:
asyncio.run(amain())
except KeyboardInterrupt:
pass
if __name__ == "__main__":
main()