Skip to content

Commit

Permalink
Simplyfied stop sequence for workers, try .. catch for stop sequences…
Browse files Browse the repository at this point in the history
…. => V0.1.2
  • Loading branch information
MyHomeMyData committed Dec 18, 2023
1 parent 72ce952 commit e19c00b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 41 deletions.
2 changes: 1 addition & 1 deletion io-package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"common": {
"name": "e3oncan",
"version": "0.1.1",
"version": "0.1.2",
"news": {
"0.0.1": {
"en": "initial release",
Expand Down
29 changes: 15 additions & 14 deletions lib/canCollect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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));
Expand Down
57 changes: 31 additions & 26 deletions lib/canUds.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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
}
}

Expand Down

0 comments on commit e19c00b

Please sign in to comment.