|
3 | 3 |
|
4 | 4 | from src.common.event_bus import EventBus |
5 | 5 | from src.common.utils import get_logger |
6 | | -from src.common.models import MatchResultEvent, DriverLocationEvent |
| 6 | +from src.common.models import DriverLocationEvent |
7 | 7 |
|
8 | | -from src.dispatch-service.producer import DispatchProducer |
9 | | -from src.dispatch-service.consumer import DispatchConsumer |
10 | | -from src.dispatch-service.api_router import router, DRIVER_STORE, MATCH_RESULTS_STORE |
| 8 | +from src.driver-location-service.location_store import DriverLocationStore |
| 9 | +from src.driver-location-service.consumer import DriverLocationConsumer |
| 10 | +from src.driver-location-service.api_router import router, DRIVER_STORE |
11 | 11 |
|
12 | | -# -------------------------------------------------------------------- |
13 | | -# CORE STATE STORES |
14 | | -# -------------------------------------------------------------------- |
15 | 12 |
|
16 | | -# In-memory driver store populated by Driver Location Service |
17 | | -_driver_locations: list[DriverLocationEvent] = [] |
| 13 | +# ------------------------------------------------------------ |
| 14 | +# INITIALIZATION |
| 15 | +# ------------------------------------------------------------ |
18 | 16 |
|
19 | | -def driver_store(): |
20 | | - return list(_driver_locations) |
| 17 | +logger = get_logger("DriverLocationMain") |
21 | 18 |
|
22 | | -# In-memory match results |
23 | | -_match_results: list[MatchResultEvent] = [] |
24 | | - |
25 | | -def match_results_store(): |
26 | | - return _match_results |
27 | | - |
28 | | - |
29 | | -# Surge lookup (injected later; stub for now) |
30 | | -def surge_lookup(zone_id: str) -> float: |
31 | | - # In production → query pricing service cache |
32 | | - return 1.0 |
33 | | - |
34 | | - |
35 | | -# -------------------------------------------------------------------- |
36 | | -# SERVICE INITIALIZATION |
37 | | -# -------------------------------------------------------------------- |
38 | | - |
39 | | -logger = get_logger("DispatchMain") |
40 | 19 | event_bus = EventBus() |
| 20 | +driver_store = DriverLocationStore() |
41 | 21 |
|
42 | 22 | app = FastAPI( |
43 | | - title="Dispatch Service", |
44 | | - description="Driver-rider matching microservice.", |
| 23 | + title="Driver Location Service", |
| 24 | + description="Real-time driver location ingestion service for the ride-sharing platform.", |
45 | 25 | version="1.0.0", |
46 | 26 | ) |
47 | 27 |
|
48 | 28 |
|
49 | | -# -------------------------------------------------------------------- |
| 29 | +# ------------------------------------------------------------ |
50 | 30 | # STARTUP SEQUENCE |
51 | | -# -------------------------------------------------------------------- |
| 31 | +# ------------------------------------------------------------ |
52 | 32 |
|
53 | 33 | @app.on_event("startup") |
54 | 34 | async def startup_event(): |
55 | 35 |
|
56 | | - logger.info("Starting Dispatch Service...") |
| 36 | + logger.info("Starting Driver Location Service...") |
57 | 37 |
|
58 | | - # Inject stores into API router |
59 | | - global DRIVER_STORE, MATCH_RESULTS_STORE |
| 38 | + # Inject store into API router |
| 39 | + global DRIVER_STORE |
60 | 40 | DRIVER_STORE = driver_store |
61 | | - MATCH_RESULTS_STORE = _match_results |
62 | 41 |
|
63 | | - # Initialize producer and consumer |
64 | | - producer = DispatchProducer(event_bus) |
65 | | - consumer = DispatchConsumer( |
| 42 | + # Initialize consumer |
| 43 | + consumer = DriverLocationConsumer( |
66 | 44 | event_bus=event_bus, |
67 | | - driver_store=driver_store, |
68 | | - surge_lookup=surge_lookup, |
| 45 | + store=driver_store |
69 | 46 | ) |
70 | 47 |
|
71 | | - # Subscribe to trip requests |
72 | | - await event_bus.subscribe("trip_requests", consumer.handle_trip_request) |
73 | | - |
74 | | - # Subscribe to match results |
75 | | - async def match_result_listener(data): |
76 | | - event = MatchResultEvent(**data) |
77 | | - _match_results.append(event) |
78 | | - logger.info(f"[DISPATCH] Stored match result for rider {event.rider_id}") |
79 | | - |
80 | | - await event_bus.subscribe("match_results", match_result_listener) |
81 | | - |
82 | | - # Start background async tasks |
83 | | - asyncio.create_task(producer.start()) |
| 48 | + # Subscribe to driver location update events |
| 49 | + await event_bus.subscribe( |
| 50 | + "driver_location_updates", |
| 51 | + consumer.handle_driver_location |
| 52 | + ) |
84 | 53 |
|
85 | | - logger.info("Dispatch Service started successfully.") |
| 54 | + logger.info("Driver Location Service started successfully.") |
86 | 55 |
|
87 | 56 |
|
88 | | -# -------------------------------------------------------------------- |
| 57 | +# ------------------------------------------------------------ |
89 | 58 | # ROUTER |
90 | | -# -------------------------------------------------------------------- |
| 59 | +# ------------------------------------------------------------ |
91 | 60 |
|
92 | | -app.include_router(router, prefix="/dispatch") |
| 61 | +app.include_router(router, prefix="/driver-location") |
93 | 62 |
|
94 | 63 |
|
95 | | -# -------------------------------------------------------------------- |
96 | | -# LOCAL RUN |
97 | | -# -------------------------------------------------------------------- |
| 64 | +# ------------------------------------------------------------ |
| 65 | +# LOCAL RUNNER |
| 66 | +# ------------------------------------------------------------ |
98 | 67 |
|
99 | 68 | if __name__ == "__main__": |
100 | 69 | import uvicorn |
| 70 | + |
101 | 71 | uvicorn.run( |
102 | | - "src.dispatch-service.main:app", |
| 72 | + "src.driver-location-service.main:app", |
103 | 73 | host="0.0.0.0", |
104 | | - port=8002, |
| 74 | + port=8003, |
105 | 75 | reload=True |
106 | 76 | ) |
0 commit comments