bound(\Illuminate\Contracts\Queue\Job::class)) { try { $currentJobId = resolve(\Illuminate\Contracts\Queue\Job::class)->getJobId(); } catch (\Throwable $e) { // Fallback if resolution fails despite being bound } } // Reset registry if this is a new job if (self::$lastJobId !== $currentJobId) { self::$isSynced = []; self::$lastJobId = $currentJobId; } $changedFields = $context['changed_fields'] ?? []; $isNewRecord = $context['is_new_record'] ?? false; // Collect all test package numbers that need to be synced $testPackageNosToSync = []; // Current test package $currentTestPackageNo = $data->test_package_no; if (!empty($currentTestPackageNo)) { // Check if already synced in this process if (!isset(self::$isSynced[$currentTestPackageNo])) { $testPackageNosToSync[] = $currentTestPackageNo; } } // If test_package_no changed, also sync the old test package if (in_array('test_package_no', $changedFields) && $beforeData && !empty($beforeData->test_package_no)) { $oldTestPackageNo = $beforeData->test_package_no; if ($oldTestPackageNo !== $currentTestPackageNo && !in_array($oldTestPackageNo, $testPackageNosToSync)) { if (!isset(self::$isSynced[$oldTestPackageNo])) { $testPackageNosToSync[] = $oldTestPackageNo; } Log::info("TestPackSync: test_package_no changed, will sync both old and new", [ 'old_test_package_no' => $oldTestPackageNo, 'new_test_package_no' => $currentTestPackageNo, 'weld_log_id' => $data->id ]); } } // If no test packages to sync, return early if (empty($testPackageNosToSync)) { Log::info("TestPackSync: No test packages to sync", [ 'weld_log_id' => $data->id, 'test_package_no' => $currentTestPackageNo ]); return [ 'success' => true, 'synced' => 0, 'reason' => 'no_test_package' ]; } $syncedCount = 0; $errors = []; // Sync each test package foreach ($testPackageNosToSync as $testPackageNo) { try { // Mark as synced before calling the view to prevent re-entry/collision self::$isSynced[$testPackageNo] = true; // Call the sync view with specific test_package_no $result = view('cron.weld_logs-sync-from-weldlog-to-test-pack', [ 'test_package_no' => $testPackageNo ])->render(); Log::info("TestPackSync: Sync completed for test package", [ 'test_package_no' => $testPackageNo, 'weld_log_id' => $data->id ]); $syncedCount++; } catch (\Throwable $th) { Log::error("TestPackSync: Sync failed for test package (non-critical)", [ 'test_package_no' => $testPackageNo, 'weld_log_id' => $data->id, 'error' => $th->getMessage(), 'file' => $th->getFile(), 'line' => $th->getLine() ]); $errors[] = [ 'test_package_no' => $testPackageNo, 'error' => $th->getMessage() ]; } } return [ 'success' => empty($errors), 'synced' => $syncedCount, 'test_packages_synced' => $testPackageNosToSync, 'errors' => $errors ]; } }