Reduce false local node start failures
This commit is contained in:
+23
-6
@@ -147,22 +147,39 @@ class _PeyaAppState extends ConsumerState<PeyaApp> {
|
||||
return;
|
||||
}
|
||||
}
|
||||
final attempts = config.nodeConfig.mode == NodeMode.local ? 5 : 1;
|
||||
final connected = await _connectAndSyncWithRetry(
|
||||
config,
|
||||
attempts: config.nodeConfig.mode == NodeMode.local ? 10 : 1,
|
||||
);
|
||||
if (!connected) {
|
||||
ref.read(loggerProvider).w('Failed to connect node after retries.');
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> _connectAndSyncWithRetry(
|
||||
AppConfig config, {
|
||||
required int attempts,
|
||||
}) async {
|
||||
for (var attempt = 0; attempt < attempts; attempt++) {
|
||||
try {
|
||||
await ref
|
||||
.read(walletControllerProvider.notifier)
|
||||
.connectNode(config.nodeConfig);
|
||||
} catch (_) {}
|
||||
final connected = await ref.read(walletRepositoryProvider).isConnected();
|
||||
if (connected) {
|
||||
await ref.read(walletControllerProvider.notifier).syncNow();
|
||||
return;
|
||||
} catch (error) {
|
||||
if (attempt + 1 >= attempts) {
|
||||
ref.read(loggerProvider).w('Failed to connect node: $error');
|
||||
return;
|
||||
final syncedState = ref.read(walletControllerProvider);
|
||||
if (syncedState.syncStatus.lastSync != null ||
|
||||
await ref.read(walletRepositoryProvider).isConnected()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (attempt + 1 < attempts) {
|
||||
await Future.delayed(const Duration(seconds: 1));
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
Future<void> _applyStartMinimized(AppConfig config) async {
|
||||
|
||||
@@ -70,7 +70,20 @@ class WindowsLocalNodeService implements LocalNodeService {
|
||||
}
|
||||
_startFuture = _startNodeInternal(config);
|
||||
try {
|
||||
return await _startFuture!;
|
||||
final started = await _startFuture!;
|
||||
if (started) {
|
||||
return true;
|
||||
}
|
||||
final eventuallyAvailable = await _waitForRpc(
|
||||
config,
|
||||
attempts: 8,
|
||||
logOnFailure: false,
|
||||
);
|
||||
if (eventuallyAvailable) {
|
||||
_logger.i('Local node RPC became available after delayed startup.');
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
} finally {
|
||||
_startFuture = null;
|
||||
}
|
||||
@@ -159,15 +172,20 @@ class WindowsLocalNodeService implements LocalNodeService {
|
||||
return result is int ? result : null;
|
||||
}
|
||||
|
||||
Future<bool> _waitForRpc(LocalNodeConfig config) async {
|
||||
const attempts = 20;
|
||||
Future<bool> _waitForRpc(
|
||||
LocalNodeConfig config, {
|
||||
int attempts = 20,
|
||||
bool logOnFailure = true,
|
||||
}) async {
|
||||
for (var i = 0; i < attempts; i++) {
|
||||
if (await _isRpcAvailable(config)) {
|
||||
return true;
|
||||
}
|
||||
await Future.delayed(const Duration(seconds: 1));
|
||||
}
|
||||
_logger.w('Local node did not become available after ${attempts}s.');
|
||||
if (logOnFailure) {
|
||||
_logger.w('Local node did not become available after ${attempts}s.');
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -391,17 +391,24 @@ class _LocalNodeControlsState extends ConsumerState<_LocalNodeControls> {
|
||||
final nodeConfig = config.nodeConfig;
|
||||
final walletState = ref.read(walletControllerProvider);
|
||||
if (walletState.walletInfo != null && nodeConfig.mode == NodeMode.local) {
|
||||
for (var attempt = 0; attempt < 5; attempt++) {
|
||||
for (var attempt = 0; attempt < 10; attempt++) {
|
||||
try {
|
||||
await ref.read(walletControllerProvider.notifier).connectNode(nodeConfig);
|
||||
await ref.read(walletControllerProvider.notifier).syncNow();
|
||||
break;
|
||||
} catch (_) {
|
||||
if (attempt == 4) {
|
||||
}
|
||||
final connected = await ref.read(walletRepositoryProvider).isConnected();
|
||||
if (connected) {
|
||||
await ref.read(walletControllerProvider.notifier).syncNow();
|
||||
final state = ref.read(walletControllerProvider);
|
||||
if (state.syncStatus.lastSync != null ||
|
||||
await ref.read(walletRepositoryProvider).isConnected()) {
|
||||
break;
|
||||
}
|
||||
await Future.delayed(const Duration(seconds: 1));
|
||||
}
|
||||
if (attempt == 9) {
|
||||
break;
|
||||
}
|
||||
await Future.delayed(const Duration(seconds: 1));
|
||||
}
|
||||
}
|
||||
return ok;
|
||||
|
||||
Reference in New Issue
Block a user