From d44bf90fe3e6da8f18ecf035480d84f3ae781154 Mon Sep 17 00:00:00 2001 From: yggverse Date: Sun, 24 Mar 2024 18:15:26 +0200 Subject: [PATCH] stop crawler on network connection lost #11 --- composer.json | 3 +- example/config.json | 12 ++++++++ src/cli/document/crawl.php | 59 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 012f838..9e09263 100644 --- a/composer.json +++ b/composer.json @@ -19,6 +19,7 @@ "symfony/dom-crawler": "^6.3", "jdenticon/jdenticon": "^1.0", "yggverse/ftp": "^1.0", - "gregwar/captcha": "^1.2" + "gregwar/captcha": "^1.2", + "yggverse/net": "^1.2" } } diff --git a/example/config.json b/example/config.json index fc3fde0..ea4b119 100644 --- a/example/config.json +++ b/example/config.json @@ -350,5 +350,17 @@ ] } } + }, + "network": + { + "check": + { + "enabled":true, + "timeout":5, + "socket": + { + "201:5eb5:f061:678e:7565:6338:c02c:5251":80 + } + } } } \ No newline at end of file diff --git a/src/cli/document/crawl.php b/src/cli/document/crawl.php index fe7605a..6c161c5 100644 --- a/src/cli/document/crawl.php +++ b/src/cli/document/crawl.php @@ -65,6 +65,65 @@ if (false === sem_acquire($semaphore, true)) exit; } +// Check network connection +if ($config->network->check->enabled) +{ + $network = false; + + foreach ($config->network->check->socket as $host => $port) + { + if ($config->cli->document->crawl->debug->level->notice) + { + echo sprintf( + _('[%s] [notice] check network connection to socket "%s" port "%d"...') . PHP_EOL, + date('c'), + $host, + $port + ); + } + + if (\Yggverse\Net\Socket::isOpen($host, $port, $config->network->check->timeout)) + { + if ($config->cli->document->crawl->debug->level->notice) + { + echo sprintf( + _('[%s] [notice] network connection test successful') . PHP_EOL, + date('c') + ); + } + + $network = true; + break; + } + + else + { + if ($config->cli->document->crawl->debug->level->warning) + { + echo sprintf( + _('[%s] [warning] could not connect to socket "%s" port "%d"...') . PHP_EOL, + date('c'), + $host, + $port + ); + } + } + } + + if (!$network) + { + if ($config->cli->document->crawl->debug->level->error) + { + echo sprintf( + _('[%s] [error] network unreachable!') . PHP_EOL, + date('c') + ); + } + + exit; + } +} + // Init client try {