|
| 1 | +.PHONY: help build up down restart logs logs-gateway logs-processor test seed seed-item health metrics metrics-gateway metrics-processor inventory order-status clean rebuild ps test-order test-idempotency |
| 2 | + |
| 3 | +# Default target |
| 4 | +help: |
| 5 | + @echo "Flash Sale Engine - Makefile Commands" |
| 6 | + @echo "" |
| 7 | + @echo "Available commands:" |
| 8 | + @echo " make build - Build Docker images" |
| 9 | + @echo " make up - Start all services" |
| 10 | + @echo " make down - Stop all services" |
| 11 | + @echo " make restart - Restart all services" |
| 12 | + @echo " make logs - View logs from all services" |
| 13 | + @echo " make logs-gateway - View gateway logs" |
| 14 | + @echo " make logs-processor - View processor logs" |
| 15 | + @echo " make test - Run comprehensive test suite" |
| 16 | + @echo " make seed - Seed inventory (100 items for item_id '101')" |
| 17 | + @echo " make seed-item - Seed inventory for specific item (usage: make seed-item ITEM=102 QTY=50)" |
| 18 | + @echo " make health - Check service health" |
| 19 | + @echo " make metrics - View Prometheus metrics" |
| 20 | + @echo " make metrics-gateway - View gateway metrics" |
| 21 | + @echo " make metrics-processor - View processor metrics" |
| 22 | + @echo " make inventory - Check inventory for item (usage: make inventory ITEM=101)" |
| 23 | + @echo " make order-status - Check order status (usage: make order-status REQ_ID=test-123)" |
| 24 | + @echo " make clean - Stop services and remove containers" |
| 25 | + @echo " make rebuild - Rebuild and restart services" |
| 26 | + @echo " make ps - Show running containers" |
| 27 | + @echo "" |
| 28 | + |
| 29 | +# Build Docker images |
| 30 | +build: |
| 31 | + @echo "Building Docker images..." |
| 32 | + docker-compose build |
| 33 | + |
| 34 | +# Start all services |
| 35 | +up: |
| 36 | + @echo "Starting services..." |
| 37 | + docker-compose up -d |
| 38 | + @echo "Waiting for services to be ready..." |
| 39 | + @sleep 5 |
| 40 | + @echo "Services started. Use 'make health' to check status." |
| 41 | + |
| 42 | +# Stop all services |
| 43 | +down: |
| 44 | + @echo "Stopping services..." |
| 45 | + docker-compose down |
| 46 | + |
| 47 | +# Restart all services |
| 48 | +restart: |
| 49 | + @echo "Restarting services..." |
| 50 | + docker-compose restart |
| 51 | + |
| 52 | +# View logs from all services |
| 53 | +logs: |
| 54 | + docker-compose logs -f |
| 55 | + |
| 56 | +# View gateway logs |
| 57 | +logs-gateway: |
| 58 | + docker-compose logs -f gateway |
| 59 | + |
| 60 | +# View processor logs |
| 61 | +logs-processor: |
| 62 | + docker-compose logs -f processor |
| 63 | + |
| 64 | +# Run comprehensive test suite (requires PowerShell on Windows, or bash script on Linux) |
| 65 | +test: |
| 66 | + @echo "Running test suite..." |
| 67 | + @if command -v pwsh >/dev/null 2>&1; then \ |
| 68 | + pwsh -File test-all-features.ps1; \ |
| 69 | + elif [ -f test-all-features.sh ]; then \ |
| 70 | + ./test-all-features.sh; \ |
| 71 | + else \ |
| 72 | + echo "Error: test-all-features.ps1 requires PowerShell or test-all-features.sh"; \ |
| 73 | + echo "For manual testing, use: make seed && curl -X POST http://localhost:8080/buy ..."; \ |
| 74 | + fi |
| 75 | + |
| 76 | +# Seed inventory (default: 100 items for item_id '101') |
| 77 | +seed: |
| 78 | + @echo "Seeding inventory: 100 items for item_id '101'..." |
| 79 | + @docker exec flash-sale-engine-redis-1 redis-cli SET inventory:101 100 || \ |
| 80 | + docker exec $$(docker ps -q -f name=redis) redis-cli SET inventory:101 100 |
| 81 | + @echo "Inventory seeded successfully" |
| 82 | + |
| 83 | +# Seed inventory for specific item |
| 84 | +seed-item: |
| 85 | + @if [ -z "$(ITEM)" ] || [ -z "$(QTY)" ]; then \ |
| 86 | + echo "Usage: make seed-item ITEM=102 QTY=50"; \ |
| 87 | + exit 1; \ |
| 88 | + fi |
| 89 | + @echo "Seeding inventory: $(QTY) items for item_id '$(ITEM)'..." |
| 90 | + @docker exec flash-sale-engine-redis-1 redis-cli SET inventory:$(ITEM) $(QTY) || \ |
| 91 | + docker exec $$(docker ps -q -f name=redis) redis-cli SET inventory:$(ITEM) $(QTY) |
| 92 | + @echo "Inventory seeded successfully" |
| 93 | + |
| 94 | +# Check service health |
| 95 | +health: |
| 96 | + @echo "Checking service health..." |
| 97 | + @curl -s http://localhost:8080/health | python3 -m json.tool 2>/dev/null || \ |
| 98 | + curl -s http://localhost:8080/health | python -m json.tool 2>/dev/null || \ |
| 99 | + curl -s http://localhost:8080/health || \ |
| 100 | + echo "Error: Could not check health. Is the gateway running?" |
| 101 | + |
| 102 | +# View Prometheus metrics (both services) |
| 103 | +metrics: |
| 104 | + @echo "=== Gateway Metrics (port 8080) ===" |
| 105 | + @curl -s http://localhost:8080/metrics | head -20 |
| 106 | + @echo "" |
| 107 | + @echo "=== Processor Metrics (port 9090) ===" |
| 108 | + @curl -s http://localhost:9090/metrics | head -20 |
| 109 | + |
| 110 | +# View gateway metrics |
| 111 | +metrics-gateway: |
| 112 | + @echo "Gateway Metrics:" |
| 113 | + @curl -s http://localhost:8080/metrics | grep -E "^gateway_" | head -20 |
| 114 | + |
| 115 | +# View processor metrics |
| 116 | +metrics-processor: |
| 117 | + @echo "Processor Metrics:" |
| 118 | + @curl -s http://localhost:9090/metrics | grep -E "^processor_" | head -20 |
| 119 | + |
| 120 | +# Check inventory for specific item |
| 121 | +inventory: |
| 122 | + @if [ -z "$(ITEM)" ]; then \ |
| 123 | + echo "Usage: make inventory ITEM=101"; \ |
| 124 | + exit 1; \ |
| 125 | + fi |
| 126 | + @echo "Checking inventory for item_id '$(ITEM)'..." |
| 127 | + @docker exec flash-sale-engine-redis-1 redis-cli GET inventory:$(ITEM) || \ |
| 128 | + docker exec $$(docker ps -q -f name=redis) redis-cli GET inventory:$(ITEM) |
| 129 | + |
| 130 | +# Check order status |
| 131 | +order-status: |
| 132 | + @if [ -z "$(REQ_ID)" ]; then \ |
| 133 | + echo "Usage: make order-status REQ_ID=test-123"; \ |
| 134 | + exit 1; \ |
| 135 | + fi |
| 136 | + @echo "Checking order status for request_id '$(REQ_ID)'..." |
| 137 | + @docker exec flash-sale-engine-redis-1 redis-cli GET "order_status:$(REQ_ID)" || \ |
| 138 | + docker exec $$(docker ps -q -f name=redis) redis-cli GET "order_status:$(REQ_ID)" |
| 139 | + |
| 140 | +# Clean up: stop services and remove containers |
| 141 | +clean: |
| 142 | + @echo "Cleaning up..." |
| 143 | + docker-compose down -v |
| 144 | + @echo "Cleanup complete" |
| 145 | + |
| 146 | +# Rebuild and restart services |
| 147 | +rebuild: |
| 148 | + @echo "Rebuilding and restarting services..." |
| 149 | + docker-compose up -d --build |
| 150 | + @echo "Waiting for services to be ready..." |
| 151 | + @sleep 5 |
| 152 | + @echo "Services rebuilt and restarted. Use 'make health' to check status." |
| 153 | + |
| 154 | +# Show running containers |
| 155 | +ps: |
| 156 | + @docker-compose ps |
| 157 | + |
| 158 | +# Quick test: send a test order |
| 159 | +test-order: |
| 160 | + @echo "Sending test order..." |
| 161 | + @curl -X POST http://localhost:8080/buy \ |
| 162 | + -H "Content-Type: application/json" \ |
| 163 | + -d '{"user_id":"test-user","item_id":"101","amount":1,"request_id":"make-test-'"$$(date +%s)"'"}' \ |
| 164 | + -w "\nHTTP Status: %{http_code}\n" \ |
| 165 | + -s || echo "Error: Could not send order. Is the gateway running?" |
| 166 | + |
| 167 | +# Test idempotency: send same request twice |
| 168 | +test-idempotency: |
| 169 | + @echo "Testing idempotency..." |
| 170 | + @REQ_ID="idempotency-test-$$(date +%s)"; \ |
| 171 | + echo "First request (should succeed):"; \ |
| 172 | + curl -X POST http://localhost:8080/buy \ |
| 173 | + -H "Content-Type: application/json" \ |
| 174 | + -d "{\"user_id\":\"test-user\",\"item_id\":\"101\",\"amount\":1,\"request_id\":\"$$REQ_ID\"}" \ |
| 175 | + -w "\nHTTP Status: %{http_code}\n" \ |
| 176 | + -s; \ |
| 177 | + echo ""; \ |
| 178 | + echo "Second request (should return 409):"; \ |
| 179 | + curl -X POST http://localhost:8080/buy \ |
| 180 | + -H "Content-Type: application/json" \ |
| 181 | + -d "{\"user_id\":\"test-user\",\"item_id\":\"101\",\"amount\":1,\"request_id\":\"$$REQ_ID\"}" \ |
| 182 | + -w "\nHTTP Status: %{http_code}\n" \ |
| 183 | + -s || echo "Error: Could not test idempotency. Is the gateway running?" |
| 184 | + |
0 commit comments