How rangonroyutsab/flight-promotion-service works
Purpose and Entry Points
Flight Promotion Service is a Django microservice that selects eligible US flights, generates Gemini marketing copy, persists promotion payloads in MinIO, and serves them over REST. Runtime entry points are manage.py, the HTTP ASGI/WSGI project configuration in config/asgi.py and config/wsgi.py, and management commands in promotions/management/commands/generate_flight_promotions.py and promotions/management/commands/run_scheduler.py.
Django Application and HTTP Boundary
config/urls.py mounts the promotions API; promotions/api/urls.py defines health, promotion-list/date, and promotion-detail routes. promotions/api/views.py is the request boundary: it delegates reads to application services rather than exposing storage details. Response and exception shaping are centralized in promotions/api/responses.py and promotions/api/exceptions.py.
Generation Pipeline
promotions/services/generation_pipeline.py orchestrates a daily run: check idempotency, query candidate flights, generate copy, store payloads, create audit records, and publish a manifest. The pipeline is invoked manually by generate_flight_promotions.py or on a midnight schedule by run_scheduler.py. A prior daily manifest terminates the run; deterministic UUIDs based on date and flight identity make reruns stable.
External Source and AI Adapters
Flight eligibility is queried through promotions/clients/elasticsearch_client.py against Elasticsearch’s sample flight index. Promotion prompts are assembled in promotions/services/prompt_builder.py, then sent through the Gemini REST adapter at promotions/clients/ai/gemini.py. Pydantic contracts, especially promotions/schemas/ai_response.py, validate structured AI responses before they enter durable storage.
Storage, Manifest, and Audit Boundary
Generated promotion JSON is the source of served content and is stored through promotions/clients/minio_client.py and promotions/services/promotion_storage.py. promotions/services/manifest_service.py writes/reads a per-run manifest that acts as the daily publication pointer and idempotency marker. Schema contracts for these objects reside in promotions/schemas/promotion_object.py, promotions/schemas/run_manifest.py, and promotions/schemas/latest_pointer.py. PostgreSQL holds Django model audit/pointer data in promotions/models.py, rather than full promotion content.
Read Path
List/detail requests resolve the relevant manifest and promotion object keys, then retrieve JSON from MinIO. promotions/services/promotion_reader.py coordinates this path; promotions/services/concurrent_object_reader.py parallelizes list-object reads with a bounded thread pool, limiting MinIO latency exposure. The API returns validated promotion representations rather than querying PostgreSQL for content.
Runtime Infrastructure
Docker Compose provisions the Django web process, dedicated scheduler process, PostgreSQL 16, Elasticsearch 8.15, and S3-compatible MinIO; deployment topology is defined in docker-compose.yml. Initialization scripts create the MinIO bucket and load Kibana/Elasticsearch sample data: docker/minio-init/create-bucket.sh and docker/kibana-init/install-sample-data.sh. Django settings in config/settings.py supply environment-driven service endpoints, credentials, retry limits, timezone, and AI-provider selection.
Components 20
Runtime
- Django entry points · HTTP runs via ASGI/WSGI; commands support manual and scheduled runs.
manage.py - Generation command · Manually starts a daily promotion run.
promotions/management/commands/generate_flight_promotions.py - Scheduler command · Triggers generation on the midnight schedule.
promotions/management/commands/run_scheduler.py - Docker Compose topology · Provisions web, scheduler, PostgreSQL, Elasticsearch, and MinIO.
docker-compose.yml
Promotions application
- API routes and views · Routes health, list/date, and detail requests to application reads.
promotions/api/views.py - Responses and exceptions
promotions/api/responses.py - Generation pipeline · Checks idempotency, selects flights, generates and publishes promotions.
promotions/services/generation_pipeline.py - Prompt builder
promotions/services/prompt_builder.py - AI response contract · Validates structured generated copy before storage.
promotions/schemas/ai_response.py - Promotion storage · Writes generated promotion JSON objects.
promotions/services/promotion_storage.py - Manifest service · Maintains daily manifests as publication pointers and idempotency markers.
promotions/services/manifest_service.py - Promotion reader · Resolves manifests and promotion keys for API reads.
promotions/services/promotion_reader.py - Concurrent object reader · Limits MinIO latency exposure during list reads.
promotions/services/concurrent_object_reader.py
External adapters
- Flight eligibility client
promotions/clients/elasticsearch_client.py - Gemini client
promotions/clients/ai/gemini.py - MinIO client
promotions/clients/minio_client.py
Infrastructure
- PostgreSQL audit data · Stores audit and pointer data, not promotion content.
promotions/models.py - Elasticsearch flights · Hosts the sample flight source index.
- Gemini API
- MinIO promotion objects · Durable source of served promotion JSON and manifests.
Connections 22
- Django entry points → API routes and views: serves HTTP
- API routes and views → Responses and exceptions: shapes responses
- API routes and views → Promotion reader: delegates reads
- Generation command → Generation pipeline: starts
- Scheduler command → Generation pipeline: triggers nightly
- Generation pipeline → Manifest service: checks and publishes
- Generation pipeline → Flight eligibility client: queries candidates
- Flight eligibility client → Elasticsearch flights: searches
- Generation pipeline → Prompt builder: builds prompts
- Prompt builder → Gemini client: supplies prompts
- Gemini client → Gemini API: calls
- Gemini client → AI response contract: validates output
- Generation pipeline → Promotion storage: persists validated copy
- Promotion storage → MinIO client: uses
- Manifest service → MinIO client: reads and writes manifests
- MinIO client → MinIO promotion objects: stores objects
- Generation pipeline → PostgreSQL audit data: creates audit records
- Promotion reader → Manifest service: resolves publication
- Promotion reader → Concurrent object reader: loads list objects
- Concurrent object reader → MinIO client: reads in parallel
- Docker Compose topology → Django entry points: runs web process
- Docker Compose topology → Scheduler command: runs scheduler process