Added code to store and consider “instance checks” made by the script to independently mark peers ad dead
This commit is contained in:
parent
f6bc6a12d4
commit
c27053314a
1 changed files with 57 additions and 16 deletions
|
@ -22,9 +22,12 @@ require(__DIR__.'/../site/mustard/include/gurl.php');
|
|||
require(__DIR__.'/../site/mustard/include/ghs.php');
|
||||
require(__DIR__.'/../site/mustard/include/ght.php');
|
||||
|
||||
use function mysqli_real_escape_string as myesc;
|
||||
|
||||
$opts=[
|
||||
'inifp'=>__DIR__.'/../conf/mustard.ini',
|
||||
'startinst'=>'mastodon.social',
|
||||
'deadline'=>62*24*60*60,
|
||||
'peersfp'=>__DIR__.'/peers',
|
||||
'apeersfp'=>__DIR__.'/peers.all',
|
||||
'cpeersfp'=>__DIR__.'/peers.checked',
|
||||
|
@ -171,23 +174,28 @@ pcntl_signal(SIGTERM,'signalHandler');// Termination ('kill' was called)
|
|||
pcntl_signal(SIGHUP,'signalHandler');// Terminal log-out
|
||||
pcntl_signal(SIGINT,'signalHandler');// Interrupted (Ctrl-C is pressed)
|
||||
|
||||
$iniarr=@parse_ini_file($opts['inifp'])
|
||||
or mexit('Error: couldn’t open «'.$opts['inifp'].'».'.N,1);
|
||||
try { $link=@mysqli_connect($iniarr['db_host'],$iniarr['db_admin_name'],$iniarr['db_admin_password'],$iniarr['db_name'],$iniarr['db_port'],$iniarr['db_socket']); }
|
||||
catch (Exception $error) { mexit('Error: couldn’t connect to MySQL server: '.mysqli_connect_error().'.'.N,1,true); }
|
||||
// for php versions < 8
|
||||
if ($link===false) mexit('Error: couldn’t connect to MySQL server: '.mysqli_connect_error().'.'.N,1,true);
|
||||
try { $res=mysqli_set_charset($link,'utf8mb4'); }
|
||||
catch (Exception $error) { mexit('Error: couldn’t set «utf8mb4» charset for MySQL: '.mysqli_error($link).' ('.mysqli_errno($link).'.'.N,1,true); }
|
||||
// for php versions < 8
|
||||
if ($res===false) mexit('Error: couldn’t set «utf8mb4» charset for MySQL: '.mysqli_error($link).' ('.mysqli_errno($link).').'.N,1,true);
|
||||
|
||||
$deadinsts=[];
|
||||
if ($opts['excludedead']) {
|
||||
$iniarr=@parse_ini_file($opts['inifp'])
|
||||
or mexit('Error: couldn’t open «'.$opts['inifp'].'».'.N,1);
|
||||
try { $link=@mysqli_connect($iniarr['db_host'],$iniarr['db_admin_name'],$iniarr['db_admin_password'],$iniarr['db_name'],$iniarr['db_port'],$iniarr['db_socket']); }
|
||||
catch (Exception $error) { mexit('Error: couldn’t connect to MySQL server: '.mysqli_connect_error().'.'.N,1,true); }
|
||||
// for php versions < 8
|
||||
if ($link===false) mexit('Error: couldn’t connect to MySQL server: '.mysqli_connect_error().'.'.N,1,true);
|
||||
try { $res=mysqli_set_charset($link,'utf8mb4'); }
|
||||
catch (Exception $error) { mexit('Error: couldn’t set «utf8mb4» charset for MySQL: '.mysqli_error($link).' ('.mysqli_errno($link).'.'.N,1,true); }
|
||||
// for php versions < 8
|
||||
if ($res===false) mexit('Error: couldn’t set «utf8mb4» charset for MySQL: '.mysqli_error($link).' ('.mysqli_errno($link).').'.N,1,true);
|
||||
$res=myq($link,'SELECT URI FROM Instances WHERE Dead=1');
|
||||
mysqli_close($link);
|
||||
while ($row=mysqli_fetch_assoc($res))
|
||||
$deadinsts[]=$row['URI'];
|
||||
unset($res);
|
||||
if (!in_array($row['URI'],$deadinsts))
|
||||
$deadinsts[]=$row['URI'];
|
||||
$res=mysq($link,'SELECT Hostname FROM Peers WHERE Dead=1');
|
||||
while ($row=mysqli_fetch_assoc($res))
|
||||
if (!in_array($row['Hostname'],$deadinsts))
|
||||
$deadinsts[]=$row['URI'];
|
||||
unset($res,$row);
|
||||
gecho('Loaded list of dead instances ('.count($deadinsts).').'.N,true,false);
|
||||
}
|
||||
|
||||
|
@ -211,9 +219,11 @@ if (!$apeersf) mexit('Error: couldn’t open «'.$opts['apeersfp'].'» in write
|
|||
$cpeersf=@fopen($opts['cpeersfp'],'w');
|
||||
if (!$cpeersf) mexit('Error: couldn’t open «'.$opts['cpeersfp'].'» in write mode.'.N,1);
|
||||
|
||||
$maxround=1;
|
||||
$tini=time();
|
||||
crawl([$opts['startinst']],1);
|
||||
gecho('Done crawling! :-)'.N,true,false);
|
||||
mysqli_close($link);
|
||||
shutdown(true);
|
||||
$now=time();
|
||||
gecho('Crawl started on '.date('Y-m-d H:i:s',$tini).' and ended on '.date('Y-m-d H:i:s',$now).'.'.N,true,false);
|
||||
|
@ -224,7 +234,7 @@ exit(0);
|
|||
// functions
|
||||
|
||||
function crawl($list,$id) {
|
||||
global $insts, $deadinsts, $cinsts, $ainsts, $tini, $opts, $peersf, $cpeersf, $apeersf, $maxround;
|
||||
global $insts, $deadinsts, $cinsts, $ainsts, $tini, $opts, $peersf, $cpeersf, $apeersf, $maxround, $link;
|
||||
gecho('###### START OF ROUND '.$id.' ######'.N,true,false);
|
||||
$nlist=[];
|
||||
$c=count($list);
|
||||
|
@ -245,6 +255,7 @@ function crawl($list,$id) {
|
|||
$peers=gurl('https://'.$inst.'/api/v1/instance/peers',$opts['timeout'],$opts['curltimeout']);
|
||||
$cinsts[]=$inst;// don't need to chech if in_array
|
||||
fwrite($cpeersf,$inst.N);
|
||||
$responded=0;
|
||||
if ($peers['cont']===false) {
|
||||
gecho('Error loading «'.$inst.'»’s peers: '.$peers['emsg'].'.'.N,true,true);
|
||||
} else {
|
||||
|
@ -253,6 +264,7 @@ function crawl($list,$id) {
|
|||
gecho('Error loading «'.$inst.'»’s peers: got not good JSON.'.N,true,true);
|
||||
} else {
|
||||
gecho('Successfully loaded «'.$inst.'»’s peers :-)'.N,true,false);
|
||||
$responded=1;
|
||||
if (!in_array($inst,$insts)) {
|
||||
gecho('Discovered instance «'.$inst.'» :-)'.N,true,false);
|
||||
$insts[]=$inst;
|
||||
|
@ -280,6 +292,35 @@ function crawl($list,$id) {
|
|||
}
|
||||
}
|
||||
}
|
||||
$instid=0;
|
||||
$res=myq($link,'SELECT * FROM Peers WHERE Hostname=\''.myesc($link,$inst).'\'');
|
||||
if (mysqli_num_rows($res)>0) {
|
||||
$row=mysqli_fetch_assoc($res);
|
||||
$instid=$row['ID'];
|
||||
$dead=0;
|
||||
if (!$responded) {
|
||||
// we check the last time instance responded, if ever
|
||||
$res=myq($link,'SELECT Time FROM PeersChecks WHERE InstID='.$instid.' AND Status=1 ORDER BY Time DESC LIMIT 1',__LINE__);
|
||||
// if instance never responded we consider the time of first check
|
||||
if (mysqli_num_rows($res)==0)
|
||||
$res=myq($link,'SELECT Time FROM PeersChecks WHERE InstID='.$instid.' ORDER BY Time ASC LIMIT 1',__LINE__);
|
||||
if (mysqli_num_rows($res)>0) {
|
||||
$row=mysqli_fetch_assoc($res);
|
||||
if ($now-$row['Time']>$opts['deadline']) {
|
||||
$dead=1;
|
||||
gecho('«'.$instid.'» just died!',true,true);
|
||||
}
|
||||
} else {
|
||||
gecho('«'.$inst.'» exists in Peers table but there’s no data about it in PeersChecks!'.N,true,true);
|
||||
}
|
||||
}
|
||||
$query='UPDATE Peers SET Hostname=\''.myesc($link,$inst).'\', IsDead='.$dead.' WHERE ID='.$instid;
|
||||
} else {
|
||||
$query='INSERT INTO Peers SET Hostname=\''.myesc($link,$inst).'\', IsDead=0';
|
||||
}
|
||||
myq($link,$query);
|
||||
if ($instid==0) $instid=mysqli_insert_id($link);
|
||||
myq($link,'INSERT INTO PeersChecks SET InstID='.$instid.', Time='.$now.', Status='.$responded);
|
||||
}
|
||||
if (count($nlist)>0) {
|
||||
unset($list);
|
||||
|
@ -343,10 +384,10 @@ function sortcheckandsave(&$arr,$arrdesc,&$fp) {
|
|||
}
|
||||
|
||||
function shutdown($dosort) {
|
||||
global $opts, $peersf, $apeersf, $insts, $ainsts, $lockfp;
|
||||
global $opts, $peersf, $apeersf, $cpeersf, $insts, $ainsts, $cinsts, $lockfp;
|
||||
if (isset($peersf) && $peersf!==false) @fclose($peersf);
|
||||
if (isset($apeersf) && $apeersf!==false) @fclose($apeersf);
|
||||
if (isset($cpeersf) && $cpeersf!==false) @fclose($apeersf);
|
||||
if (isset($cpeersf) && $cpeersf!==false) @fclose($cpeersf);
|
||||
if (isset($lockfp) && file_exists($lockfp)) unlink($lockfp);
|
||||
if ($dosort) {
|
||||
sortcheckandsave($insts,'list of responding instances',$opts['peersfp']);
|
||||
|
|
Loading…
Reference in a new issue