-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathredis_worker.py
More file actions
562 lines (488 loc) · 18.2 KB
/
redis_worker.py
File metadata and controls
562 lines (488 loc) · 18.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
"""
Redis Worker Example
=====================
This example demonstrates how to create and manage worker processes that
execute tasks from a Redis queue. Workers enable distributed execution by:
- Polling Redis queue for tasks
- Executing tasks independently
- Storing results back to Redis
- Handling errors gracefully
Prerequisites:
--------------
1. Redis running (auto-starts via Docker if available):
docker run -p 6379:6379 redis:7.2
2. pip install redis
Concepts Covered:
-----------------
1. Worker process creation
2. Worker lifecycle management
3. Task polling and execution
4. Graceful shutdown
5. Worker metrics and monitoring
6. Distributed workflow execution with engine.execute()
How to Run:
-----------
# Option 1: Run with embedded workers (self-contained)
python examples/05_distributed/redis_worker.py
# Option 2: Run with external workers
# Terminal 1: Start Redis
docker run -p 6379:6379 redis:7.2
# Terminal 2: Start worker using CLI (prefix must match demo)
python -m graflow.worker.main --worker-id worker-1 --redis-key-prefix graflow:worker_demo
# Terminal 3: Run this example
python examples/05_distributed/redis_worker.py
Expected Output:
----------------
=== Redis Worker Demo ===
Step 1: Setup
✅ Redis connected
Using Redis at localhost:6379
✅ 2 local worker threads started
Step 2: Executing workflow with Redis backend
⏳ Waiting for workflow completion...
Step 3: Results
✅ Task test_task_1: result_1
✅ Task test_task_2: result_2
✅ Task test_task_3: result_3
✅ Distributed workflow completed
=== Summary ===
✅ Worker lifecycle demonstrated
✅ Tasks executed by workers
✅ Results stored in Redis
✅ Graceful shutdown working
To run workers as separate processes:
python -m graflow.worker.main --worker-id worker-1 --redis-host localhost
"""
import atexit
import logging
import os
import signal
import socket
import sys
import time
import redis
from graflow.coordination.coordinator import CoordinationBackend
from graflow.core.context import ExecutionContext
from graflow.core.decorators import task
from graflow.core.workflow import workflow
from graflow.queue.distributed import DistributedTaskQueue
from graflow.worker.worker import TaskWorker
REDIS_IMAGE = "redis:7.2"
def _is_port_available(port: int) -> bool:
"""Check if a local TCP port is free to bind."""
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
sock.bind(("127.0.0.1", port))
except OSError:
return False
return True
def _get_container_host_port(container):
"""Return the host port mapped to container's 6379/tcp."""
try:
container.reload()
ports = container.attrs.get("NetworkSettings", {}).get("Ports", {})
mapping = ports.get("6379/tcp")
if mapping:
return int(mapping[0]["HostPort"])
except Exception as exc: # pragma: no cover - best effort inspection
print(f"⚠️ Could not determine Redis port: {exc}")
return None
def _register_container_cleanup(container):
"""Register cleanup for a Docker container."""
def _cleanup():
try:
container.stop()
except Exception as exc: # pragma: no cover - best effort cleanup
print(f"⚠️ Failed to stop Redis container: {exc}")
atexit.register(_cleanup)
def _stop_redis_container(container):
"""Stop the Docker Redis container if we started one."""
try:
container.stop()
except Exception as exc: # pragma: no cover - best effort cleanup
print(f"⚠️ Failed to stop Redis container: {exc}")
def _start_redis_container():
"""Start Redis via Docker using the redis:7.2 image on port 6379."""
try:
import docker # type: ignore
except ImportError:
print("⚠️ Docker SDK not installed; cannot auto-start Redis container.")
return None
try:
client = docker.from_env()
container = client.containers.run(REDIS_IMAGE, ports={"6379/tcp": 6379}, detach=True, remove=True)
_register_container_cleanup(container)
host_port = _get_container_host_port(container)
if host_port is None or host_port != 6379:
print(f"❌ Redis container did not bind to port 6379 (bound to {host_port}).")
_stop_redis_container(container)
return None
return container, host_port
except Exception as exc: # pragma: no cover - docker import/env errors
print(f"❌ Failed to start Redis via Docker: {exc}")
return None
def _connect_redis(host: str = "localhost", port: int = 6379, password: str | None = None) -> redis.Redis:
client = redis.Redis(host=host, port=port, password=password, decode_responses=True)
client.ping()
return client
def check_redis() -> redis.Redis | None:
"""Ensure Redis is available, starting a Docker container if needed."""
host = os.getenv("REDIS_HOST", "localhost")
port = int(os.getenv("REDIS_PORT", "6379"))
password = os.getenv("REDIS_PASSWORD")
try:
return _connect_redis(host=host, port=port, password=password)
except ImportError as exc:
print(f"❌ redis package not installed: {exc}")
return None
except Exception as exc:
print(f"⚠️ Redis not available locally: {exc}")
message = str(exc).upper()
if not _is_port_available(port):
print("⚠️ Port 6379 is already in use; using the existing Redis instance.")
if "NOAUTH" in message and not password:
print("⚠️ Existing Redis requires authentication. Set REDIS_PASSWORD and rerun.")
return None
try:
return _connect_redis(host=host, port=port, password=password)
except Exception as retry_exc:
print(f"❌ Failed to use existing Redis on {host}:{port}: {retry_exc}")
return None
if "NOAUTH" in message:
print("⚠️ Redis at localhost:6379 requires authentication; this example expects no auth.")
print("ℹ️ Set REDIS_PASSWORD for the example to use your existing Redis.") # noqa: RUF001
return None
print("⏳ Attempting to start Redis via Docker (no auth) on port 6379...")
container_info = _start_redis_container()
if not container_info:
print(f"\n⚠️ Start Redis manually: docker run -p 6379:6379 {REDIS_IMAGE}")
return None
container, host_port = container_info
last_error = None
for _ in range(3):
time.sleep(2) # Give the container time to become ready
try:
client = _connect_redis(port=host_port)
if host_port == 6379:
print("✅ Started Redis via Docker")
else:
print(f"✅ Started Redis via Docker on port {host_port}")
return client
except Exception as exc:
last_error = exc
print(f"❌ Redis still unavailable after starting container: {last_error}")
_stop_redis_container(container)
return None
def start_workers(redis_client: redis.Redis, num_workers: int = 2):
"""Start local worker threads so the example is self-contained."""
# In a real deployment, these would be separate processes/containers
redis_queue = DistributedTaskQueue(redis_client=redis_client, key_prefix="graflow:worker_demo")
redis_queue.cleanup()
workers = [TaskWorker(queue=redis_queue, worker_id=f"worker-{i}") for i in range(num_workers)]
def shutdown_workers():
print("\nStopping workers...")
for worker in workers:
worker.stop()
print("✅ Workers stopped")
atexit.register(shutdown_workers)
# Override TaskWorker's signal handlers to ensure we exit and trigger atexit
def handle_signal(signum, frame):
sys.exit(0)
signal.signal(signal.SIGINT, handle_signal)
signal.signal(signal.SIGTERM, handle_signal)
for worker in workers:
worker.start()
print(f"✅ {num_workers} local worker threads started")
return workers
def demonstrate_cli_worker():
"""Demonstrate CLI worker usage."""
print("\n" + "=" * 60)
print("CLI Worker Usage")
print("=" * 60)
print("\nTo start a worker as a separate process:")
print("\n python -m graflow.worker.main --worker-id worker-1 --redis-key-prefix graflow:worker_demo")
print("\nWith custom configuration:")
print("\n python -m graflow.worker.main \\")
print(" --worker-id worker-1 \\")
print(" --redis-host localhost \\")
print(" --redis-port 6379 \\")
print(" --redis-key-prefix graflow:worker_demo \\")
print(" --max-concurrent-tasks 4 \\")
print(" --poll-interval 0.1")
print("\nMultiple workers:")
print("\n # Terminal 1")
print(" python -m graflow.worker.main --worker-id worker-1 --redis-key-prefix graflow:worker_demo")
print("\n # Terminal 2")
print(" python -m graflow.worker.main --worker-id worker-2 --redis-key-prefix graflow:worker_demo")
print("\n # Terminal 3")
print(" python -m graflow.worker.main --worker-id worker-3 --redis-key-prefix graflow:worker_demo")
def main():
"""Run distributed worker demonstration."""
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
)
print("=== Redis Worker Demo ===\n")
# Step 1: Setup
print("Step 1: Setup")
redis_client = check_redis()
if not redis_client:
sys.exit(1)
print("✅ Redis connected")
redis_host = redis_client.connection_pool.connection_kwargs.get("host", "localhost")
redis_port = int(redis_client.connection_pool.connection_kwargs.get("port", 6379))
print(f"Using Redis at {redis_host}:{redis_port}")
start_workers(redis_client=redis_client, num_workers=2)
# Create workflow
with workflow("worker_demo") as ctx:
@task
def test_task_1():
"""Test task 1."""
time.sleep(0.5) # Simulate work
return "result_1"
@task
def test_task_2():
"""Test task 2."""
time.sleep(0.5)
return "result_2"
@task
def test_task_3():
"""Test task 3."""
time.sleep(0.5)
return "result_3"
# Define parallel execution with Redis backend
_ = (
(test_task_1 | test_task_2 | test_task_3)
.set_group_name("parallel_tasks")
.with_execution(backend=CoordinationBackend.REDIS)
)
# Step 2: Execute workflow
print("\nStep 2: Executing workflow with Redis backend")
# Create execution context with Redis
# Note: We use the same key_prefix as the workers to ensure they share the queue
exec_context = ExecutionContext.create(
ctx.graph,
start_node="parallel_tasks",
channel_backend="redis",
max_steps=100,
config={"redis_client": redis_client, "key_prefix": "graflow:worker_demo"},
)
exec_context.channel.clear()
try:
print("⏳ Waiting for workflow completion...")
# Execute workflow using the engine
# This will block until the workflow completes (or fails)
# The engine handles distributed coordination via RedisCoordinator
from graflow.core.engine import WorkflowEngine
engine = WorkflowEngine()
result = engine.execute(exec_context)
print("\nStep 3: Results")
if result:
# Results are stored in the execution context
for task_name in ["test_task_1", "test_task_2", "test_task_3"]:
task_result = exec_context.get_result(task_name)
if task_result:
print(f"✅ Task {task_name}: {task_result}")
else:
print(f"⚠️ Task {task_name}: no result")
print("✅ Distributed workflow completed")
else:
print("⚠️ Distributed workflow did not return a result")
except Exception as e:
print(f"\n❌ Workflow execution failed: {e}")
import traceback
traceback.print_exc()
# Show CLI usage
demonstrate_cli_worker()
# Summary
print("\n=== Summary ===")
print("✅ Worker lifecycle demonstrated")
print("✅ Tasks executed by workers")
print("✅ Results stored in Redis")
print("✅ Graceful shutdown working")
print("\nTo run workers as separate processes:")
print(" python -m graflow.worker.main --worker-id worker-1 --redis-host localhost")
if __name__ == "__main__":
main()
# ============================================================================
# Key Takeaways:
# ============================================================================
#
# 1. **Distributed Workflow Pattern**
# with workflow("name") as ctx:
# parallel_tasks = (task1 | task2 | task3).set_group_name("group").with_execution(backend=CoordinationBackend.REDIS)
#
# - Use workflow context manager for graph construction
# - Use | operator for parallel execution
# - with_execution(backend=CoordinationBackend.REDIS) enables distributed execution
# - Tasks are automatically distributed across workers
#
# 2. **Engine-Based Execution**
# engine = WorkflowEngine()
# result = engine.execute(exec_context)
#
# - Use engine.execute() for proper workflow execution
# - Blocks until workflow completes
# - Handles distributed coordination automatically
# - Results stored in exec_context
#
# 3. **Worker Creation**
# from graflow.worker.worker import TaskWorker
#
# worker = TaskWorker(
# queue=redis_queue,
# worker_id="worker-1",
# max_concurrent_tasks=4,
# poll_interval=0.1
# )
#
# - Requires a DistributedTaskQueue instance
# - worker_id should be unique
# - max_concurrent_tasks controls parallelism
# - poll_interval affects latency vs CPU usage
#
# 4. **Worker Lifecycle**
# worker.start() # Blocks until stopped
# worker.stop() # Graceful shutdown
# worker.is_running # Check if running
#
# - start() is blocking - run in thread for background
# - stop() waits for current tasks to finish
# - Use threading.Thread for programmatic workers
#
# 5. **CLI Worker (Recommended)**
# python -m graflow.worker.main --worker-id worker-1 --redis-key-prefix graflow:worker_demo
#
# - Simplest way to run workers
# - Handles signal handling automatically
# - Supports environment variables
# - Production-ready
#
# 6. **Execution Context with Redis**
# exec_context = ExecutionContext.create(
# graph,
# start_node="parallel_tasks",
# channel_backend="redis",
# config={"redis_client": redis_client, "key_prefix": "graflow:worker_demo"}
# )
#
# - channel_backend="redis" enables distributed state sharing
# - key_prefix must match worker configuration
# - Results accessible via exec_context.get_result(task_id)
#
# 7. **Worker Metrics**
# worker.tasks_processed # Total tasks
# worker.tasks_succeeded # Successful tasks
# worker.tasks_failed # Failed tasks
# worker.total_execution_time # Total time
#
# - Access metrics for monitoring
# - Track worker performance
# - Implement health checks
#
# 8. **Multiple Workers**
# # Start multiple workers in separate processes
# # They automatically share the Redis queue
# # Tasks are distributed among workers
#
# - Add workers to scale horizontally
# - Workers don't need to know about each other
# - Redis handles task distribution
#
# 9. **Graceful Shutdown**
# # Worker handles SIGTERM and SIGINT
# # Finishes current tasks before stopping
# # Timeout configurable
#
# - Important for production deployments
# - Prevents task loss
# - Clean resource cleanup
#
# 10. **Fault Tolerance**
# - If worker crashes, tasks remain in Redis
# - Other workers can pick up tasks
# - Implement retry logic in tasks
# - Monitor worker health
#
# ============================================================================
# Try Experimenting:
# ============================================================================
#
# 1. Start multiple workers:
# # Terminal 1
# python -m graflow.worker.main --worker-id worker-1
# # Terminal 2
# python -m graflow.worker.main --worker-id worker-2
#
# 2. Adjust concurrency:
# python -m graflow.worker.main --max-concurrent-tasks 8
#
# 3. Use environment variables:
# export WORKER_ID=my-worker
# export MAX_CONCURRENT_TASKS=4
# python -m graflow.worker.main
#
# 4. Monitor with different log levels:
# python -m graflow.worker.main --log-level DEBUG
#
# 5. Test graceful shutdown:
# # Start worker and send SIGINT (Ctrl+C)
# # Watch it finish current tasks
#
# ============================================================================
# Real-World Use Cases:
# ============================================================================
#
# **Multi-Server Deployment**:
# Deploy workers on multiple servers, all reading from same Redis
#
# **Auto-Scaling**:
# Monitor queue size, start/stop workers dynamically
#
# **Specialized Workers**:
# Some workers handle CPU tasks, others handle I/O tasks
#
# **Geographic Distribution**:
# Workers in different regions for low-latency processing
#
# **Resource-Specific Workers**:
# GPU workers for ML tasks, regular workers for other tasks
#
# ============================================================================
# Production Deployment:
# ============================================================================
#
# **Systemd Service**:
# [Unit]
# Description=Graflow Worker
#
# [Service]
# ExecStart=/usr/bin/python3 -m graflow.worker.main --worker-id worker-1
# Restart=always
#
# [Install]
# WantedBy=multi-user.target
#
# **Docker Container**:
# FROM python:3.11
# RUN pip install graflow redis
# CMD ["python", "-m", "graflow.worker.main", "--worker-id", "worker-1"]
#
# **Kubernetes Deployment**:
# apiVersion: apps/v1
# kind: Deployment
# metadata:
# name: graflow-workers
# spec:
# replicas: 3
# template:
# spec:
# containers:
# - name: worker
# image: graflow-worker:latest
# env:
# - name: REDIS_HOST
# value: redis-service
#
# ============================================================================