[FIX] 修正重啟後產生重複機台日誌的問題並重構邏輯一致性
1. 修正 ProcessHeartbeat 在快取失效時會誤報韌體與溫度的問題。 2. 引入 Database Fallback 機制:當 Redis 無紀錄時自動比對資料庫現有值。 3. 統一韌體與溫度的偵測邏輯,確保兩者皆為「比對舊值 -> 異動才記 -> 補回快取」。 4. 提升系統對 CI/CD 重啟或快取清理的容錯能力。
This commit is contained in:
parent
080412d2b3
commit
c014d7431c
@ -64,13 +64,17 @@ class ProcessHeartbeat implements ShouldQueue
|
||||
// 2. 韌體版本比對 (有改才傳,有改才記)
|
||||
if (isset($this->payload['firmware_version'])) {
|
||||
$fv = (string) $this->payload['firmware_version'];
|
||||
if (!isset($oldState['firmware_version']) || (string)$oldState['firmware_version'] !== $fv) {
|
||||
|
||||
// 優先從快取取舊版本,若無則從資料庫取
|
||||
$oldVersion = $oldState['firmware_version'] ?? $machine->firmware_version;
|
||||
|
||||
if ((string)$oldVersion !== $fv) {
|
||||
\App\Jobs\Machine\ProcessStateLog::dispatch(
|
||||
$machine->id,
|
||||
$machine->company_id,
|
||||
"Firmware updated to :version",
|
||||
'info',
|
||||
['version' => $fv, 'old' => $oldState['firmware_version'] ?? 'Unknown']
|
||||
['version' => $fv, 'old' => $oldVersion ?? 'Unknown']
|
||||
);
|
||||
}
|
||||
$updateData['firmware_version'] = $fv;
|
||||
@ -87,21 +91,11 @@ class ProcessHeartbeat implements ShouldQueue
|
||||
$tempLogCacheKey = "machine:{$this->serialNo}:last_temp_log";
|
||||
$lastLogEntry = \Illuminate\Support\Facades\Cache::get($tempLogCacheKey);
|
||||
|
||||
$shouldLog = false;
|
||||
if (!$lastLogEntry) {
|
||||
$shouldLog = true;
|
||||
} else {
|
||||
$lastValue = (int) $lastLogEntry['value'];
|
||||
$lastAt = \Carbon\Carbon::parse($lastLogEntry['at']);
|
||||
// 統一邏輯:優先從快取取值,若無則從資料庫取
|
||||
$oldTemp = isset($lastLogEntry['value']) ? (int)$lastLogEntry['value'] : $machine->temperature;
|
||||
|
||||
// 條件 A: 只要溫度與上次記錄不同就記錄 (整數比對)
|
||||
if ($temp !== $lastValue) {
|
||||
Log::debug("ProcessHeartbeat: Temperature changed from {$lastValue} to {$temp}. Triggering log.");
|
||||
$shouldLog = true;
|
||||
}
|
||||
}
|
||||
|
||||
if ($shouldLog) {
|
||||
if ($temp !== (int)$oldTemp) {
|
||||
Log::debug("ProcessHeartbeat: Temperature changed from {$oldTemp} to {$temp}. Triggering log.");
|
||||
\App\Jobs\Machine\ProcessStateLog::dispatch(
|
||||
$machine->id,
|
||||
$machine->company_id,
|
||||
@ -109,13 +103,13 @@ class ProcessHeartbeat implements ShouldQueue
|
||||
'info',
|
||||
['temp' => $temp]
|
||||
);
|
||||
|
||||
// 更新快取,保留 7 天
|
||||
\Illuminate\Support\Facades\Cache::put($tempLogCacheKey, [
|
||||
'value' => $temp,
|
||||
'at' => now()->toDateTimeString()
|
||||
], 604800);
|
||||
}
|
||||
|
||||
// 無論是否有變動,都更新/延長快取壽命,確保快取資料存在
|
||||
\Illuminate\Support\Facades\Cache::put($tempLogCacheKey, [
|
||||
'value' => $temp,
|
||||
'at' => now()->toDateTimeString()
|
||||
], 604800);
|
||||
}
|
||||
|
||||
// 更新快取
|
||||
|
||||
Loading…
Reference in New Issue
Block a user