|
| 1 | +import { NextResponse } from 'next/server'; |
| 2 | + |
| 3 | +export async function POST(): Promise<NextResponse> { |
| 4 | + const baseUrl = process.env.DEMO_MODEL_URL || 'http://localhost:8000/v1'; |
| 5 | + const apiKey = process.env.DEMO_API_KEY || ''; |
| 6 | + const modelName = process.env.DEMO_MODEL_NAME || 'default'; |
| 7 | + |
| 8 | + console.log('[Warmup] Starting warmup...'); |
| 9 | + console.log('[Warmup] Base URL:', baseUrl); |
| 10 | + |
| 11 | + const runpodMatch = baseUrl.match(/https:\/\/api\.runpod\.ai\/v2\/([^/]+)/); |
| 12 | + |
| 13 | + if (!runpodMatch) { |
| 14 | + console.log('[Warmup] Not a RunPod endpoint, skipping'); |
| 15 | + return NextResponse.json({ |
| 16 | + status: 'skipped', |
| 17 | + message: 'Not a RunPod endpoint', |
| 18 | + }); |
| 19 | + } |
| 20 | + |
| 21 | + const endpointId = runpodMatch[1]; |
| 22 | + console.log('[Warmup] Endpoint ID:', endpointId); |
| 23 | + |
| 24 | + try { |
| 25 | + const healthUrl = `https://api.runpod.ai/v2/${endpointId}/health`; |
| 26 | + let healthData = null; |
| 27 | + |
| 28 | + try { |
| 29 | + const healthResponse = await fetch(healthUrl, { |
| 30 | + method: 'GET', |
| 31 | + headers: { |
| 32 | + 'Authorization': `Bearer ${apiKey}`, |
| 33 | + }, |
| 34 | + signal: AbortSignal.timeout(5000), |
| 35 | + }); |
| 36 | + |
| 37 | + if (healthResponse.ok) { |
| 38 | + healthData = await healthResponse.json(); |
| 39 | + console.log('[Warmup] Health:', JSON.stringify(healthData)); |
| 40 | + |
| 41 | + if (healthData.workers?.idle > 0) { |
| 42 | + console.log('[Warmup] Idle workers available'); |
| 43 | + return NextResponse.json({ |
| 44 | + status: 'ready', |
| 45 | + message: 'Workers already available', |
| 46 | + workers: healthData.workers, |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + if (healthData.workers?.initializing > 0) { |
| 51 | + console.log('[Warmup] Workers already initializing'); |
| 52 | + return NextResponse.json({ |
| 53 | + status: 'warming', |
| 54 | + message: 'Workers already starting', |
| 55 | + workers: healthData.workers, |
| 56 | + }); |
| 57 | + } |
| 58 | + } |
| 59 | + } catch (e) { |
| 60 | + console.log('[Warmup] Health check error:', e); |
| 61 | + } |
| 62 | + |
| 63 | + const openaiUrl = `${baseUrl}/chat/completions`; |
| 64 | + console.log('[Warmup] Sending to OpenAI endpoint:', openaiUrl); |
| 65 | + |
| 66 | + const abortController = new AbortController(); |
| 67 | + const timeoutId = setTimeout(() => abortController.abort(), 5000); |
| 68 | + |
| 69 | + try { |
| 70 | + const warmupResponse = await fetch(openaiUrl, { |
| 71 | + method: 'POST', |
| 72 | + headers: { |
| 73 | + 'Authorization': `Bearer ${apiKey}`, |
| 74 | + 'Content-Type': 'application/json', |
| 75 | + }, |
| 76 | + body: JSON.stringify({ |
| 77 | + model: modelName, |
| 78 | + messages: [{ role: 'user', content: 'hi' }], |
| 79 | + max_tokens: 1, |
| 80 | + stream: false, |
| 81 | + }), |
| 82 | + signal: abortController.signal, |
| 83 | + }); |
| 84 | + |
| 85 | + clearTimeout(timeoutId); |
| 86 | + |
| 87 | + console.log('[Warmup] Response status:', warmupResponse.status); |
| 88 | + |
| 89 | + return NextResponse.json({ |
| 90 | + status: warmupResponse.status === 200 ? 'ready' : 'warming', |
| 91 | + message: warmupResponse.status === 200 |
| 92 | + ? 'Model responded (was ready)' |
| 93 | + : 'Request queued, worker starting', |
| 94 | + httpStatus: warmupResponse.status, |
| 95 | + workers: healthData?.workers, |
| 96 | + }); |
| 97 | + |
| 98 | + } catch (fetchError) { |
| 99 | + clearTimeout(timeoutId); |
| 100 | + |
| 101 | + if ((fetchError as Error).name === 'AbortError') { |
| 102 | + console.log('[Warmup] Request sent (aborted wait - worker starting)'); |
| 103 | + return NextResponse.json({ |
| 104 | + status: 'warming', |
| 105 | + message: 'Request sent, worker starting', |
| 106 | + workers: healthData?.workers, |
| 107 | + }); |
| 108 | + } |
| 109 | + |
| 110 | + throw fetchError; |
| 111 | + } |
| 112 | + |
| 113 | + } catch (error) { |
| 114 | + console.error('[Warmup] Error:', error); |
| 115 | + return NextResponse.json({ |
| 116 | + status: 'error', |
| 117 | + message: error instanceof Error ? error.message : 'Warmup failed', |
| 118 | + }, { status: 500 }); |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +export async function GET(): Promise<NextResponse> { |
| 123 | + const baseUrl = process.env.DEMO_MODEL_URL || 'http://localhost:8000/v1'; |
| 124 | + const apiKey = process.env.DEMO_API_KEY || ''; |
| 125 | + |
| 126 | + const runpodMatch = baseUrl.match(/https:\/\/api\.runpod\.ai\/v2\/([^/]+)/); |
| 127 | + |
| 128 | + if (!runpodMatch) { |
| 129 | + return NextResponse.json({ |
| 130 | + ready: true, |
| 131 | + message: 'Not a RunPod endpoint' |
| 132 | + }); |
| 133 | + } |
| 134 | + |
| 135 | + const endpointId = runpodMatch[1]; |
| 136 | + const healthUrl = `https://api.runpod.ai/v2/${endpointId}/health`; |
| 137 | + |
| 138 | + try { |
| 139 | + const response = await fetch(healthUrl, { |
| 140 | + method: 'GET', |
| 141 | + headers: { |
| 142 | + 'Authorization': `Bearer ${apiKey}`, |
| 143 | + }, |
| 144 | + signal: AbortSignal.timeout(10000), |
| 145 | + }); |
| 146 | + |
| 147 | + if (!response.ok) { |
| 148 | + console.log('[Warmup GET] Health check failed:', response.status); |
| 149 | + return NextResponse.json({ ready: false, message: 'Health check failed' }); |
| 150 | + } |
| 151 | + |
| 152 | + const health = await response.json(); |
| 153 | + console.log('[Warmup GET] Health:', JSON.stringify(health)); |
| 154 | + |
| 155 | + const idleWorkers = health.workers?.idle || 0; |
| 156 | + const readyWorkers = health.workers?.ready || 0; |
| 157 | + const runningWorkers = health.workers?.running || 0; |
| 158 | + const initializingWorkers = health.workers?.initializing || 0; |
| 159 | + const throttledWorkers = health.workers?.throttled || 0; |
| 160 | + |
| 161 | + const isReady = idleWorkers > 0 || readyWorkers > 0; |
| 162 | + const isWarming = initializingWorkers > 0; |
| 163 | + const isBusy = runningWorkers > 0 && !isReady; |
| 164 | + const jobsInQueue = health.jobs?.inQueue || 0; |
| 165 | + const jobsInProgress = health.jobs?.inProgress || 0; |
| 166 | + |
| 167 | + return NextResponse.json({ |
| 168 | + ready: isReady, |
| 169 | + warming: isWarming, |
| 170 | + busy: isBusy, |
| 171 | + jobsInQueue, |
| 172 | + jobsInProgress, |
| 173 | + workers: { |
| 174 | + idle: idleWorkers, |
| 175 | + ready: readyWorkers, |
| 176 | + running: runningWorkers, |
| 177 | + initializing: initializingWorkers, |
| 178 | + throttled: throttledWorkers, |
| 179 | + }, |
| 180 | + }); |
| 181 | + } catch (error) { |
| 182 | + const isTimeout = error instanceof Error && error.name === 'TimeoutError'; |
| 183 | + if (!isTimeout) { |
| 184 | + console.error('[Warmup GET] Error:', error); |
| 185 | + } |
| 186 | + return NextResponse.json({ |
| 187 | + ready: false, |
| 188 | + warming: true, |
| 189 | + message: isTimeout ? 'Health check timed out' : 'Check failed' |
| 190 | + }); |
| 191 | + } |
| 192 | +} |
0 commit comments