okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
9caf39495
main
1/** 2* One long-lived PowerShell that prints a line per reading, in a loop. The monitor used to start a cold PowerShell for every sample; 3* its WMI start cost more than the probe timeout, so every query failed and WmiPrvSE burned a core (#3939). 4* The loop gives up after `maxStarts` early exits in a row, so a counter that cannot be read stays quiet. 5*/ 6import { spawn } from 'node:child_process' ; 7 8export function createPowerShellLoop ({ script, onLine, spawnFn= spawn , maxStarts= 3 }) { 9let child = null , starts = 0 , buffer = '' ; 10function start () { 11if ( child || starts >= maxStarts ) return ; 12starts ++ ; 13let proc ; 14try { proc = spawnFn ( 'powershell.exe' , [ '-NoProfile' , '-NonInteractive' , '-Command' , script ], { windowsHide :true , stdio :[ 'ignore' , 'pipe' , 'ignore' ] }); } 15catch { return ; } 16child = proc ; 17buffer = '' ; 18proc . on ?.( 'error' , () => { if ( child === proc ) child = null ; }); 19proc . on ?.( 'exit' , () => { if ( child === proc ) child = null ; }); 20proc . stdout ?. on ( 'data' , chunk => { 21buffer += chunk ; 22let i ; 23while (( i = buffer . indexOf ( '\n' )) >= 0 ) { 24const line = buffer . slice ( 0 , i ); 25buffer = buffer . slice ( i + 1 ); 26if ( onLine ( line )) starts = 0 ; 27} 28}); 29proc . unref ?.(); 30proc . stdout ?. unref ?.(); 31} 32return { 33 start, 34stop () { try { child ?. kill (); } catch { /* gone */ } child = null ; }, 35get starts () { return starts ; }, 36}; 37} 38 39let stoppers = null ; 40/** Every loop is ended with the host. */ 41export function endWithHost ( loop ) { 42if ( ! stoppers ) { stoppers = new Set (); process . once ( 'exit' , () => stoppers . forEach ( l => l . stop ())); } 43stoppers . add ( loop ); 44return loop ; 45}