jararaca 0.2.37a10__py3-none-any.whl → 0.2.37a12__py3-none-any.whl
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.
- jararaca/__init__.py +7 -0
- jararaca/cli.py +78 -4
- jararaca/messagebus/interceptors/aiopika_publisher_interceptor.py +66 -11
- jararaca/messagebus/worker.py +11 -11
- jararaca-0.2.37a12.dist-info/METADATA +154 -0
- {jararaca-0.2.37a10.dist-info → jararaca-0.2.37a12.dist-info}/RECORD +9 -12
- {jararaca-0.2.37a10.dist-info → jararaca-0.2.37a12.dist-info}/WHEEL +1 -1
- README.md +0 -243
- jararaca-0.2.37a10.dist-info/LICENSE +0 -674
- jararaca-0.2.37a10.dist-info/METADATA +0 -278
- pyproject.toml +0 -77
- /LICENSE → /jararaca-0.2.37a12.dist-info/LICENSE +0 -0
- {jararaca-0.2.37a10.dist-info → jararaca-0.2.37a12.dist-info}/entry_points.txt +0 -0
README.md
DELETED
|
@@ -1,243 +0,0 @@
|
|
|
1
|
-
<img src="https://raw.githubusercontent.com/LuscasLeo/jararaca/main/docs/assets/_f04774c9-7e05-4da4-8b17-8be23f6a1475.jpeg" alt="README.md" width="250" float="right">
|
|
2
|
-
|
|
3
|
-
# Jararaca Microservice Framework
|
|
4
|
-
|
|
5
|
-
## Overview
|
|
6
|
-
|
|
7
|
-
Jararaca is a aio-first microservice framework that provides a set of tools to build and deploy microservices in a simple and clear way.
|
|
8
|
-
|
|
9
|
-
## Features
|
|
10
|
-
|
|
11
|
-
### Hexagonal Architecture
|
|
12
|
-
|
|
13
|
-
The framework is based on the hexagonal architecture, which allows you to separate the business logic from the infrastructure, making the code more testable and maintainable.
|
|
14
|
-
|
|
15
|
-
### Dependency Injection
|
|
16
|
-
|
|
17
|
-
The framework uses the dependency injection pattern to manage the dependencies between the components of the application.
|
|
18
|
-
|
|
19
|
-
```py
|
|
20
|
-
app = Microservice(
|
|
21
|
-
providers=[
|
|
22
|
-
ProviderSpec(
|
|
23
|
-
provide=Token(AuthConfig, "AUTH_CONFIG"),
|
|
24
|
-
use_value=AuthConfig(
|
|
25
|
-
secret="secret",
|
|
26
|
-
identity_refresh_token_expires_delta_seconds=60 * 60 * 24 * 30,
|
|
27
|
-
identity_token_expires_delta_seconds=60 * 60,
|
|
28
|
-
),
|
|
29
|
-
),
|
|
30
|
-
ProviderSpec(
|
|
31
|
-
provide=Token(AppConfig, "APP_CONFIG"),
|
|
32
|
-
use_factory=AppConfig.provider,
|
|
33
|
-
),
|
|
34
|
-
ProviderSpec(
|
|
35
|
-
provide=TokenBlackListService,
|
|
36
|
-
use_value=InMemoryTokenBlackListService(),
|
|
37
|
-
),
|
|
38
|
-
],
|
|
39
|
-
)
|
|
40
|
-
```
|
|
41
|
-
|
|
42
|
-
### Web Server Port
|
|
43
|
-
|
|
44
|
-
The framework provides a web server that listens on a specific port and routes the requests to the appropriate handler. It uses [FastAPI](https://fastapi.tiangolo.com/) as the web framework.
|
|
45
|
-
|
|
46
|
-
```py
|
|
47
|
-
@Delete("/{task_id}")
|
|
48
|
-
async def delete_task(self, task_id: TaskId) -> None:
|
|
49
|
-
await self.tasks_crud.delete_by_id(task_id)
|
|
50
|
-
|
|
51
|
-
await use_ws_manager().broadcast(("Task %s deleted" % task_id).encode())
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
### Message Bus
|
|
55
|
-
|
|
56
|
-
The framework provides a topic-based message bus that allows you to send messages between the components of the application. It uses [AIO Pika](https://aio-pika.readthedocs.io/) as the message broker worker and publisher.
|
|
57
|
-
|
|
58
|
-
```py
|
|
59
|
-
@IncomingHandler("task")
|
|
60
|
-
async def process_task(self, message: Message[Identifiable[TaskSchema]]) -> None:
|
|
61
|
-
name = generate_random_name()
|
|
62
|
-
now = asyncio.get_event_loop().time()
|
|
63
|
-
print("Processing task: ", name)
|
|
64
|
-
|
|
65
|
-
task = message.payload()
|
|
66
|
-
|
|
67
|
-
print("Received task: ", task)
|
|
68
|
-
await asyncio.sleep(random.randint(1, 5))
|
|
69
|
-
|
|
70
|
-
await use_publisher().publish(task, topic="task")
|
|
71
|
-
|
|
72
|
-
then = asyncio.get_event_loop().time()
|
|
73
|
-
print("Task Finished: ", name, " Time: ", then - now)
|
|
74
|
-
```
|
|
75
|
-
|
|
76
|
-
### Distributed Websocket
|
|
77
|
-
|
|
78
|
-
You can setup a room-based websocket server that allows you to send messages to a specific room or broadcast messages to all connected clients. All backend instances communicates with each other using a pub/sub mechanism (such as Redis).
|
|
79
|
-
|
|
80
|
-
```py
|
|
81
|
-
@WebSocketEndpoint("/ws")
|
|
82
|
-
async def ws_endpoint(self, websocket: WebSocket) -> None:
|
|
83
|
-
await websocket.accept()
|
|
84
|
-
counter.increment()
|
|
85
|
-
await use_ws_manager().add_websocket(websocket)
|
|
86
|
-
await use_ws_manager().join(["tasks"], websocket)
|
|
87
|
-
await use_ws_manager().broadcast(
|
|
88
|
-
("New Connection (%d) from %s" % (counter.count, self.hostname)).encode()
|
|
89
|
-
)
|
|
90
|
-
|
|
91
|
-
print("New Connection (%d)" % counter.count)
|
|
92
|
-
|
|
93
|
-
while True:
|
|
94
|
-
try:
|
|
95
|
-
await websocket.receive_text()
|
|
96
|
-
except WebSocketDisconnect:
|
|
97
|
-
counter.decrement()
|
|
98
|
-
await use_ws_manager().remove_websocket(websocket)
|
|
99
|
-
|
|
100
|
-
await use_ws_manager().broadcast(
|
|
101
|
-
(
|
|
102
|
-
"Connection Closed (%d) from %s"
|
|
103
|
-
% (counter.count, self.hostname)
|
|
104
|
-
).encode()
|
|
105
|
-
)
|
|
106
|
-
print("Connection Closed (%d)" % counter.count)
|
|
107
|
-
break
|
|
108
|
-
```
|
|
109
|
-
|
|
110
|
-
### Scheduled Routine
|
|
111
|
-
|
|
112
|
-
You can setup a scheduled routine that runs a specific task at a specific time or interval.
|
|
113
|
-
|
|
114
|
-
```py
|
|
115
|
-
...
|
|
116
|
-
@ScheduledAction("* * * * * */3", allow_overlap=False)
|
|
117
|
-
async def scheduled_task(self) -> None:
|
|
118
|
-
print("Scheduled Task at ", asyncio.get_event_loop().time())
|
|
119
|
-
|
|
120
|
-
print("sleeping")
|
|
121
|
-
await asyncio.sleep(5)
|
|
122
|
-
|
|
123
|
-
await use_publisher().publish(
|
|
124
|
-
message=Identifiable(
|
|
125
|
-
id=uuid4(),
|
|
126
|
-
data=TaskSchema(name=generate_random_name()),
|
|
127
|
-
),
|
|
128
|
-
topic="task",
|
|
129
|
-
)
|
|
130
|
-
```
|
|
131
|
-
|
|
132
|
-
### Observability
|
|
133
|
-
|
|
134
|
-
You can setup Observability Interceptors for logs, traces and metric collection with [OpenTelemetry](https://opentelemetry.io/docs)-based Protocols
|
|
135
|
-
|
|
136
|
-
```python
|
|
137
|
-
class HelloService:
|
|
138
|
-
def __init__(
|
|
139
|
-
self,
|
|
140
|
-
hello_rpc: Annotated[HelloRPC, Token(HelloRPC, "HELLO_RPC")],
|
|
141
|
-
):
|
|
142
|
-
self.hello_rpc = hello_rpc
|
|
143
|
-
|
|
144
|
-
@TracedFunc("ping") # Decorator for tracing
|
|
145
|
-
async def ping(self) -> HelloResponse:
|
|
146
|
-
return await self.hello_rpc.ping()
|
|
147
|
-
|
|
148
|
-
@TracedFunc("hello-service")
|
|
149
|
-
async def hello(
|
|
150
|
-
self,
|
|
151
|
-
gather: bool,
|
|
152
|
-
) -> HelloResponse:
|
|
153
|
-
now = asyncio.get_event_loop().time()
|
|
154
|
-
if gather:
|
|
155
|
-
await asyncio.gather(*[self.random_await(a) for a in range(10)])
|
|
156
|
-
else:
|
|
157
|
-
for a in range(10):
|
|
158
|
-
await self.random_await(a)
|
|
159
|
-
return HelloResponse(
|
|
160
|
-
message="Elapsed time: {}".format(asyncio.get_event_loop().time() - now)
|
|
161
|
-
)
|
|
162
|
-
|
|
163
|
-
@TracedFunc("random-await")
|
|
164
|
-
async def random_await(self, index: int) -> None:
|
|
165
|
-
logger.info("Random await %s", index, extra={"index": index})
|
|
166
|
-
await asyncio.sleep(random.randint(1, 3))
|
|
167
|
-
logger.info("Random await %s done", index, extra={"index": index})
|
|
168
|
-
```
|
|
169
|
-
|
|
170
|
-
## Installation
|
|
171
|
-
|
|
172
|
-
```bash
|
|
173
|
-
pip install jararaca
|
|
174
|
-
```
|
|
175
|
-
|
|
176
|
-
## Usage
|
|
177
|
-
|
|
178
|
-
### Create a Microservice
|
|
179
|
-
|
|
180
|
-
```python
|
|
181
|
-
# app.py
|
|
182
|
-
|
|
183
|
-
from jararaca import Microservice, create_http_server, create_messagebus_worker
|
|
184
|
-
from jararaca.presentation.http_microservice import HttpMicroservice
|
|
185
|
-
|
|
186
|
-
app = Microservice(
|
|
187
|
-
providers=[
|
|
188
|
-
ProviderSpec(
|
|
189
|
-
provide=Token[AppConfig],
|
|
190
|
-
use_factory=AppConfig.provider,
|
|
191
|
-
)
|
|
192
|
-
],
|
|
193
|
-
controllers=[TasksController],
|
|
194
|
-
interceptors=[
|
|
195
|
-
AIOSqlAlchemySessionInterceptor(
|
|
196
|
-
AIOSQAConfig(
|
|
197
|
-
connection_name="default",
|
|
198
|
-
url="sqlite+aiosqlite:///db.sqlite3",
|
|
199
|
-
)
|
|
200
|
-
),
|
|
201
|
-
],
|
|
202
|
-
)
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
# App for specific Http Configuration Context
|
|
206
|
-
http_app = HttpMicroservice(app)
|
|
207
|
-
|
|
208
|
-
web_app = create_http_server(app)
|
|
209
|
-
|
|
210
|
-
```
|
|
211
|
-
|
|
212
|
-
### Run as a Web Server
|
|
213
|
-
|
|
214
|
-
```bash
|
|
215
|
-
uvicorn app:web_app --reload
|
|
216
|
-
# or
|
|
217
|
-
jararaca server app:app
|
|
218
|
-
# or
|
|
219
|
-
jararaca server app:http_app
|
|
220
|
-
|
|
221
|
-
```
|
|
222
|
-
|
|
223
|
-
### Run as a Message Bus Worker
|
|
224
|
-
|
|
225
|
-
```bash
|
|
226
|
-
jararaca worker app:app
|
|
227
|
-
```
|
|
228
|
-
|
|
229
|
-
### Run as a scheduled routine
|
|
230
|
-
|
|
231
|
-
```bash
|
|
232
|
-
jararaca scheduler app:app
|
|
233
|
-
```
|
|
234
|
-
|
|
235
|
-
### Generate Typescript intefaces from microservice app controllers
|
|
236
|
-
|
|
237
|
-
```bash
|
|
238
|
-
jararaca gen-tsi app.main:app app.ts
|
|
239
|
-
```
|
|
240
|
-
|
|
241
|
-
### Documentation
|
|
242
|
-
|
|
243
|
-
Documentation is under construction [here](https://luscasleo.github.io/jararaca/).
|