// Pilota della prova di carico (prova-carico.sh): gira in un contenitore node sulla rete interna della prova, entra // nell'interfaccia come un browser e usa lo stesso canale WebSocket (comandi con rid, ack, blocco di stato «export»). // INDIRIZZO http://mimante-prova-:8080 // UI_PASSWORD la password casuale della prova (non si stampa mai) // RIGHE righe massime dell'esportazione (1.000.000) // Codice di uscita 0 solo se l'esportazione finisce con esattamente RIGHE righe e la seconda si annulla entro 5 s. const base = process.env.INDIRIZZO; const password = process.env.UI_PASSWORD; const rows = Number(process.env.RIGHE || 1000000); const minutes = 40; function fail(message) { console.error(message); process.exit(1); } main().catch(e => fail(`Prova interrotta: ${e.message}`)); async function main() { const login = await fetch(base + '/accesso', { method: 'POST', body: new URLSearchParams({ password }), redirect: 'manual' }); const cookie = (login.headers.getSetCookie?.() || []).map(c => c.split(';')[0]).find(c => c.startsWith('mimante_sessione=')); if (!cookie) fail(`Accesso all'interfaccia non riuscito (HTTP ${login.status}).`); console.log('accesso all\'interfaccia: ok'); // Origin come la manderebbe il browser: senza, il bot rifiuta il canale (WebServer, OriginCheck). const ws = new WebSocket(base.replace(/^http/, 'ws') + '/ws', { headers: { Cookie: cookie, Origin: new URL(base).origin } }); const pending = new Map(); const watchers = new Set(); let nextRid = 1; ws.onmessage = ev => { let m; try { m = JSON.parse(ev.data); } catch { return; } if (m.t === 'ack' && pending.has(m.rid)) { pending.get(m.rid)(m); pending.delete(m.rid); return; } const exp = m.t === 'export' ? m : m.export; if (exp) for (const w of watchers) w(exp); }; ws.onclose = () => { for (const w of watchers) w({ state: 'closed' }); }; await new Promise((resolve, reject) => { ws.onopen = resolve; ws.onerror = () => reject(new Error('WebSocket non aperto')); }).catch(e => fail(e.message)); function send(msg) { const rid = nextRid++; return new Promise(resolve => { pending.set(rid, resolve); ws.send(JSON.stringify({ ...msg, rid })); }); } /** Aspetta che il blocco «export» arrivi a uno stato finale, entro ms; scrive le fasi una volta sola. */ function until(states, ms) { return new Promise(resolve => { let phase = ''; let last = 0; const timer = setTimeout(() => { watchers.delete(w); resolve({ state: 'timeout' }); }, ms); const w = s => { if (s.state === 'closed') { clearTimeout(timer); watchers.delete(w); resolve(s); return; } if (s.phase && s.phase !== phase) { phase = s.phase; console.log(` fase: ${phase}`); } if (s.state === 'running' && Date.now() - last > 30000) { last = Date.now(); console.log(` ${s.rows ?? 0} righe su ${s.total ?? '?'}`); } if (states.includes(s.state)) { clearTimeout(timer); watchers.delete(w); resolve(s); } }; watchers.add(w); }); } const day = d => d.toISOString().slice(0, 10); const now = Date.now(); const request = { t: 'export.start', what: 'dati_modello', from: day(new Date(now - 35 * 86400000)), to: day(new Date(now + 86400000)), onlyMyBids: false, maxRows: rows }; const started = Date.now(); const done = until(['done', 'failed', 'cancelled'], minutes * 60000); const ack = await send(request); if (!ack.ok) fail(`L'esportazione non parte: ${ack.msg}`); const end = await done; const seconds = Math.round((Date.now() - started) / 1000); if (end.state !== 'done') fail(`Esportazione finita come «${end.state}» dopo ${seconds} s: ${end.message ?? ''}`); console.log(`esportazione finita in ${seconds} s: ${end.rows} righe, campionata: ${end.sampled ? 'sì' : 'no'}, file: ${(end.files || []).join(', ')}`); if (end.rows !== rows) fail(`Righe scritte ${end.rows}, attese ${rows}.`); // Una seconda, annullata dopo 3 secondi: deve fermarsi entro 5 senza lasciare file a metà. const running = until(['running'], 10000); const ack2 = await send(request); if (!ack2.ok) fail(`La seconda esportazione non parte: ${ack2.msg}`); await running; await new Promise(r => setTimeout(r, 3000)); const stopped = until(['cancelled', 'done', 'failed'], 5000); const asked = Date.now(); const ack3 = await send({ t: 'export.cancel' }); if (!ack3.ok) fail(`Annullamento rifiutato: ${ack3.msg}`); const after = await stopped; if (after.state !== 'cancelled') fail(`Dopo l'annullamento lo stato è «${after.state}» invece di «cancelled».`); console.log(`annullata in ${((Date.now() - asked) / 1000).toFixed(1)} s`); ws.close(); process.exit(0); }