-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.py
More file actions
111 lines (89 loc) · 3.11 KB
/
Copy pathmain.py
File metadata and controls
111 lines (89 loc) · 3.11 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
import argparse
import asyncio
import logging
import threading
import uvicorn
from fastapi import FastAPI
from app.api.bulk import router as bulk_router
from app.api.routes import router as submissions_router
from app.kafka.consumer import IngestionConsumer
from app.logging_config import configure_logging
from app.temporal.worker import start_worker
logger = logging.getLogger("analytics_service.main")
consumer_running = False
worker_running = False
def run_web():
"""Start the FastAPI web server."""
app = FastAPI(
title="Analytics Service API Ingestion & Orchestration Layer",
description="FastAPI ingestion endpoints and manual orchestration controls.",
version="1.0.0",
)
app.include_router(submissions_router)
app.include_router(bulk_router)
@app.get("/health")
def health_check():
return {
"status": "healthy",
"consumer_running": consumer_running,
"worker_running": worker_running,
}
logger.info("Starting FastAPI web server...")
uvicorn.run(app, host="0.0.0.0", port=8000, log_config=None)
async def run_consumer():
"""Start the Kafka consumer loop."""
consumer = IngestionConsumer()
try:
await consumer.start()
except KeyboardInterrupt:
logger.info("Interrupt received, stopping consumer...")
consumer.stop()
async def run_worker():
"""Start the Temporal worker."""
await start_worker()
def main():
parser = argparse.ArgumentParser(description="Analytics Ingestion and Orchestration runner.")
parser.add_argument(
"--mode",
choices=["consumer", "worker", "web", "all"],
default="all",
help="Specify the service mode to start: 'consumer' (Kafka consumer), 'worker' (Temporal worker), 'web' (API server), or 'all' (run all three services).",
)
args = parser.parse_args()
configure_logging(args.mode)
global consumer_running, worker_running
if args.mode == "web":
run_web()
elif args.mode == "consumer":
consumer_running = True
try:
asyncio.run(run_consumer())
except KeyboardInterrupt:
logger.info("Kafka consumer stopped.")
finally:
consumer_running = False
elif args.mode == "worker":
worker_running = True
try:
asyncio.run(run_worker())
except KeyboardInterrupt:
logger.info("Temporal worker stopped.")
finally:
worker_running = False
elif args.mode == "all":
web_thread = threading.Thread(target=run_web, daemon=True)
web_thread.start()
async def run_all_services():
global consumer_running, worker_running
consumer_running = True
worker_running = True
await asyncio.gather(run_consumer(), run_worker())
try:
asyncio.run(run_all_services())
except KeyboardInterrupt:
logger.info("Shutdown signal received, stopping all services.")
finally:
consumer_running = False
worker_running = False
if __name__ == "__main__":
main()