From 45c9a9f4f1ec4592a69e2e3b42b4109bfd91b9e2 Mon Sep 17 00:00:00 2001 From: dongpd Date: Mon, 11 Jul 2022 13:41:25 +0700 Subject: [PATCH] fix bug auto sync feature --- config/params.php | 3 ++- controllers/ApiController.php | 45 +++++++++++++++++++++++++++++++---- 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/config/params.php b/config/params.php index 05a8ead7..858c4256 100644 --- a/config/params.php +++ b/config/params.php @@ -4,5 +4,6 @@ return [ 'adminEmail' => 'admin@example.com', "maxLogs" => 50000, "maxPicture" => 9999, - "version" => "1.0.7" + "version" => "1.0.7", + "autoSyncLog" => true ]; diff --git a/controllers/ApiController.php b/controllers/ApiController.php index 6ef4e1a8..7789a5ca 100644 --- a/controllers/ApiController.php +++ b/controllers/ApiController.php @@ -514,8 +514,24 @@ class ApiController extends Controller { } public function actionCheckLogs() { - set_time_limit(5000); + set_time_limit(5000); Yii::$app->response->format = "json"; + $logProcessStatus = \app\models\SyncUrl::findOne(['key_config' => 'log_process_status']); + if (!$logProcessStatus) { + $model = new \app\models\SyncUrl(); + $model->create(['key_config' => 'log_process_status', 'data' => "true"]); + $logProcessStatus = \app\models\SyncUrl::findOne(['key_config' => 'log_process_status']); + } else { + if ($logProcessStatus->data == "true") { + if (Yii::$app->params['autoSyncLog']) + file_put_contents("logs.txt", "CHECK LOGS PROCESSING\n", FILE_APPEND); + return ["data" => "check logs processing"]; + } else { + $logProcessStatus->data = "true"; + $logProcessStatus->save(); + } + } + $server_ip = \app\models\SyncUrl::findOne(['key_config' => 'server_api']); $ip = "https://dev-dc.beetai.com"; if ($server_ip) @@ -535,11 +551,16 @@ class ApiController extends Controller { ]) ] ])), true); + if (Yii::$app->params['autoSyncLog']) + file_put_contents("logs.txt", "START CHECK LOGS\n", FILE_APPEND); if ($datas['status'] == 10000) { $logs = $datas['data']; $infomation = []; $fault_information = []; + $count = 0; foreach ($logs as $key => $value) { + $count++; + $start = $this->getCurrentTime(); $result = true; if ($value['action'] == "insert_image") $result = $this->insertImage($value['images'][0], $value['id'], $value['files_name'][0]); @@ -558,6 +579,10 @@ class ApiController extends Controller { \Yii::$app->db->createCommand()->truncateTable('list_management')->execute(); array_map('unlink', glob("/var/www/html/BiFace_Server_Lite/web/data/uploads/face/*.*")); } + $finish = $this->getCurrentTime(); + $processTime = round(($finish - $start), 4); + if (Yii::$app->params['autoSyncLog']) + file_put_contents("logs.txt", $count . "\t" . $value['action'] . "\t" . $value['files_name'][0] . "\t" . ($result ? "true" : "false") . "\t" . $processTime . "\t" . $value['name'] . "\n", FILE_APPEND); if (in_array($value['action'], ['insert_image', 'remove_image', 'insert_info', 'remove_info', 'reset_all'])) { if ($result) @@ -587,6 +612,8 @@ class ApiController extends Controller { ])); @file_get_contents("http://localhost:2305/update-feature?total=" . ListManagement::find()->count()); } + $logProcessStatus->data = "false"; + $logProcessStatus->save(); return ["data" => "check logs"]; } @@ -627,8 +654,11 @@ class ApiController extends Controller { ], "type" => "128" ]); - if (!$features['status']) + if (!$features['status']) { + if (Yii::$app->params['autoSyncLog']) + file_put_contents("logs.txt", "[ERROR] extract feature " . json_encode($features) . "\n", FILE_APPEND); return false; + } $features = json_decode($features['data'], true); $ft[] = [ "serverKey" => $files_name, @@ -639,10 +669,17 @@ class ApiController extends Controller { ]; $model->image = json_encode($ft); $model->last_modified = time(); - $model->save(); + return $model->save(); + } else { + if (Yii::$app->params['autoSyncLog']) + file_put_contents("logs.txt", "[ERROR] image error" . $image . "\n", FILE_APPEND); + return false; } + } else { + if (Yii::$app->params['autoSyncLog']) + file_put_contents("logs.txt", "[ERROR] file exists" . $files_name . "\n", FILE_APPEND); + return false; } - return true; } public function removeImage($staff_id, $files_name) {