From 8cf739765e409ab3e7bf58db15055a5ea6b50a08 Mon Sep 17 00:00:00 2001 From: r4sas Date: Sun, 19 Feb 2023 11:46:13 +0000 Subject: [PATCH] add lockfile for checker --- .gitignore | 1 + checker.php | 51 ++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index e9a0a5b..4bbb279 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,6 @@ /logs/* /public/export/* /tmp/* +/checker.lock /config.php /hosts.txt diff --git a/checker.php b/checker.php index a266321..ffa792c 100644 --- a/checker.php +++ b/checker.php @@ -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()) { } /* 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) { $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;