fix : sync issu

This commit is contained in:
SajjadMahmoody
2026-04-19 11:29:57 +03:30
parent d657c48600
commit 95592aa510
6 changed files with 63 additions and 22 deletions
+33 -12
View File
@@ -51,8 +51,8 @@ public function sync(): array
$report = [];
$baseUrl = "http://{$this->peerIp}:{$this->peerPort}";
$peerCursor = (int) \App\Models\OsurgInitial::val('sync_peer_cursor', 0);
$ourLastSync = (int) \App\Models\OsurgInitial::val('our_sync_cursor', 0);
$peerCursor = \App\Models\OsurgInitial::val('sync_peer_cursor') ?: null;
$ourLastSync = \App\Models\OsurgInitial::val('our_sync_cursor') ?: null;
try {
$pullResponse = Http::timeout(30)
@@ -65,14 +65,27 @@ public function sync(): array
if (! empty($peerChanges)) {
app()->instance('sync.applying', true);
DB::transaction(function () use ($peerChanges, &$report) {
$pullReport = [];
DB::transaction(function () use ($peerChanges, &$pullReport) {
foreach ($peerChanges as $change) {
$this->applyChange($change, 'دریافت', $report);
$this->applyChange($change, 'دریافت', $pullReport);
}
});
foreach ($pullReport as $line) {
$report[] = $line;
}
$maxPeerChangeId = max(array_column($peerChanges, 'id'));
$this->savePeerCursor(max($peerCursor, $maxPeerChangeId));
$pullHasErrors = ! empty(array_filter($pullReport, fn ($l) => str_contains((string) $l, '✗')));
if (! $pullHasErrors) {
$datetimes = array_filter(array_column($peerChanges, 'datetime'));
$maxPeerDatetime = \Carbon\Carbon::parse(max($datetimes))->format('Y-m-d H:i:s');
if ($peerCursor === null || $maxPeerDatetime > $peerCursor) {
$this->savePeerCursor($maxPeerDatetime);
}
} else {
$report[] = 'هشدار: peer_cursor پیش نرفت — برخی تغییرات دریافتی اعمال نشد و دفعه بعد retry می‌شه.';
}
if ($this->isFtpConfigured()) {
$this->transferFilesViaFtp($peerChanges, 's2c', $report);
@@ -98,14 +111,22 @@ public function sync(): array
->post("{$baseUrl}/api/sync/apply", ['changes' => $ourChanges]);
if ($pushResponse->successful()) {
foreach ($pushResponse->json('report', []) as $line) {
$pushReport = $pushResponse->json('report', []);
foreach ($pushReport as $line) {
$report[] = 'ارسال: ' . $line;
}
$report[] = count($ourChanges) . ' تغییر به سیستم مقابل ارسال شد.';
if (! empty($ourChanges)) {
$maxOurId = max(array_column($ourChanges, 'id'));
$this->saveOurCursor($maxOurId);
$hasErrors = ! empty(array_filter($pushReport, fn ($l) => str_contains((string) $l, '✗')));
if (! empty($ourChanges) && ! $hasErrors) {
$ourDatetimes = array_filter(array_column($ourChanges, 'datetime'));
$maxOurDatetime = \Carbon\Carbon::parse(max($ourDatetimes))->format('Y-m-d H:i:s');
if ($ourLastSync === null || $maxOurDatetime > $ourLastSync) {
$this->saveOurCursor($maxOurDatetime);
}
} elseif ($hasErrors) {
$report[] = 'هشدار: cursor پیش نرفت — برخی تغییرات روی سیستم مقابل اعمال نشد و دفعه بعد retry می‌شه.';
}
if ($this->isFtpConfigured()) {
@@ -431,7 +452,7 @@ private function applyChange(array $change, string $prefix, array &$report): voi
}
}
private function saveOurCursor(int $cursor): void
private function saveOurCursor(string $cursor): void
{
\App\Models\OsurgInitial::updateOrCreate(
['init_parameter' => 'our_sync_cursor'],
@@ -440,7 +461,7 @@ private function saveOurCursor(int $cursor): void
cache()->forget('osurg_initial.our_sync_cursor');
}
private function savePeerCursor(int $cursor): void
private function savePeerCursor(string $cursor): void
{
\App\Models\OsurgInitial::updateOrCreate(
['init_parameter' => 'sync_peer_cursor'],