Browse Source

add lockfile for checker

master
R4SAS 2 years ago
parent
commit
8cf739765e
  1. 1
      .gitignore
  2. 51
      checker.php

1
.gitignore vendored

@ -2,5 +2,6 @@ @@ -2,5 +2,6 @@
/logs/*
/public/export/*
/tmp/*
/checker.lock
/config.php
/hosts.txt

51
checker.php

@ -6,16 +6,32 @@ use App\BOB; @@ -6,16 +6,32 @@ use App\BOB;
use App\DB;
use Amp\Loop;
use Amp\Parallel\Worker\DefaultPool;
use Amp\TimeoutException;
/* Disable execution limit */
set_time_limit(0);
$pdo = (new DB($options))->pdo;
$lockfile = __DIR__ . '/checker.lock';
$full = false;
$results = [];
if($argc == 2 && $argv[1] == "full") {
$full = true;
}
echo "[CHECKER] Checking lockfile" . PHP_EOL;
$fp = fopen($lockfile, 'c');
if(!flock($fp, LOCK_EX | LOCK_NB)) {
echo "[CHECKER] Looks like another checker is running, exiting..." . PHP_EOL;
exit(-1);
}
echo "[DB] Fetching hosts to check from database" . PHP_EOL;
if ((($options['fullhour'] >= 0) && ($options['fullhour'] <= 23)) && date('H') == $options['fullhour']) { // check all hosts at full check hour
if ((($options['fullhour'] >= 0) && ($options['fullhour'] <= 23)) && date('H') == $options['fullhour'] || $full) { // check all hosts at full check hour or when argument 'full' passed
echo "[CHECKER] Starting FULL check" . PHP_EOL;
$STH = $pdo->query("SELECT `host`, `base32` FROM `hosts`");
} else {
@ -41,21 +57,27 @@ if ($bob->setnick()) { @@ -41,21 +57,27 @@ if ($bob->setnick()) {
}
/* Start async checker tasks */
Loop::run(function () use (&$results, $hosts, $options) {
$pool = new DefaultPool($options['checkthreads']);
try {
Loop::run(function () use (&$results, $hosts, $options) {
$pool = new DefaultPool($options['checkthreads']);
$coroutines = [];
$coroutines = [];
foreach ($hosts as $host => $base32) {
$coroutines[$host] = Amp\call(function () use ($pool, $base32, $options) {
return yield $pool->enqueue(new App\Checker($base32, $options));
});
}
foreach ($hosts as $host => $base32) {
$coroutines[$host] = Amp\call(function () use ($pool, $base32, $options) {
return yield $pool->enqueue(new App\Checker($base32, $options));
});
}
$results = yield Amp\Promise\all($coroutines);
$results = yield Amp\Promise\all($coroutines);
return yield $pool->shutdown();
});
//} catch (Amp\TimeoutException $ex) {
} catch (\Throwable $ex) {
echo "[Checker] Exception: " . $ex . PHP_EOL;
}
return yield $pool->shutdown();
});
echo "[BOB] Stopping session" . PHP_EOL;
$bob->stop();
@ -76,4 +98,7 @@ foreach ($results as $host => $ts) { @@ -76,4 +98,7 @@ foreach ($results as $host => $ts) {
$pdo->commit();
echo "Result: Total hosts: " . count($results) . ", Alive: " . $i . PHP_EOL;
echo "[CHECKER] Unlocking and closing lockfile" . PHP_EOL;
fclose($fp);
echo "[CHECKER] Result: Total hosts: " . count($results) . ", Alive: " . $i . PHP_EOL;

Loading…
Cancel
Save