statistics docker setup, cleanup on sync script, added supervisord

This commit is contained in:
2025-03-30 14:36:20 +03:00
parent b0c09ade50
commit 6557a951ba
4 changed files with 54 additions and 56 deletions
+14 -41
View File
@@ -1,29 +1,4 @@
<?php
/**
* Data Synchronization Script
*
* @filename sync.php
* @description Performs periodic data synchronization between local & remote database
* @author conrad96
* @date 2025-03-26
* @license MIT
*
* @usage Executed via cronjob at configured intervals
*
* Overview:
* - Check available services e.g Redis, MySQL
* - Perform periodic query fetch
* - Check Internet connectivity before uploading payload.
* - Saving query result set to redis cache
* - Upload cached data
*
* Cron Configuration Example:
* execution of sync.php after every hour
* 0 + + + php /app/sync.php >> /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);