Skip to content

Commit c9cd8f0

Browse files
Added 'SettingsDict'
1 parent d015083 commit c9cd8f0

File tree

6 files changed

+52
-4
lines changed

6 files changed

+52
-4
lines changed

docs/settings.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
# Settings
22

33
TODO: Discuss settings dict and options available
4-
TODO: Create SettingsDict type
54

65
```python
7-
settings = {
6+
settings_raw_dict = {
87
"queue": queue,
98
"functions": [test],
109
"concurrency": 10,
@@ -14,4 +13,14 @@ settings = {
1413
"before_process": before_process,
1514
"after_process": after_process,
1615
}
16+
settings_typed_dict = SettingsDict(
17+
queue=queue,
18+
functions=[test],
19+
concurrency=10,
20+
cron_jobs=[CronJob(cron, cron="* * * * * */5")], # run every 5 seconds
21+
startup=startup,
22+
shutdown=shutdown,
23+
before_process=before_process,
24+
after_process=after_process,
25+
)
1726
```

examples/map.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ async def sum_of_squares(ctx, *, n):
2424
"functions": [square, sum_of_squares],
2525
"concurrency": 100,
2626
}
27+
# or
28+
# settings = SettingsDict(
29+
# queue=queue,
30+
# functions=[square, sum_of_squares],
31+
# concurrency=100,
32+
# )
2733

2834

2935
async def enqueue():

examples/propagation.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ async def before_process(ctx):
3636
"concurrency": 100,
3737
"before_process": before_process,
3838
}
39+
# or
40+
# settings = SettingsDict(
41+
# queue=queue,
42+
# functions=[recurse],
43+
# concurrency=100,
44+
# before_process"=before_process,
45+
# )
3946

4047

4148
class LoggingContextFilter(logging.Filter):

examples/simple.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ async def cron_job(ctx):
2424
"concurrency": 100,
2525
"cron_jobs": [CronJob(cron_job, cron="* * * * * */5")],
2626
}
27+
# or
28+
# settings = SettingsDict(
29+
# functions=[sleeper, adder],
30+
# concurrency=100,
31+
# cron_jobs=[CronJob(cron_job, cron="* * * * * */5")],
32+
# }
2733

2834

2935
async def enqueue(func, **kwargs):

saq/types.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
from __future__ import annotations
66

77
import typing as t
8+
from collections.abc import Collection
9+
810

911
if t.TYPE_CHECKING:
1012
from asyncio import Task
1113

12-
from saq.job import Job, Status
14+
from saq.job import CronJob, Job, Status
1315
from saq.worker import Worker
1416
from saq.queue import Queue
1517
from typing_extensions import Required
@@ -97,6 +99,23 @@ class PartialTimersDict(TimersDict, total=False):
9799
"""
98100

99101

102+
class SettingsDict(t.TypedDict, total=False):
103+
"""
104+
Settings
105+
"""
106+
107+
queue: Queue
108+
functions: Required[Collection[Function | tuple[str, Function]]]
109+
concurrency: int
110+
cron_jobs: Collection[CronJob]
111+
startup: ReceivesContext
112+
shutdown: ReceivesContext
113+
before_process: ReceivesContext
114+
after_process: ReceivesContext
115+
timers: PartialTimersDict
116+
dequeue_timeout: float
117+
118+
100119
BeforeEnqueueType = t.Callable[["Job"], t.Awaitable[t.Any]]
101120
CountKind = t.Literal["queued", "active", "incomplete"]
102121
DumpType = t.Callable[[t.Mapping[t.Any, t.Any]], str]

saq/worker.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
JobTaskContext,
3232
PartialTimersDict,
3333
ReceivesContext,
34+
SettingsDict,
3435
TimersDict,
3536
)
3637

@@ -310,7 +311,7 @@ async def wrapped(*args: t.Any, **kwargs: t.Any) -> t.Any:
310311
return wrapped
311312

312313

313-
def import_settings(settings: str) -> dict[str, t.Any]:
314+
def import_settings(settings: str) -> SettingsDict:
314315
import importlib
315316

316317
# given a.b.c, parses out a.b as the module path and c as the variable

0 commit comments

Comments
 (0)