From e19c00bf0e665f83ce8aed8640d0479f835e89c2 Mon Sep 17 00:00:00 2001 From: MyHomeMyData Date: Mon, 18 Dec 2023 18:07:17 +0100 Subject: [PATCH] Simplyfied stop sequence for workers, try .. catch for stop sequences. => V0.1.2 --- io-package.json | 2 +- lib/canCollect.js | 29 ++++++++++++------------ lib/canUds.js | 57 ++++++++++++++++++++++++++--------------------- 3 files changed, 47 insertions(+), 41 deletions(-) diff --git a/io-package.json b/io-package.json index 36528f6..767a24c 100644 --- a/io-package.json +++ b/io-package.json @@ -1,7 +1,7 @@ { "common": { "name": "e3oncan", - "version": "0.1.1", + "version": "0.1.2", "news": { "0.0.1": { "en": "initial release", diff --git a/lib/canCollect.js b/lib/canCollect.js index b4bdeec..6258679 100644 --- a/lib/canCollect.js +++ b/lib/canCollect.js @@ -50,23 +50,22 @@ class collect { } async stop(ctx) { - // Stop Timeout: - if (this.timeoutHandle) await clearTimeout(this.timeoutHandle); - this.timeoutHandle = null; + try { + await this.storage.setOpMode('standby'); - // Wait till possibly running communication has finished: - const tsAbort = await new Date().getTime() + this.config.timeout; - while ( (this.commBusy) && (new Date().getTime() < tsAbort) ) { - await this.sleep(50); - } + // Stop Timeout: + if (this.timeoutHandle) await clearTimeout(this.timeoutHandle); + this.timeoutHandle = null; - this.stat.state = 'stopped'; - await this.storage.storeStatistics(ctx, this); + this.stat.state = 'stopped'; + await this.storage.storeStatistics(ctx, this); - // Stop worker: - this.data.collecting = false; - await this.storage.setOpMode('standby'); - await ctx.log.debug('Collect worker stopped on '+this.config.stateBase); + // Stop worker: + this.data.collecting = false; + await ctx.log.debug('Collect worker stopped on '+this.config.stateBase); + } catch (e) { + // Do nothing + } } sleep(milliseconds) { @@ -75,6 +74,8 @@ class collect { async msgCollect(ctx, msg) { + if (await this.storage.getOpMode() == 'standby') return; // No communication in mode 'standby' + if (this.commBusy) { this.stat.cntTooBusy += 1; if (this.stat.cntTooBusy == 1) ctx.log.warn('Collect: Evaluation of messages overloaded on '+String(this.config.stateBase)); diff --git a/lib/canUds.js b/lib/canUds.js index 0d937c3..c8973af 100644 --- a/lib/canUds.js +++ b/lib/canUds.js @@ -22,8 +22,12 @@ class scheduleLoop { } async stopSchedule() { - if (this.schedHandle) await clearInterval(this.schedHandle); - await this.ctxGlobal.log.silly('UDS schedule stopped: '+String(this.schedule)+' '+this.ctxLocal.canIDhex+'.'+JSON.stringify(this.dids)); + try { + if (this.schedHandle) await clearInterval(this.schedHandle); + await this.ctxGlobal.log.silly('UDS schedule stopped: '+String(this.schedule)+' '+this.ctxLocal.canIDhex+'.'+JSON.stringify(this.dids)); + } catch (e) { + // Dod nothing + } } async loop() { @@ -160,34 +164,35 @@ class uds { } async stop(ctx) { - if (await this.storage.getOpMode() != 'udsDevScan') await ctx.unRegisterUdsOnStateChange(this.userDidsToReadId); - // Stop loops: - for (const sched of Object.values(this.schedules)) { - await sched.stopSchedule(); - } - if (this.cmndsHandle) await clearInterval(this.cmndsHandle); + try { + if (await this.storage.getOpMode() != 'udsDevScan') await ctx.unRegisterUdsOnStateChange(this.userDidsToReadId); - // Stop Timeout: - if (this.timeoutHandle) await clearTimeout(this.timeoutHandle); - this.timeoutHandle = null; + await this.storage.setOpMode('standby'); - // Wait till possibly running communication has finished: - const tsAbort = await new Date().getTime() + this.config.timeout; - while ( (this.busy) && (new Date().getTime() < tsAbort) ) { - await this.sleep(50); - } + // Stop loops: + for (const sched of Object.values(this.schedules)) { + await sched.stopSchedule(); + } + if (this.cmndsHandle) await clearInterval(this.cmndsHandle); - this.stat.state = 'stopped'; - await this.storage.storeStatistics(ctx, this); + // Stop Timeout: + if (this.timeoutHandle) await clearTimeout(this.timeoutHandle); + this.timeoutHandle = null; - // Stop worker: - const opMode = await this.storage.getOpMode(); - this.callback = null; - await this.storage.setOpMode('standby'); - if (opMode == 'normal') { - await ctx.log.debug('UDS worker stopped on '+this.config.stateBase); - } else { - await ctx.log.silly('UDS worker stopped in mode '+opMode+' on '+this.config.stateBase); + this.stat.state = 'stopped'; + await this.storage.storeStatistics(ctx, this); + + // Stop worker: + const opMode = await this.storage.getOpMode(); + this.callback = null; + await this.storage.setOpMode('standby'); + if (opMode == 'normal') { + await ctx.log.debug('UDS worker stopped on '+this.config.stateBase); + } else { + await ctx.log.silly('UDS worker stopped in mode '+opMode+' on '+this.config.stateBase); + } + } catch (e) { + // Do nothing } }