Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/servc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:

env:
SERVC_VERSION: 0.4.2
SERVC_VERSION: 0.5.0

permissions:
contents: write
Expand Down
12 changes: 3 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,15 @@ Serv-C implmentation for Python. Documentation can be found [here][1]
Here is the most simple example of use, starting a server to handle requests at the route `my-route`;

```python
from typing import Any, List
from typing import Any

from servc.svc import Middleware
from servc.server import start_server
from servc.svc.com.bus import BusComponent
from servc.svc.com.cache import CacheComponent
from servc.svc.com.worker.types import EMIT_EVENT, RESOLVER_RETURN_TYPE
from servc.svc.com.worker.types import RESOLVER_CONTEXT, RESOLVER_RETURN_TYPE

def inputProcessor(
messageId: str,
bus: BusComponent,
cache: CacheComponent,
payload: Any,
components: List[Middleware],
emitEvent: EMIT_EVENT,
context: RESOLVER_CONTEXT,
) -> RESOLVER_RETURN_TYPE:
return True

Expand Down
42 changes: 10 additions & 32 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
#!/usr/bin/env python

import os
from typing import Any, List
from typing import Any

from servc.server import start_server
from servc.svc import Middleware
from servc.svc.client.send import sendMessage
from servc.svc.com.bus import BusComponent
from servc.svc.com.cache import CacheComponent
from servc.svc.com.worker.types import EMIT_EVENT, RESOLVER_RETURN_TYPE
from servc.svc.com.worker.types import RESOLVER_CONTEXT, RESOLVER_RETURN_TYPE
from servc.svc.idgen.simple import simple
from servc.svc.io.input import InputType


def test_resolver(
id: str,
bus: BusComponent,
cache: CacheComponent,
payload: str | list[str],
_c: List[Middleware],
emitEvent: EMIT_EVENT,
id: str, payload: Any, context: RESOLVER_CONTEXT
) -> RESOLVER_RETURN_TYPE:
if not isinstance(payload, list):
sendMessage(
Expand All @@ -34,41 +26,27 @@ def test_resolver(
"inputs": payload,
},
},
bus,
cache,
context["bus"],
context["cache"],
simple,
)
return False
for x in payload:
if not isinstance(x, str):
return False

emitEvent(
context["bus"].emitEvent(
os.getenv("EVENT", "my-event"),
payload,
)
return True


def test_hook(
id: str,
_b: BusComponent,
_c: CacheComponent,
p: List[Any],
_ch: List[Middleware],
_e: EMIT_EVENT,
) -> RESOLVER_RETURN_TYPE:
return [x for x in p]
def test_hook(id: str, payload: Any, context: RESOLVER_CONTEXT) -> RESOLVER_RETURN_TYPE:
return [x for x in payload]


def fail(
id: str,
_b: BusComponent,
_c: CacheComponent,
_p: Any,
_ch: List[Middleware],
_e: EMIT_EVENT,
) -> RESOLVER_RETURN_TYPE:
def fail(id: str, payload: Any, context: RESOLVER_CONTEXT) -> RESOLVER_RETURN_TYPE:
raise Exception("This is a test exception")


Expand All @@ -77,7 +55,7 @@ def main():
resolver={
"test": test_resolver,
"fail": fail,
"hook": lambda id, _b, _c, p, _ch, _e: len(p),
"hook": lambda id, p, _c: len(p),
"hook_part": test_hook,
},
)
Expand Down
36 changes: 9 additions & 27 deletions servc/server.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from multiprocessing import Process
from typing import Any, List, Tuple
from typing import List

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


COMPONENT_ARRAY = List[Tuple[type[Middleware], List[Any]]]


def compose_components(component_list: COMPONENT_ARRAY) -> List[Middleware]:
components: List[Middleware] = []
for [componentClass, args] in component_list:
components.append(componentClass(*args))
return components
COMPONENT_ARRAY = List[type[Middleware]]


def start_consumer(
Expand All @@ -38,23 +31,18 @@ def start_consumer(
):
config = configClass()
config.setAll(configDictionary)
bus = busClass(
config.get("conf.bus.url"),
config.get("conf.bus.routemap"),
config.get("conf.bus.prefix"),
)
cache = cacheClass(config.get("conf.cache.url"))
bus = busClass(config.get(f"conf.{busClass.name}"))
cache = cacheClass(config.get(f"conf.{cacheClass.name}"))

consumer = workerClass(
config.get("conf.bus.route"),
config.get("conf.instanceid"),
resolver,
eventResolver,
onConsuming,
bus,
busClass,
cache,
config,
compose_components(components),
[X(config.get(f"conf.{X.name}")) for X in components],
)
consumer.connect()

Expand Down Expand Up @@ -93,18 +81,12 @@ def start_server(
)
consumer.start()

bus = busClass(
config.get("conf.bus.url"),
config.get("conf.bus.routemap"),
config.get("conf.bus.prefix"),
)
cache = cacheClass(config.get("conf.cache.url"))
bus = busClass(config.get(f"conf.{busClass.name}"))
cache = cacheClass(config.get(f"conf.{cacheClass.name}"))
http = httpClass(
int(config.get("conf.http.port")),
config.get(f"conf.{httpClass.name}"),
bus,
cache,
config.get("conf.bus.route"),
config.get("conf.instanceid"),
consumer,
resolver,
eventResolver,
Expand Down
10 changes: 4 additions & 6 deletions servc/svc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from enum import Enum
from typing import Callable, List

from servc.svc.config import Config


class ComponentType(Enum):
BUS = "bus"
Expand All @@ -16,7 +18,7 @@ class ComponentType(Enum):
class Middleware:
_children: List[Middleware]

_name: str
name: str

_isReady: bool

Expand All @@ -28,15 +30,11 @@ class Middleware:

_close: Callable[..., bool]

def __init__(self):
def __init__(self, _config: Config):
self._children = []
self._isReady = False
self._isOpen = False

@property
def name(self) -> str:
return self._name

@property
def isReady(self) -> bool:
isReadyCheck = self._isReady
Expand Down
43 changes: 32 additions & 11 deletions servc/svc/com/bus/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Any, Callable, Union
from typing import Any, Callable, Dict, Union

from servc.svc import ComponentType, Middleware
from servc.svc.config import Config
from servc.svc.io.input import EventPayload, InputPayload, InputType
from servc.svc.io.output import StatusCode

Expand All @@ -10,20 +11,40 @@


class BusComponent(Middleware):
name: str = "bus"

_type: ComponentType = ComponentType.BUS

_url: str

_routeMap: dict
_routeMap: Dict[str, str]

_prefix: str

def __init__(self, url: str, routeMap: dict, prefix: str):
super().__init__()
_instanceId: str

_route: str

def __init__(self, config: Config):
super().__init__(config)

self._url = str(config.get("url"))
self._prefix = str(config.get("prefix"))
self._instanceId = str(config.get("instanceid"))
self._route = str(config.get("route"))

routemap = config.get("routemap")
if routemap is None or not isinstance(routemap, dict):
routemap = {}
self._routeMap = routemap

@property
def instanceId(self) -> str:
return self._instanceId

self._url = url
self._routeMap = routeMap
self._prefix = prefix
@property
def route(self) -> str:
return self._route

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

def emitEvent(self, event: str, instanceId: str, details: Any) -> bool:
def emitEvent(self, event: str, details: Any) -> bool:
return self.publishMessage(
self.getRoute(event),
{
"type": InputType.EVENT.value,
"route": self.getRoute(event),
"event": event,
"details": details,
"instanceId": instanceId,
"instanceId": self._instanceId,
},
)

def create_queue(self, queue: str, bindEventExchange: bool = True) -> bool:
def create_queue(self, queue: str, bindEventExchange: bool) -> bool:
return False

def delete_queue(self, queue: str) -> bool:
Expand All @@ -59,6 +80,6 @@ def subscribe(
route: str,
inputProcessor: InputProcessor,
onConsuming: OnConsuming | None,
bindEventExchange: bool = True,
bindEventExchange: bool,
) -> bool:
return True
4 changes: 2 additions & 2 deletions servc/svc/com/bus/rabbitmq.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def get_channel(self, method: Callable | None, args: Tuple | None):
on_open_callback=lambda c: on_channel_open(c, method, args)
)

def create_queue(self, queue: str, bindEventExchange: bool = False, channel: pika.channel.Channel | None = None) -> bool: # type: ignore
def create_queue(self, queue: str, bindEventExchange: bool, channel: pika.channel.Channel | None = None) -> bool: # type: ignore
if not self.isReady:
return self._connect(self.create_queue, (queue, bindEventExchange))
if not channel:
Expand Down Expand Up @@ -180,7 +180,7 @@ def subscribe( # type: ignore
route: str,
inputProcessor: InputProcessor,
onConsuming: OnConsuming | None,
bindEventExchange: bool = True,
bindEventExchange: bool,
channel: pika.channel.Channel | None = None,
) -> bool:
if not self.isReady:
Expand Down
11 changes: 5 additions & 6 deletions servc/svc/com/cache/__init__.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
from typing import Any

from servc.svc import ComponentType, Middleware
from servc.svc.config import Config
from servc.svc.io.output import StatusCode
from servc.svc.io.response import generateResponseArtifact


class CacheComponent(Middleware):
_type: ComponentType = ComponentType.CACHE

_url: str
name: str = "cache"

def __init__(self, url: str):
super().__init__()
_type: ComponentType = ComponentType.CACHE

self._url = url
def __init__(self, config: Config):
super().__init__(config)

def setKey(self, id: str, value: Any) -> str:
return ""
Expand Down
7 changes: 7 additions & 0 deletions servc/svc/com/cache/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from redis import Redis

from servc.svc.com.cache import CacheComponent
from servc.svc.config import Config


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

_url: str

def __init__(self, config: Config):
super().__init__(config)
self._url = str(config.get("url"))

@property
def conn(self):
return self._redisClient
Expand Down
Loading
Loading