From 3e70bc9c2aec90eeaf62f4637432f264566efc38 Mon Sep 17 00:00:00 2001 From: ghost Date: Sun, 17 Sep 2023 00:13:16 +0300 Subject: [PATCH] add log settings for scrape and sitemap crontab tasks --- example/environment/env.example.php | 6 ++++ src/crontab/scrape.php | 49 ++++++++++++++++++++--------- src/crontab/sitemap.php | 49 ++++++++++++++++++++--------- 3 files changed, 76 insertions(+), 28 deletions(-) diff --git a/example/environment/env.example.php b/example/environment/env.example.php index efd0c45..709232a 100644 --- a/example/environment/env.example.php +++ b/example/environment/env.example.php @@ -179,6 +179,12 @@ define('API_IMPORT_MAGNET_VIEWS_ENABLED', true); // depends of API_IMP // Logs define('LOG_DIRECTORY', __DIR__ . '/../storage/log'); +define('LOG_CRONTAB_SCRAPE_ENABLED', true); +define('LOG_CRONTAB_SCRAPE_FILENAME', sprintf('crontab_scrape_%s.log', date('Y-m-d'))); + +define('LOG_CRONTAB_SITEMAP_ENABLED', true); +define('LOG_CRONTAB_SITEMAP_FILENAME', sprintf('crontab_sitemap_%s.log', date('Y-m-d'))); + define('LOG_CRONTAB_EXPORT_FEED_ENABLED', true); define('LOG_CRONTAB_EXPORT_FEED_FILENAME', sprintf('crontab_export_feed_%s.log', date('Y-m-d'))); diff --git a/src/crontab/scrape.php b/src/crontab/scrape.php index 45185a4..f974165 100644 --- a/src/crontab/scrape.php +++ b/src/crontab/scrape.php @@ -17,6 +17,16 @@ $debug = [ 'ISO8601' => date('c'), 'total' => microtime(true), ], + 'http' => + [ + 'total' => 0, + ], + 'memory' => + [ + 'start' => memory_get_usage(), + 'total' => 0, + 'peaks' => 0 + ], ]; // Init Scraper @@ -121,17 +131,28 @@ try { } // Debug output -$debug['time']['total'] = microtime(true) - $debug['time']['total']; - -print_r( - array_merge($debug, [ - 'db' => [ - 'total' => [ - 'select' => $db->getDebug()->query->select->total, - 'insert' => $db->getDebug()->query->insert->total, - 'update' => $db->getDebug()->query->update->total, - 'delete' => $db->getDebug()->query->delete->total, - ] - ] - ]) -); \ No newline at end of file +$debug['time']['total'] = microtime(true) - $debug['time']['total']; + +$debug['memory']['total'] = memory_get_usage() - $debug['memory']['start']; +$debug['memory']['peaks'] = memory_get_peak_usage(); + +$debug['db']['total']['select'] = $db->getDebug()->query->select->total; +$debug['db']['total']['insert'] = $db->getDebug()->query->insert->total; +$debug['db']['total']['update'] = $db->getDebug()->query->update->total; +$debug['db']['total']['delete'] = $db->getDebug()->query->delete->total; + +print_r($debug); + +// Debug log +if (LOG_CRONTAB_SCRAPE_ENABLED) +{ + @mkdir(LOG_DIRECTORY, 0774, true); + + if ($handle = fopen(LOG_DIRECTORY . '/' . LOG_CRONTAB_SCRAPE_FILENAME, 'a+')) + { + fwrite($handle, print_r($debug, true)); + fclose($handle); + + chmod(LOG_DIRECTORY . '/' . LOG_CRONTAB_SCRAPE_FILENAME, 0774); + } +} \ No newline at end of file diff --git a/src/crontab/sitemap.php b/src/crontab/sitemap.php index 94f6953..1c65e87 100644 --- a/src/crontab/sitemap.php +++ b/src/crontab/sitemap.php @@ -17,6 +17,16 @@ $debug = [ 'ISO8601' => date('c'), 'total' => microtime(true), ], + 'http' => + [ + 'total' => 0, + ], + 'memory' => + [ + 'start' => memory_get_usage(), + 'total' => 0, + 'peaks' => 0 + ], ]; // Begin @@ -49,17 +59,28 @@ try { } // Debug output -$debug['time']['total'] = microtime(true) - $debug['time']['total']; - -print_r( - array_merge($debug, [ - 'db' => [ - 'total' => [ - 'select' => $db->getDebug()->query->select->total, - 'insert' => $db->getDebug()->query->insert->total, - 'update' => $db->getDebug()->query->update->total, - 'delete' => $db->getDebug()->query->delete->total, - ] - ] - ]) -); \ No newline at end of file +$debug['time']['total'] = microtime(true) - $debug['time']['total']; + +$debug['memory']['total'] = memory_get_usage() - $debug['memory']['start']; +$debug['memory']['peaks'] = memory_get_peak_usage(); + +$debug['db']['total']['select'] = $db->getDebug()->query->select->total; +$debug['db']['total']['insert'] = $db->getDebug()->query->insert->total; +$debug['db']['total']['update'] = $db->getDebug()->query->update->total; +$debug['db']['total']['delete'] = $db->getDebug()->query->delete->total; + +print_r($debug); + +// Debug log +if (LOG_CRONTAB_SITEMAP_ENABLED) +{ + @mkdir(LOG_DIRECTORY, 0774, true); + + if ($handle = fopen(LOG_DIRECTORY . '/' . LOG_CRONTAB_SITEMAP_FILENAME, 'a+')) + { + fwrite($handle, print_r($debug, true)); + fclose($handle); + + chmod(LOG_DIRECTORY . '/' . LOG_CRONTAB_SITEMAP_FILENAME, 0774); + } +} \ No newline at end of file