From 5f944df6dd87e257bf776b2d9f02cd677511e038 Mon Sep 17 00:00:00 2001 From: yanguoyu <841185308@qq.com> Date: Wed, 18 Jan 2023 15:52:15 +0800 Subject: [PATCH] fix: Sometimes RPC timeout but ckb is living 1. Try to check ckb is alive every 30s. 2. When startCkb try to check the ckb is null. --- packages/neuron-wallet/src/services/ckb-runner.ts | 4 ++++ packages/neuron-wallet/src/services/monitor/base.ts | 2 +- .../neuron-wallet/tests/services/ckb-runner.test.ts | 13 ++++++++++++- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/packages/neuron-wallet/src/services/ckb-runner.ts b/packages/neuron-wallet/src/services/ckb-runner.ts index 127e4a2f74..ba13e652dc 100644 --- a/packages/neuron-wallet/src/services/ckb-runner.ts +++ b/packages/neuron-wallet/src/services/ckb-runner.ts @@ -67,6 +67,10 @@ let lastLogTime: number export const getLookingValidTargetStatus = () => isLookingValidTarget export const startCkbNode = async () => { + if (ckb !== null) { + logger.info(`CKB:\tckb is not closed, close it before start...`) + await stopCkbNode() + } await initCkb() logger.info('CKB:\tstarting node...') diff --git a/packages/neuron-wallet/src/services/monitor/base.ts b/packages/neuron-wallet/src/services/monitor/base.ts index 2857985c79..6480788edc 100644 --- a/packages/neuron-wallet/src/services/monitor/base.ts +++ b/packages/neuron-wallet/src/services/monitor/base.ts @@ -35,7 +35,7 @@ export default abstract class Monitor { } } - async startMonitor(intervalTime: number = 10000, startNow: boolean = false) { + async startMonitor(intervalTime: number = 30000, startNow: boolean = false) { this.interval = interval(intervalTime) if (!this.subcription?.closed) { this.subcription?.unsubscribe() diff --git a/packages/neuron-wallet/tests/services/ckb-runner.test.ts b/packages/neuron-wallet/tests/services/ckb-runner.test.ts index aa31c8cd00..866c3209a4 100644 --- a/packages/neuron-wallet/tests/services/ckb-runner.test.ts +++ b/packages/neuron-wallet/tests/services/ckb-runner.test.ts @@ -76,6 +76,7 @@ describe('ckb runner', () => { beforeEach(() => { resetMocks() + stubbedCkb.kill = jest.fn() stubbedCkb.stderr = new EventEmitter() stubbedCkb.stdout = new EventEmitter() stubbedSpawn.mockReturnValue(stubbedCkb) @@ -95,7 +96,12 @@ describe('ckb runner', () => { stubbedExistsSync.mockReturnValue(true) await startCkbNode() }) - it('should not init ckb config', () => { + afterEach(async () => { + const promise = stopCkbNode() + stubbedCkb.emit('close') + await promise + }) + it('should not init ckb config', async () => { expect(stubbedSpawn).not.toHaveBeenCalledWith(expect.stringContaining('/ckb'), [ 'init', '--chain', @@ -124,6 +130,11 @@ describe('ckb runner', () => { stubbedCkb.emit('close') await promise }) + afterEach(async () => { + const stopPromise = stopCkbNode() + stubbedCkb.emit('close') + await stopPromise + }) it('inits ckb config', () => { expect(stubbedSpawn).toHaveBeenCalledWith(expect.stringContaining(path.join(platformPath, 'ckb')), [ 'init',