Added “round [number]” to every status “eecho”; changed all “echo('...')” to “echo '...'”

This commit is contained in:
pezcurrel 2023-12-29 13:02:40 +01:00
parent ececc8e8c8
commit 6a750a2f3c

View file

@ -164,10 +164,10 @@ for ($i=1; $i<$argc; $i++) {
$opts['minmsgimplev']=array_search(ucfirst(strtolower($argv[$i])),$msglevs);
} elseif ($argv[$i]=='-G' || $argv[$i]=='--graceline') {
$graceline=time()-$opts['gracetime'];
echo('Graceline: '.$graceline.' ('.date('Y-m-d H:i:s',$graceline).').'.N);
echo 'Graceline: '.$graceline.' ('.date('Y-m-d H:i:s',$graceline).').'.N;
exit(0);
} elseif ($argv[$i]=='-h' || $argv[$i]=='--help') {
echo($help);
echo $help;
exit(0);
} else {
mexit(3,'dont know how to interpret «'.$argv[$i].'» (use «-h» to read the help text).'.N,1,false);
@ -241,6 +241,7 @@ exit(0);
function crawl(&$list,$id) {
global $insts, $ckinsts, $deadinsts, $tini, $opts, $maxround, $totnewc, $link;
// wouldn't make sense to filter $list here: filtering already happens before adding an instance to next round list
$newc=0;
lecho(1,'###### START OF ROUND '.$id.' ######'.N);
$clist=count($list);
@ -252,27 +253,27 @@ function crawl(&$list,$id) {
$responded=false;
$i++;
lecho(1,'round '.$id.': working on instance «'.$inst.'» ('.$i.'/'.$clist.').'.N);
updexarr();
waituntilonline();
lecho(1,'trying to load instance «'.$inst.s peers...'.N);
updexarr($id);
waituntilonline($id);
lecho(1,'round '.$id.': trying to load instance «'.$inst.s peers...'.N);
$peers=gurl('https://'.$inst.'/api/v1/instance/peers',$opts['conntimeout'],$opts['functimeout']);
if ($peers['cont']===false) {
lecho(2,'could not load instance «'.$inst.s peers: '.$peers['emsg'].'.'.N);
lecho(2,'round '.$id.': could not load instance «'.$inst.s peers: '.$peers['emsg'].'.'.N);
} else {
$peers=@json_decode($peers['cont'],true);
if (!is_array($peers)) {
lecho(2,'expecting instance «'.$inst.s peers, got bad JSON instead.'.N);
lecho(2,'round '.$id.': expecting instance «'.$inst.s peers, got bad JSON instead.'.N);
} else {
$responded=true;
$cpeers=count($peers);
lecho(1,'successfully loaded instance «'.$inst.s peers ('.$cpeers.') :-)'.N);
lecho(1,'round '.$id.': successfully loaded instance «'.$inst.s peers ('.$cpeers.') :-)'.N);
$pi=1;
foreach ($peers as $key=>$peer) {
if ($key!=$pi-1) {
lecho(2,'instance «'.$inst.s peers: entity '.$pi.'/'.$cpeers.'s key is not sequential: not checking further.'.N);
lecho(2,'round '.$id.': instance «'.$inst.s peers: entity '.$pi.'/'.$cpeers.'s key is not sequential: not checking further.'.N);
break;
} elseif (!is_string($peer)) {
lecho(2,'instance «'.$inst.s peers: entity '.$pi.'/'.$cpeers.' is not a string: not checking further.'.N);
lecho(2,'round '.$id.': instance «'.$inst.s peers: entity '.$pi.'/'.$cpeers.' is not a string: not checking further.'.N);
break;
} else {
$whynot=[];
@ -283,9 +284,9 @@ function crawl(&$list,$id) {
if (in_array($peer,$nlist)) $whynot[]='it is already present in next round list';
if ($opts['excludedead'] && in_array($peer,$deadinsts)) $whynot[]='its dead';
if (count($whynot)>0) {
lecho(0,'instance «'.$inst.'»: not adding peer «'.$peer.'» ('.$pi.'/'.$cpeers.') to next round list: '.implode(', ',$whynot).'.'.N);
lecho(0,'round '.$id.': instance «'.$inst.'»: not adding peer «'.$peer.'» ('.$pi.'/'.$cpeers.') to next round list: '.implode(', ',$whynot).'.'.N);
} else {
lecho(1,'instance «'.$inst.'»: adding peer «'.$peer.'» ('.$pi.'/'.$cpeers.') to next round list :-)'.N);
lecho(1,'round '.$id.': instance «'.$inst.'»: adding peer «'.$peer.'» ('.$pi.'/'.$cpeers.') to next round list :-)'.N);
$nlist[]=$peer;
}
}
@ -294,52 +295,53 @@ function crawl(&$list,$id) {
}
}
if (!$responded) {
lecho(1,'instance «'.$inst.'» didnt respond at its “peers” endpoint; trying to load its info from “instance” endpoint...'.N);
lecho(1,'round '.$id.': instance «'.$inst.'» didnt respond at its “peers” endpoint; trying to load its info from “instance” endpoint...'.N);
$instinfo=gurl('https://'.$inst.'/api/v1/instance',$opts['conntimeout'],$opts['functimeout']);
if ($instinfo['cont']===false) {
lecho(2,'could not load instance «'.$inst.s info: '.$instinfo['emsg'].'.'.N);
lecho(2,'round '.$id.': could not load instance «'.$inst.s info: '.$instinfo['emsg'].'.'.N);
} else {
$instinfo=@json_decode($instinfo['cont'],true);
if (is_array($instinfo))
$responded=true;
else
lecho(2,'expecting instance «'.$inst.s info, got bad JSON instead.'.N);
lecho(2,'round '.$id.': expecting instance «'.$inst.s info, got bad JSON instead.'.N);
}
}
$now=time();
if ($responded) {
lecho(1,'instance «'.$inst.'» responded :-)'.N);
lecho(1,'round '.$id.': instance «'.$inst.'» responded :-)'.N);
$insts[]=$inst;
$res=myq($link,'SELECT ID FROM Instances WHERE URI=\''.myesc($link,$inst).'\'');
$cres=mysqli_num_rows($res);
if ($cres<1) {
lecho(1,'instance «'.$inst.'» is new to “Instances” table, adding it :-)'.N);
lecho(1,'round '.$id.': instance «'.$inst.'» is new to “Instances” table, adding it :-)'.N);
myq($link,'INSERT INTO Instances SET URI=\''.myesc($link,$inst).'\', InsertTS='.$now);
$totnewc++;
$newc++;
} elseif ($cres>1) {
lecho(2,'instance «'.$inst.'» has '.$cres.' records in “Instances” table! :-('.N);
lecho(2,'round '.$id.': instance «'.$inst.'» has '.$cres.' records in “Instances” table! :-('.N);
} else {
lecho(1,'instance «'.$inst.'» is already present in “Instances” table.'.N);
lecho(1,'round '.$id.': instance «'.$inst.'» is already present in “Instances” table.'.N);
}
} else {
lecho(1,'instance «'.$inst.'» didnt respond :-('.N);
lecho(1,'round '.$id.': instance «'.$inst.'» didnt respond :-('.N);
}
$res=myq($link,'SELECT * FROM Peers WHERE Hostname=\''.myesc($link,$inst).'\'');
$cres=mysqli_num_rows($res);
if ($cres<1) {
lecho(1,'instance «'.$inst.'» is new to “Peers” table, adding it :-)'.N);
lecho(1,'round '.$id.': instance «'.$inst.'» is new to “Peers” table, adding it :-)'.N);
$query='INSERT INTO Peers SET Hostname=\''.myesc($link,$inst).'\', FirstCheckTS='.$now;
if ($responded) $query.=', LastOkCheckTS='.$now;
myq($link,$query);
} elseif ($cres>0) {
if ($cres>1) lecho(2,'«'.$inst.'» has '.$cres.' records in “Peers” table! :-('.N);
if ($cres>1) lecho(2,'round '.$id.': instance «'.$inst.'» has '.$cres.' records in “Peers” table! :-('.N);
if ($responded) {
lecho(1,'instance «'.$inst.'» is already present in “Peers” table, but it responded: updating its records “LastOkCheckTS” value...'.N);
lecho(1,'round '.$id.': instance «'.$inst.'» is already present in “Peers” table, but it responded: updating its records “LastOkCheckTS” value...'.N);
$row=mysqli_fetch_assoc($res);
myq($link,'UPDATE Peers SET LastOkCheckTS='.$now.' WHERE ID='.$row['ID']);
}
}
$ckinsts[]=$inst;
$now=time();
$rtela=$now-$rtini;
lecho(1,'round '.$id.': finished working on instance «'.$inst.'» ('.$i.'/'.$clist.'); RoundElapsedTime: '.ght($rtela,null,0).'; RoundEstimatedTimeRemaining: '.ght($rtela/$i*$clist-$rtela,null,0).'; RoundNewInsts: '.$newc.'; NextRoundInsts: '.count($nlist).'; TotElapsedTime: '.ght($now-$tini,null,0).'; TotConsideredInsts: '.count($ckinsts).'; TotRespondingInsts: '.count($insts).'; TotNewInsts: '.$totnewc.'; using '.ghs(memory_get_usage(true)).' mem. (peak: '.ghs(memory_get_peak_usage(true)).').'.N);
@ -372,7 +374,7 @@ function lecho($lev,$msg) {
$msg=$time.' '.$msglevs[$lev].': '.$msg;
if ($lev>=$opts['minmsgimplev']) {
if ($lev<2)
echo($msg);
echo $msg;
else
fwrite(STDERR,$msg);
}
@ -410,7 +412,7 @@ function sortcheckandsave(&$arr,$arrdesc,&$fp) {
}
function sighandler($signal) {
echo(N);
echo N;
mexit(1,'interrupted (signal: '.$signal.').'.N,0,true);
}
@ -421,19 +423,19 @@ function isempty($val) {
return(false);
}
function waituntilonline() {
function waituntilonline($roundid) {
$url='www.google.com';
$gotoff=false;
while (false===($f=@fsockopen($url,80,$errno,$errstr,1))) {
$gotoff=true;
lecho(2,'it seems we are offline, waiting for 10 seconds before retrying...'.N);
lecho(2,'round '.$roundid.': it seems we are offline, waiting 10 seconds before retrying...'.N);
sleep(10);
}
fclose($f);
if ($gotoff) lecho(1,'it seems we are back online! :-)'.N);
if ($gotoff) lecho(1,'round '.$roundid.': it seems we are back online! :-)'.N);
}
function updexarr() {
function updexarr($roundid) {
global $exarr, $opts;
if (!is_null($opts['excludefp'])) {
$f=@fopen($opts['excludefp'],'r');
@ -447,11 +449,11 @@ function updexarr() {
if (@preg_match($line,'foo')!==false)
$exarr[]=$line;
else
lecho(2,'exclude file «'.$opts['excludefp'].'» contains an invalid regular expression on line '.$i.': «'.$line.'».'.N);
lecho(2,'round '.$roundid.': exclude file «'.$opts['excludefp'].'» contains an invalid regular expression on line '.$i.': «'.$line.'».'.N);
}
}
} else {
lecho(2,'could not open exclude file «'.$opts['excludefp'].'» for reading.'.N);
lecho(2,'round '.$roundid.': could not open exclude file «'.$opts['excludefp'].'» for reading.'.N);
}
}
}