ioc-scope-worker-base 1.0.0__tar.gz
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.
- ioc_scope_worker_base-1.0.0/.gitignore +138 -0
- ioc_scope_worker_base-1.0.0/PKG-INFO +343 -0
- ioc_scope_worker_base-1.0.0/README.md +317 -0
- ioc_scope_worker_base-1.0.0/pyproject.toml +63 -0
- ioc_scope_worker_base-1.0.0/src/__init__.py +2 -0
- ioc_scope_worker_base-1.0.0/src/worker_base/__init__.py +2 -0
- ioc_scope_worker_base-1.0.0/src/worker_base/base_worker.py +415 -0
- ioc_scope_worker_base-1.0.0/src/worker_base/dal_adapter.py +191 -0
- ioc_scope_worker_base-1.0.0/src/worker_base/queue_message.py +167 -0
- ioc_scope_worker_base-1.0.0/src/worker_base/runner.py +169 -0
- ioc_scope_worker_base-1.0.0/src/worker_base/utils.py +275 -0
- ioc_scope_worker_base-1.0.0/tests/__init__.py +2 -0
- ioc_scope_worker_base-1.0.0/tests/conftest.py +11 -0
- ioc_scope_worker_base-1.0.0/tests/test_base_worker.py +458 -0
- ioc_scope_worker_base-1.0.0/tests/test_queue_message.py +247 -0
- ioc_scope_worker_base-1.0.0/tests/test_runner.py +72 -0
- ioc_scope_worker_base-1.0.0/tests/test_thread_pool.py +57 -0
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
pip-wheel-metadata/
|
|
24
|
+
share/python-wheels/
|
|
25
|
+
*.egg-info/
|
|
26
|
+
.installed.cfg
|
|
27
|
+
*.egg
|
|
28
|
+
MANIFEST
|
|
29
|
+
|
|
30
|
+
# PyInstaller
|
|
31
|
+
*.manifest
|
|
32
|
+
*.spec
|
|
33
|
+
|
|
34
|
+
# Installer logs
|
|
35
|
+
pip-log.txt
|
|
36
|
+
pip-delete-this-directory.txt
|
|
37
|
+
|
|
38
|
+
# Unit test / coverage reports
|
|
39
|
+
htmlcov/
|
|
40
|
+
.tox/
|
|
41
|
+
.nox/
|
|
42
|
+
.coverage
|
|
43
|
+
.coverage.*
|
|
44
|
+
.cache
|
|
45
|
+
nosetests.xml
|
|
46
|
+
coverage.xml
|
|
47
|
+
*.cover
|
|
48
|
+
*.py,cover
|
|
49
|
+
.hypothesis/
|
|
50
|
+
.pytest_cache/
|
|
51
|
+
|
|
52
|
+
# Translations
|
|
53
|
+
*.mo
|
|
54
|
+
*.pot
|
|
55
|
+
|
|
56
|
+
# Django stuff:
|
|
57
|
+
*.log
|
|
58
|
+
local_settings.py
|
|
59
|
+
db.sqlite3
|
|
60
|
+
db.sqlite3-journal
|
|
61
|
+
|
|
62
|
+
# Flask stuff:
|
|
63
|
+
instance/
|
|
64
|
+
.webassets-cache
|
|
65
|
+
|
|
66
|
+
# Scrapy stuff:
|
|
67
|
+
.scrapy
|
|
68
|
+
|
|
69
|
+
# Sphinx documentation
|
|
70
|
+
docs/_build/
|
|
71
|
+
|
|
72
|
+
# PyBuilder
|
|
73
|
+
target/
|
|
74
|
+
|
|
75
|
+
# Jupyter Notebook
|
|
76
|
+
.ipynb_checkpoints
|
|
77
|
+
|
|
78
|
+
# IPython
|
|
79
|
+
profile_default/
|
|
80
|
+
ipython_config.py
|
|
81
|
+
|
|
82
|
+
# pyenv
|
|
83
|
+
.python-version
|
|
84
|
+
|
|
85
|
+
# pipenv
|
|
86
|
+
Pipfile.lock
|
|
87
|
+
|
|
88
|
+
# PEP 582
|
|
89
|
+
__pypackages__/
|
|
90
|
+
|
|
91
|
+
# Celery stuff
|
|
92
|
+
celerybeat-schedule
|
|
93
|
+
celerybeat.pid
|
|
94
|
+
|
|
95
|
+
# SageMath parsed files
|
|
96
|
+
*.sage.py
|
|
97
|
+
|
|
98
|
+
# Environments
|
|
99
|
+
.env
|
|
100
|
+
.venv
|
|
101
|
+
env/
|
|
102
|
+
venv/
|
|
103
|
+
ENV/
|
|
104
|
+
env.bak/
|
|
105
|
+
venv.bak/
|
|
106
|
+
|
|
107
|
+
# Spyder project settings
|
|
108
|
+
.spyderproject
|
|
109
|
+
.spyproject
|
|
110
|
+
|
|
111
|
+
# Rope project settings
|
|
112
|
+
.ropeproject
|
|
113
|
+
|
|
114
|
+
# mkdocs documentation
|
|
115
|
+
/site
|
|
116
|
+
|
|
117
|
+
# mypy
|
|
118
|
+
.mypy_cache/
|
|
119
|
+
.dmypy.json
|
|
120
|
+
dmypy.json
|
|
121
|
+
|
|
122
|
+
# Pyre type checker
|
|
123
|
+
.pyre/
|
|
124
|
+
|
|
125
|
+
# IDE
|
|
126
|
+
.vscode/
|
|
127
|
+
.idea/
|
|
128
|
+
*.swp
|
|
129
|
+
*.swo
|
|
130
|
+
*~
|
|
131
|
+
.DS_Store
|
|
132
|
+
|
|
133
|
+
# Project specific
|
|
134
|
+
/tmp/
|
|
135
|
+
*.json.tmp
|
|
136
|
+
output.txt
|
|
137
|
+
.deepeval/
|
|
138
|
+
.ruff_cache/
|
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: ioc-scope-worker-base
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Base worker framework for RabbitMQ-based message processing
|
|
5
|
+
Project-URL: Homepage, https://github.com/agntcy/telemetry-hub
|
|
6
|
+
Project-URL: Repository, https://github.com/agntcy/telemetry-hub
|
|
7
|
+
Author-email: Outshift Open <oss-github@cisco.com>
|
|
8
|
+
License-Expression: Apache-2.0
|
|
9
|
+
Classifier: Development Status :: 3 - Alpha
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Requires-Python: >=3.11
|
|
15
|
+
Requires-Dist: aio-pika>=8.0.0
|
|
16
|
+
Requires-Dist: ioc-scope-api
|
|
17
|
+
Requires-Dist: pika>=1.3.0
|
|
18
|
+
Requires-Dist: pydantic>=2.0.0
|
|
19
|
+
Requires-Dist: ruff<=0.15.22
|
|
20
|
+
Provides-Extra: dev
|
|
21
|
+
Requires-Dist: pre-commit>=4.4.0; extra == 'dev'
|
|
22
|
+
Requires-Dist: pytest-asyncio>=0.21.0; extra == 'dev'
|
|
23
|
+
Requires-Dist: pytest-mock>=3.10.0; extra == 'dev'
|
|
24
|
+
Requires-Dist: pytest>=7.0.0; extra == 'dev'
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
|
|
27
|
+
# Base Worker Framework
|
|
28
|
+
|
|
29
|
+
A reusable RabbitMQ-based worker framework for building message processing workers. Provides a foundation for implementing workers that process messages from queues, with support for multiple concurrent messages, feedback queues, and Argo Workflows integration.
|
|
30
|
+
|
|
31
|
+
## Overview
|
|
32
|
+
|
|
33
|
+
The Base Worker framework provides:
|
|
34
|
+
- **RabbitMQ Integration**: Async message queue handling with `aio_pika`
|
|
35
|
+
- **Message Models**: Pydantic-based message types for queue communication
|
|
36
|
+
- **Error Handling**: Comprehensive error handling and feedback mechanisms
|
|
37
|
+
- **Argo Support**: Process single messages without RabbitMQ (Argo Workflows mode)
|
|
38
|
+
- **Concurrent Processing**: Handle multiple messages in parallel with configurable limits
|
|
39
|
+
|
|
40
|
+
## Architecture
|
|
41
|
+
|
|
42
|
+
```
|
|
43
|
+
BaseWorker (abstract)
|
|
44
|
+
├── RabbitMQ message handling
|
|
45
|
+
├── Message parsing and validation
|
|
46
|
+
├── Feedback queue support
|
|
47
|
+
├── Output message propagation
|
|
48
|
+
└── Argo Workflows mode
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Installation
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
pip install ioc-scope-worker-base
|
|
55
|
+
# or for development
|
|
56
|
+
pip install -e .
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Usage
|
|
60
|
+
|
|
61
|
+
### Creating a Custom Worker
|
|
62
|
+
|
|
63
|
+
```python
|
|
64
|
+
from worker_base.base_worker import BaseWorker
|
|
65
|
+
from worker_base.queue_message import BaseQueueMessage
|
|
66
|
+
|
|
67
|
+
class MyCustomWorker(BaseWorker):
|
|
68
|
+
def __init__(self, rabbitmq_url: str, input_queue: str, **kwargs):
|
|
69
|
+
super().__init__(
|
|
70
|
+
rabbitmq_url=rabbitmq_url,
|
|
71
|
+
input_queue=input_queue,
|
|
72
|
+
**kwargs
|
|
73
|
+
)
|
|
74
|
+
self.name = "MyCustomWorker"
|
|
75
|
+
self.input_message_class = BaseQueueMessage
|
|
76
|
+
|
|
77
|
+
async def handle_message(self, msg: BaseQueueMessage):
|
|
78
|
+
# Implement your processing logic here
|
|
79
|
+
session_id = msg.session_id
|
|
80
|
+
|
|
81
|
+
# Process the message
|
|
82
|
+
result = process_session(session_id)
|
|
83
|
+
|
|
84
|
+
# Return True to propagate to output queues
|
|
85
|
+
# Return None for errors
|
|
86
|
+
# Return False for success without propagation
|
|
87
|
+
return True if result else None
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### Running the Worker
|
|
91
|
+
|
|
92
|
+
```python
|
|
93
|
+
import asyncio
|
|
94
|
+
|
|
95
|
+
async def main():
|
|
96
|
+
worker = MyCustomWorker(
|
|
97
|
+
rabbitmq_url="amqp://<user>:<password>@rabbitmq:5672/",
|
|
98
|
+
input_queue="my_input_queue",
|
|
99
|
+
output_queue=["output_queue_1", "output_queue_2"],
|
|
100
|
+
feedback_queue="feedback_queue",
|
|
101
|
+
message_limit=-1, # -1 for unlimited
|
|
102
|
+
max_inflight_messages=1,
|
|
103
|
+
)
|
|
104
|
+
await worker.run()
|
|
105
|
+
|
|
106
|
+
if __name__ == "__main__":
|
|
107
|
+
asyncio.run(main())
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## Components
|
|
111
|
+
|
|
112
|
+
### BaseWorker
|
|
113
|
+
|
|
114
|
+
The abstract base class for all workers. Key methods:
|
|
115
|
+
|
|
116
|
+
- `async run()`: Start the worker (reads from RabbitMQ)
|
|
117
|
+
- `async run_argo_message(input_path, output_path)`: Process single Argo message
|
|
118
|
+
- `async handle_message(msg)`: Override to implement processing logic
|
|
119
|
+
- `async propagate_message()`: Send output to output queues
|
|
120
|
+
|
|
121
|
+
Key attributes:
|
|
122
|
+
|
|
123
|
+
- `self.name`: Worker name (for logging)
|
|
124
|
+
- `self.input_message_class`: Message type to deserialize
|
|
125
|
+
- `self.output_messages`: List to populate with output messages
|
|
126
|
+
|
|
127
|
+
### Message Types
|
|
128
|
+
|
|
129
|
+
#### BaseMessage
|
|
130
|
+
|
|
131
|
+
Base message class with:
|
|
132
|
+
- `job_id`: Optional job identifier
|
|
133
|
+
- `workflow_id`: Optional workflow identifier
|
|
134
|
+
- `local_file`: Optional file reference
|
|
135
|
+
|
|
136
|
+
#### BaseQueueMessage
|
|
137
|
+
|
|
138
|
+
Queue-based message extending BaseMessage:
|
|
139
|
+
- `session_id`: Required session identifier
|
|
140
|
+
|
|
141
|
+
#### BaseTriggerMessage
|
|
142
|
+
|
|
143
|
+
Trigger-based message:
|
|
144
|
+
- `application_id`: Required application identifier
|
|
145
|
+
|
|
146
|
+
#### FeedbackMessage
|
|
147
|
+
|
|
148
|
+
Status feedback messages (not a Pydantic model):
|
|
149
|
+
- `session_id`: Session identifier
|
|
150
|
+
- `event`: Event type (start, complete, error)
|
|
151
|
+
- `workflow`: Worker name
|
|
152
|
+
- `workflow_id`: Workflow identifier
|
|
153
|
+
- `queue_name`: Queue name
|
|
154
|
+
|
|
155
|
+
### Configuration
|
|
156
|
+
|
|
157
|
+
#### Constructor Parameters
|
|
158
|
+
|
|
159
|
+
```python
|
|
160
|
+
BaseWorker(
|
|
161
|
+
rabbitmq_url: str, # RabbitMQ connection URL
|
|
162
|
+
input_queue: str, # Input queue name
|
|
163
|
+
output_queue: list[str] = [], # Output queue names
|
|
164
|
+
feedback_queue: str | None = None, # Feedback queue (optional)
|
|
165
|
+
message_limit: int = -1, # Max messages to process (-1=unlimited)
|
|
166
|
+
max_inflight_messages: int = 1, # Concurrent messages to process
|
|
167
|
+
db_type: str = "neo4j", # DB type for optional KG_DAL
|
|
168
|
+
db_uri: str | None = None, # Database URI
|
|
169
|
+
db_user: str | None = None, # Database user
|
|
170
|
+
db_password: str | None = None, # Database password
|
|
171
|
+
db_database: str | None = None, # Database name
|
|
172
|
+
)
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
`rabbitmq_url` must resolve to a real connection string. At the application layer,
|
|
176
|
+
workers can now be configured with either:
|
|
177
|
+
- `RABBITMQ_URL`
|
|
178
|
+
- or split env vars: `RABBITMQ_HOST`/`RABBITMQ_PORT` plus required `RABBITMQ_USER` and `RABBITMQ_PASSWORD`, and optional `RABBITMQ_VHOST`
|
|
179
|
+
|
|
180
|
+
## Message Flow
|
|
181
|
+
|
|
182
|
+
```
|
|
183
|
+
RabbitMQ Queue
|
|
184
|
+
↓
|
|
185
|
+
Parse Message
|
|
186
|
+
↓
|
|
187
|
+
Feedback: start event
|
|
188
|
+
↓
|
|
189
|
+
handle_message() ← IMPLEMENT THIS
|
|
190
|
+
↓
|
|
191
|
+
├─ Returns True
|
|
192
|
+
│ ↓
|
|
193
|
+
│ propagate_message()
|
|
194
|
+
│ ↓
|
|
195
|
+
│ Output Queue(s)
|
|
196
|
+
│
|
|
197
|
+
├─ Returns False
|
|
198
|
+
│ ↓
|
|
199
|
+
│ No output
|
|
200
|
+
│
|
|
201
|
+
└─ Returns None (error)
|
|
202
|
+
↓
|
|
203
|
+
Feedback: error event
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
## Error Handling
|
|
207
|
+
|
|
208
|
+
- Message parsing errors: Logged and skipped
|
|
209
|
+
- Processing errors: Caught, logged, feedback sent
|
|
210
|
+
- Worker lifecycle errors: Logged and raised
|
|
211
|
+
|
|
212
|
+
Feedback messages are sent for:
|
|
213
|
+
- `start`: When message processing begins
|
|
214
|
+
- `complete`: When processing completes successfully
|
|
215
|
+
- `error`: When an error occurs
|
|
216
|
+
|
|
217
|
+
## Concurrent Message Processing
|
|
218
|
+
|
|
219
|
+
Control concurrency with:
|
|
220
|
+
|
|
221
|
+
```python
|
|
222
|
+
# Process one message at a time
|
|
223
|
+
max_inflight_messages=1
|
|
224
|
+
|
|
225
|
+
# Process up to 5 messages in parallel
|
|
226
|
+
max_inflight_messages=5
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
RabbitMQ prefetch count is automatically set based on `max_inflight_messages`.
|
|
230
|
+
|
|
231
|
+
## Argo Workflows Integration
|
|
232
|
+
|
|
233
|
+
Process a single message file without RabbitMQ:
|
|
234
|
+
|
|
235
|
+
```python
|
|
236
|
+
worker = MyWorker(
|
|
237
|
+
rabbitmq_url="dummy", # Not used in Argo mode
|
|
238
|
+
input_queue="argo",
|
|
239
|
+
)
|
|
240
|
+
|
|
241
|
+
# Process single message
|
|
242
|
+
await worker.run_argo_message(
|
|
243
|
+
raw_input="input.json",
|
|
244
|
+
output_path="output.json"
|
|
245
|
+
)
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
Input file (JSON):
|
|
249
|
+
```json
|
|
250
|
+
{
|
|
251
|
+
"session_id": "session_123",
|
|
252
|
+
"job_id": "job_456",
|
|
253
|
+
"workflow_id": "wf_789"
|
|
254
|
+
}
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
Output file (JSON):
|
|
258
|
+
```json
|
|
259
|
+
{
|
|
260
|
+
"session_id": "session_123",
|
|
261
|
+
"workflow_id": "wf_789"
|
|
262
|
+
}
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
## Advanced Features
|
|
266
|
+
|
|
267
|
+
### Custom Message Types
|
|
268
|
+
|
|
269
|
+
Extend `BaseQueueMessage` with additional fields:
|
|
270
|
+
|
|
271
|
+
```python
|
|
272
|
+
from worker_base.queue_message import BaseQueueMessage
|
|
273
|
+
from pydantic import Field
|
|
274
|
+
|
|
275
|
+
class CustomMessage(BaseQueueMessage):
|
|
276
|
+
priority: int = Field(alias="priority", default=0)
|
|
277
|
+
tags: list[str] = Field(alias="tags", default=[])
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
### Database Integration
|
|
281
|
+
|
|
282
|
+
Optional knowledge graph database integration:
|
|
283
|
+
|
|
284
|
+
```python
|
|
285
|
+
worker = MyWorker(
|
|
286
|
+
rabbitmq_url=...,
|
|
287
|
+
input_queue=...,
|
|
288
|
+
db_uri="neo4j://localhost:7687",
|
|
289
|
+
db_user="neo4j",
|
|
290
|
+
db_password="password",
|
|
291
|
+
db_database="neo4j",
|
|
292
|
+
)
|
|
293
|
+
|
|
294
|
+
# Access with worker.db_handler
|
|
295
|
+
graph = worker.db_handler
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
### Feedback Queues
|
|
299
|
+
|
|
300
|
+
Enable processing feedback:
|
|
301
|
+
|
|
302
|
+
```python
|
|
303
|
+
worker = MyWorker(
|
|
304
|
+
...,
|
|
305
|
+
feedback_queue="feedback_queue"
|
|
306
|
+
)
|
|
307
|
+
|
|
308
|
+
# Worker automatically sends feedback messages
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
## Examples
|
|
312
|
+
|
|
313
|
+
See the `norm-worker` project for a complete example of a worker built on `worker-base`.
|
|
314
|
+
|
|
315
|
+
## Dependencies
|
|
316
|
+
|
|
317
|
+
- **pydantic**: Data validation
|
|
318
|
+
- **aio_pika**: Async RabbitMQ client
|
|
319
|
+
- **pika**: RabbitMQ library
|
|
320
|
+
- **dal**: Data Access Layer (optional KG integration)
|
|
321
|
+
|
|
322
|
+
## Testing
|
|
323
|
+
|
|
324
|
+
```bash
|
|
325
|
+
pytest tests/
|
|
326
|
+
pytest tests/ -v
|
|
327
|
+
pytest tests/ --cov
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
## Contributing
|
|
331
|
+
|
|
332
|
+
When creating custom workers:
|
|
333
|
+
1. Extend `BaseWorker`
|
|
334
|
+
2. Override `handle_message()`
|
|
335
|
+
3. Set `self.name` and `self.input_message_class`
|
|
336
|
+
4. Return appropriate values (True/False/None)
|
|
337
|
+
5. Populate `self.output_messages` if needed
|
|
338
|
+
|
|
339
|
+
## License
|
|
340
|
+
|
|
341
|
+
Apache-2.0
|
|
342
|
+
|
|
343
|
+
See LICENSE file and copyright headers in source files.
|