Skip to content

Commit c854142

Browse files
committed
feat: upgrade to latest servc spec
1 parent 0cdd713 commit c854142

File tree

23 files changed

+218
-261
lines changed

23 files changed

+218
-261
lines changed

.github/workflows/servc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ on:
44
push:
55

66
env:
7-
SERVC_VERSION: 0.4.2
7+
SERVC_VERSION: 0.5.0
88

99
permissions:
1010
contents: write

README.md

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,15 @@ Serv-C implmentation for Python. Documentation can be found [here][1]
1313
Here is the most simple example of use, starting a server to handle requests at the route `my-route`;
1414

1515
```python
16-
from typing import Any, List
16+
from typing import Any
1717

18-
from servc.svc import Middleware
1918
from servc.server import start_server
20-
from servc.svc.com.bus import BusComponent
21-
from servc.svc.com.cache import CacheComponent
22-
from servc.svc.com.worker.types import EMIT_EVENT, RESOLVER_RETURN_TYPE
19+
from servc.svc.com.worker.types import RESOLVER_CONTEXT, RESOLVER_RETURN_TYPE
2320

2421
def inputProcessor(
2522
messageId: str,
26-
bus: BusComponent,
27-
cache: CacheComponent,
2823
payload: Any,
29-
components: List[Middleware],
30-
emitEvent: EMIT_EVENT,
24+
context: RESOLVER_CONTEXT,
3125
) -> RESOLVER_RETURN_TYPE:
3226
return True
3327

main.py

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,17 @@
11
#!/usr/bin/env python
22

33
import os
4-
from typing import Any, List
4+
from typing import Any
55

66
from servc.server import start_server
7-
from servc.svc import Middleware
87
from servc.svc.client.send import sendMessage
9-
from servc.svc.com.bus import BusComponent
10-
from servc.svc.com.cache import CacheComponent
11-
from servc.svc.com.worker.types import EMIT_EVENT, RESOLVER_RETURN_TYPE
8+
from servc.svc.com.worker.types import RESOLVER_CONTEXT, RESOLVER_RETURN_TYPE
129
from servc.svc.idgen.simple import simple
1310
from servc.svc.io.input import InputType
1411

1512

1613
def test_resolver(
17-
id: str,
18-
bus: BusComponent,
19-
cache: CacheComponent,
20-
payload: str | list[str],
21-
_c: List[Middleware],
22-
emitEvent: EMIT_EVENT,
14+
id: str, payload: Any, context: RESOLVER_CONTEXT
2315
) -> RESOLVER_RETURN_TYPE:
2416
if not isinstance(payload, list):
2517
sendMessage(
@@ -34,41 +26,27 @@ def test_resolver(
3426
"inputs": payload,
3527
},
3628
},
37-
bus,
38-
cache,
29+
context["bus"],
30+
context["cache"],
3931
simple,
4032
)
4133
return False
4234
for x in payload:
4335
if not isinstance(x, str):
4436
return False
4537

46-
emitEvent(
38+
context["bus"].emitEvent(
4739
os.getenv("EVENT", "my-event"),
4840
payload,
4941
)
5042
return True
5143

5244

53-
def test_hook(
54-
id: str,
55-
_b: BusComponent,
56-
_c: CacheComponent,
57-
p: List[Any],
58-
_ch: List[Middleware],
59-
_e: EMIT_EVENT,
60-
) -> RESOLVER_RETURN_TYPE:
61-
return [x for x in p]
45+
def test_hook(id: str, payload: Any, context: RESOLVER_CONTEXT) -> RESOLVER_RETURN_TYPE:
46+
return [x for x in payload]
6247

6348

64-
def fail(
65-
id: str,
66-
_b: BusComponent,
67-
_c: CacheComponent,
68-
_p: Any,
69-
_ch: List[Middleware],
70-
_e: EMIT_EVENT,
71-
) -> RESOLVER_RETURN_TYPE:
49+
def fail(id: str, payload: Any, context: RESOLVER_CONTEXT) -> RESOLVER_RETURN_TYPE:
7250
raise Exception("This is a test exception")
7351

7452

@@ -77,7 +55,7 @@ def main():
7755
resolver={
7856
"test": test_resolver,
7957
"fail": fail,
80-
"hook": lambda id, _b, _c, p, _ch, _e: len(p),
58+
"hook": lambda id, p, _c: len(p),
8159
"hook_part": test_hook,
8260
},
8361
)

servc/server.py

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from multiprocessing import Process
2-
from typing import Any, List, Tuple
2+
from typing import List
33

44
from servc.svc import Middleware
55
from servc.svc.com.bus import BusComponent, OnConsuming
@@ -15,14 +15,7 @@ def blankOnConsuming(route: str):
1515
print("Consuming on route", route, flush=True)
1616

1717

18-
COMPONENT_ARRAY = List[Tuple[type[Middleware], List[Any]]]
19-
20-
21-
def compose_components(component_list: COMPONENT_ARRAY) -> List[Middleware]:
22-
components: List[Middleware] = []
23-
for [componentClass, args] in component_list:
24-
components.append(componentClass(*args))
25-
return components
18+
COMPONENT_ARRAY = List[type[Middleware]]
2619

2720

2821
def start_consumer(
@@ -38,23 +31,18 @@ def start_consumer(
3831
):
3932
config = configClass()
4033
config.setAll(configDictionary)
41-
bus = busClass(
42-
config.get("conf.bus.url"),
43-
config.get("conf.bus.routemap"),
44-
config.get("conf.bus.prefix"),
45-
)
46-
cache = cacheClass(config.get("conf.cache.url"))
34+
bus = busClass(config.get(f"conf.{busClass.name}"))
35+
cache = cacheClass(config.get(f"conf.{cacheClass.name}"))
36+
4737
consumer = workerClass(
48-
config.get("conf.bus.route"),
49-
config.get("conf.instanceid"),
5038
resolver,
5139
eventResolver,
5240
onConsuming,
5341
bus,
5442
busClass,
5543
cache,
5644
config,
57-
compose_components(components),
45+
[X(config.get(f"conf.{X.name}")) for X in components],
5846
)
5947
consumer.connect()
6048

@@ -93,18 +81,12 @@ def start_server(
9381
)
9482
consumer.start()
9583

96-
bus = busClass(
97-
config.get("conf.bus.url"),
98-
config.get("conf.bus.routemap"),
99-
config.get("conf.bus.prefix"),
100-
)
101-
cache = cacheClass(config.get("conf.cache.url"))
84+
bus = busClass(config.get(f"conf.{busClass.name}"))
85+
cache = cacheClass(config.get(f"conf.{cacheClass.name}"))
10286
http = httpClass(
103-
int(config.get("conf.http.port")),
87+
config.get(f"conf.{httpClass.name}"),
10488
bus,
10589
cache,
106-
config.get("conf.bus.route"),
107-
config.get("conf.instanceid"),
10890
consumer,
10991
resolver,
11092
eventResolver,

servc/svc/__init__.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from enum import Enum
44
from typing import Callable, List
55

6+
from servc.svc.config import Config
7+
68

79
class ComponentType(Enum):
810
BUS = "bus"
@@ -16,7 +18,7 @@ class ComponentType(Enum):
1618
class Middleware:
1719
_children: List[Middleware]
1820

19-
_name: str
21+
name: str
2022

2123
_isReady: bool
2224

@@ -28,15 +30,11 @@ class Middleware:
2830

2931
_close: Callable[..., bool]
3032

31-
def __init__(self):
33+
def __init__(self, _config: Config):
3234
self._children = []
3335
self._isReady = False
3436
self._isOpen = False
3537

36-
@property
37-
def name(self) -> str:
38-
return self._name
39-
4038
@property
4139
def isReady(self) -> bool:
4240
isReadyCheck = self._isReady

servc/svc/com/bus/__init__.py

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
from typing import Any, Callable, Union
1+
from typing import Any, Callable, Dict, Union
22

33
from servc.svc import ComponentType, Middleware
4+
from servc.svc.config import Config
45
from servc.svc.io.input import EventPayload, InputPayload, InputType
56
from servc.svc.io.output import StatusCode
67

@@ -10,20 +11,40 @@
1011

1112

1213
class BusComponent(Middleware):
14+
name: str = "bus"
15+
1316
_type: ComponentType = ComponentType.BUS
1417

1518
_url: str
1619

17-
_routeMap: dict
20+
_routeMap: Dict[str, str]
1821

1922
_prefix: str
2023

21-
def __init__(self, url: str, routeMap: dict, prefix: str):
22-
super().__init__()
24+
_instanceId: str
25+
26+
_route: str
27+
28+
def __init__(self, config: Config):
29+
super().__init__(config)
30+
31+
self._url = str(config.get("url"))
32+
self._prefix = str(config.get("prefix"))
33+
self._instanceId = str(config.get("instanceid"))
34+
self._route = str(config.get("route"))
35+
36+
routemap = config.get("routemap")
37+
if routemap is None or not isinstance(routemap, dict):
38+
routemap = {}
39+
self._routeMap = routemap
40+
41+
@property
42+
def instanceId(self) -> str:
43+
return self._instanceId
2344

24-
self._url = url
25-
self._routeMap = routeMap
26-
self._prefix = prefix
45+
@property
46+
def route(self) -> str:
47+
return self._route
2748

2849
def getRoute(self, route: str) -> str:
2950
if route in self._routeMap:
@@ -33,19 +54,19 @@ def getRoute(self, route: str) -> str:
3354
def publishMessage(self, route: str, message: InputPayload | EventPayload) -> bool:
3455
return True
3556

36-
def emitEvent(self, event: str, instanceId: str, details: Any) -> bool:
57+
def emitEvent(self, event: str, details: Any) -> bool:
3758
return self.publishMessage(
3859
self.getRoute(event),
3960
{
4061
"type": InputType.EVENT.value,
4162
"route": self.getRoute(event),
4263
"event": event,
4364
"details": details,
44-
"instanceId": instanceId,
65+
"instanceId": self._instanceId,
4566
},
4667
)
4768

48-
def create_queue(self, queue: str, bindEventExchange: bool = True) -> bool:
69+
def create_queue(self, queue: str, bindEventExchange: bool) -> bool:
4970
return False
5071

5172
def delete_queue(self, queue: str) -> bool:
@@ -59,6 +80,6 @@ def subscribe(
5980
route: str,
6081
inputProcessor: InputProcessor,
6182
onConsuming: OnConsuming | None,
62-
bindEventExchange: bool = True,
83+
bindEventExchange: bool,
6384
) -> bool:
6485
return True

servc/svc/com/bus/rabbitmq.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def get_channel(self, method: Callable | None, args: Tuple | None):
108108
on_open_callback=lambda c: on_channel_open(c, method, args)
109109
)
110110

111-
def create_queue(self, queue: str, bindEventExchange: bool = False, channel: pika.channel.Channel | None = None) -> bool: # type: ignore
111+
def create_queue(self, queue: str, bindEventExchange: bool, channel: pika.channel.Channel | None = None) -> bool: # type: ignore
112112
if not self.isReady:
113113
return self._connect(self.create_queue, (queue, bindEventExchange))
114114
if not channel:
@@ -180,7 +180,7 @@ def subscribe( # type: ignore
180180
route: str,
181181
inputProcessor: InputProcessor,
182182
onConsuming: OnConsuming | None,
183-
bindEventExchange: bool = True,
183+
bindEventExchange: bool,
184184
channel: pika.channel.Channel | None = None,
185185
) -> bool:
186186
if not self.isReady:

servc/svc/com/cache/__init__.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
from typing import Any
22

33
from servc.svc import ComponentType, Middleware
4+
from servc.svc.config import Config
45
from servc.svc.io.output import StatusCode
56
from servc.svc.io.response import generateResponseArtifact
67

78

89
class CacheComponent(Middleware):
9-
_type: ComponentType = ComponentType.CACHE
10-
11-
_url: str
10+
name: str = "cache"
1211

13-
def __init__(self, url: str):
14-
super().__init__()
12+
_type: ComponentType = ComponentType.CACHE
1513

16-
self._url = url
14+
def __init__(self, config: Config):
15+
super().__init__(config)
1716

1817
def setKey(self, id: str, value: Any) -> str:
1918
return ""

servc/svc/com/cache/redis.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from redis import Redis
88

99
from servc.svc.com.cache import CacheComponent
10+
from servc.svc.config import Config
1011

1112

1213
def decimal_default(obj: Any) -> None | str | float:
@@ -20,6 +21,12 @@ def decimal_default(obj: Any) -> None | str | float:
2021
class CacheRedis(CacheComponent):
2122
_redisClient: Redis
2223

24+
_url: str
25+
26+
def __init__(self, config: Config):
27+
super().__init__(config)
28+
self._url = str(config.get("url"))
29+
2330
@property
2431
def conn(self):
2532
return self._redisClient

0 commit comments

Comments
 (0)