From 6557a951ba4efaf1724d369fe568b07a32ba6460 Mon Sep 17 00:00:00 2001 From: conrad96 Date: Sun, 30 Mar 2025 14:36:20 +0300 Subject: [PATCH] statistics docker setup, cleanup on sync script, added supervisord --- docker/statistics/Dockerfile | 39 +++++++++++++-------- docker/statistics/crontab | 2 +- docker/statistics/supervisord.conf | 14 ++++++++ docker/statistics/sync.php | 55 ++++++++---------------------- 4 files changed, 54 insertions(+), 56 deletions(-) create mode 100644 docker/statistics/supervisord.conf diff --git a/docker/statistics/Dockerfile b/docker/statistics/Dockerfile index f3de1ebe..12a07b59 100644 --- a/docker/statistics/Dockerfile +++ b/docker/statistics/Dockerfile @@ -1,23 +1,34 @@ FROM php:cli -# Install required dependencies -RUN apt-get update && apt-get install -y cron unzip curl && rm -rf /var/lib/apt/lists/* +RUN apt-get update && apt-get install -y \ + cron \ + unzip \ + curl \ + libpng-dev \ + libjpeg-dev \ + supervisor \ + && pecl install redis \ + && docker-php-ext-install pdo pdo_mysql gd \ + && docker-php-ext-enable redis \ + && rm -rf /var/lib/apt/lists/* -RUN docker-php-ext-install pdo pdo_mysql +RUN curl -sS https://getcomposer.org/installer | \ + php -- --install-dir=/usr/local/bin --filename=composer -RUN apt-get update && apt-get install -y libpng-dev libjpeg-dev \ - && pecl install redis && docker-php-ext-enable redis - -# Copy the cron job script WORKDIR /app -COPY fetch_and_send.php /app/fetch_and_send.php + +# install composer dependencies +COPY . . + +RUN composer install + COPY crontab /etc/cron.d/crontab -RUN chmod +x /app/fetch_and_send.php -# Give execution rights on the cron job file -RUN chmod 0644 /etc/cron.d/crontab && crontab /etc/cron.d/crontab +COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf -RUN touch /var/log/cron.log && touch /app/statistics.log +RUN chmod 0644 /etc/cron.d/crontab \ + && crontab /etc/cron.d/crontab \ + && touch /var/log/cron.log /app/sync.log \ + && chmod +x /app/sync.php -# Start cron in the foreground -CMD ["cron", "-f"] \ No newline at end of file +CMD ["supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"] \ No newline at end of file diff --git a/docker/statistics/crontab b/docker/statistics/crontab index 9ae28477..1277a88b 100644 --- a/docker/statistics/crontab +++ b/docker/statistics/crontab @@ -1 +1 @@ -00 23 * * * php /app/fetch_and_send.php >> /var/log/cron.log 2>&1 \ No newline at end of file +00 23 * * * /usr/local/bin/php /app/sync.php >> /var/log/cron.log 2>&1 \ No newline at end of file diff --git a/docker/statistics/supervisord.conf b/docker/statistics/supervisord.conf new file mode 100644 index 00000000..629d99b1 --- /dev/null +++ b/docker/statistics/supervisord.conf @@ -0,0 +1,14 @@ +[supervisord] +nodaemon=true + +[program:cron] +command=cron -f +autorestart=true + +[program:sync-cron-log] +command=tail -f /var/log/cron.log +autorestart=true + +[program:sync] +command=/usr/local/bin/php /app/sync.php +autorestart=true \ No newline at end of file diff --git a/docker/statistics/sync.php b/docker/statistics/sync.php index 3f6bcb4e..057bd8b8 100644 --- a/docker/statistics/sync.php +++ b/docker/statistics/sync.php @@ -1,29 +1,4 @@ > /var/log/sync.log 2>&1 - * - * Monitoring: - * - script monitoring logs are saved in sync.log - */ require __DIR__ . "/vendor/autoload.php"; use GuzzleHttp\Client; @@ -100,11 +75,8 @@ class sync{ 'timeout' => 3.0, ]); - # init fetch data - $this->payload = $this->fetchData(); - - # check / upload cache - $this->uploadCache(); + # fetch data + $db_data = $this->fetchData(); } function startSync(){ @@ -116,7 +88,13 @@ class sync{ $response = $this->http->get($this->server_status_url, [ 'timeout'=> 10, - 'http_errors'=> true + 'http_errors'=> true, + 'headers' => [ + 'Accept' => 'application/json', + 'X-Requested-With' => 'XMLHttpRequest' + ], + 'verify'=> true, + 'allow_redirects'=> false ]); $status_code = $response->getStatusCode(); @@ -126,7 +104,8 @@ class sync{ $this->logger->debug("Internet connection available.", ["status_code"=> $status_code, "response"=> $response_body]); - $upload = $this->uploadData(); # upload payload + $upload_cache = $this->uploadCache(); + if(!$upload_cache) throw new Exception("Upload failed. [". $upload_cache."]"); $this->logger->info("Internet connection available. uploading data.", ["upload"=> $upload]); @@ -134,7 +113,6 @@ class sync{ }catch(Exception $ex){ $this->logger->error($ex->getMessage()); - $this->saveToCache($this->payload); return false; } } @@ -285,8 +263,8 @@ class sync{ $this->logger->debug("Fetched: ". $data); $this->logger->info("Queries executed successfully. data saved.", ['size'=> strlen($data)]); - - return $data; + + return $this->saveToCache($data); }catch(Exception $ex){ $this->logger->error($ex->getMessage()); return false; @@ -295,9 +273,6 @@ class sync{ public function uploadData($endpoint = null, $data = null) { - if(is_null($endpoint)) $endpoint= $this->endpoint; - if(is_null($data)) $data= $this->payload; - $this->logger->info("Uploading payload.", ["url"=> $this->server. $this->endpoint ]); try{ @@ -330,8 +305,6 @@ class sync{ }catch(Exception $ex){ $this->logger->error($ex->getMessage()); - $this->saveToCache($data); - return false; } } @@ -394,7 +367,7 @@ class sync{ $iterator = null; do { - //get all keys with data: pattern + //get all keys $result = $this->redis->scan($iterator, 'data:*'); if ($result !== false) $keys = array_merge($keys, $result);