Skip to content

Commit

Permalink
Fix coordinator cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
longzheng committed Jan 27, 2025
1 parent dc3352f commit 844dde7
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/coordinator/helpers/inverterController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ export class InverterController {
private secondsToSample: number;
private intervalSeconds: number;

private controlLimitsLoopTimer: NodeJS.Timeout | null = null;
private applyControlLoopTimer: NodeJS.Timeout | null = null;

constructor({
config,
limiters,
Expand Down Expand Up @@ -196,7 +199,7 @@ export class InverterController {
// update at most every 1 second
const delay = Math.max(1000 - duration, 0);

setTimeout(() => {
this.controlLimitsLoopTimer = setTimeout(() => {
void this.updateControlLimitsLoop();
}, delay);
}
Expand Down Expand Up @@ -235,7 +238,7 @@ export class InverterController {

const delay = Math.max(this.intervalSeconds * 1000 - duration, 0);

setTimeout(() => {
this.applyControlLoopTimer = setTimeout(() => {
void this.applyControlLoop();
}, delay);
}
Expand Down Expand Up @@ -361,6 +364,16 @@ export class InverterController {

return rampedInverterConfiguration;
}

public destroy() {
if (this.controlLimitsLoopTimer) {
clearTimeout(this.controlLimitsLoopTimer);
}

if (this.applyControlLoopTimer) {
clearTimeout(this.applyControlLoopTimer);
}
}
}

export function calculateInverterConfiguration({
Expand Down
10 changes: 10 additions & 0 deletions src/coordinator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ export function createCoordinator(): Coordinator {
logger.info('Destroying coordinator');

siteSamplePoller.destroy();
invertersPoller.destroy();
inverterController.destroy();
sep2Instance?.sep2Client.destroy();

Check failure on line 86 in src/coordinator/index.ts

View workflow job for this annotation

GitHub Actions / build

Property 'destroy' does not exist on type 'SEP2Client'.
sep2Instance?.derHelper.destroy();

Check failure on line 87 in src/coordinator/index.ts

View workflow job for this annotation

GitHub Actions / build

Property 'destroy' does not exist on type 'DerHelper'.
sep2Instance?.mirrorUsagePointListHelper.destroy();
limiters.csipAus?.destroy();

Check failure on line 89 in src/coordinator/index.ts

View workflow job for this annotation

GitHub Actions / build

Property 'destroy' does not exist on type 'LimiterType'.
limiters.fixed?.destroy();

Check failure on line 90 in src/coordinator/index.ts

View workflow job for this annotation

GitHub Actions / build

Property 'destroy' does not exist on type 'LimiterType'.
limiters.negativeFeedIn?.destroy();

Check failure on line 91 in src/coordinator/index.ts

View workflow job for this annotation

GitHub Actions / build

Property 'destroy' does not exist on type 'LimiterType'.
limiters.twoWayTariff?.destroy();

Check failure on line 92 in src/coordinator/index.ts

View workflow job for this annotation

GitHub Actions / build

Property 'destroy' does not exist on type 'LimiterType'.
limiters.mqtt?.destroy();

Check failure on line 93 in src/coordinator/index.ts

View workflow job for this annotation

GitHub Actions / build

Property 'destroy' does not exist on type 'LimiterType'.
},
};
}
8 changes: 8 additions & 0 deletions src/limiters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,11 @@ export function getLimiters({
: null,
};
}

export function destroyLimiters(limiters: Limiters): void {
Object.values(limiters).forEach((limiter) => {
if (limiter && typeof limiter.destroy === 'function') {

Check failure on line 49 in src/limiters/index.ts

View workflow job for this annotation

GitHub Actions / build

Property 'destroy' does not exist on type 'LimiterType'.
limiter.destroy();

Check failure on line 50 in src/limiters/index.ts

View workflow job for this annotation

GitHub Actions / build

Property 'destroy' does not exist on type 'LimiterType'.
}
});
}
14 changes: 14 additions & 0 deletions src/sep2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export type Sep2Instance = {
derHelper: DerHelper;
mirrorUsagePointListHelper: MirrorUsagePointListHelper;
limiter: CsipAusLimiter;
destroy: () => void;
};

export function getSep2Instance({
Expand Down Expand Up @@ -265,5 +266,18 @@ export function getSep2Instance({
derHelper,
mirrorUsagePointListHelper,
limiter,
destroy: () => {
pinoLogger.info('Destroying SEP2 instance');

timeHelper.destroy();

Check failure on line 272 in src/sep2/index.ts

View workflow job for this annotation

GitHub Actions / build

Property 'destroy' is private and only accessible within class 'TimeHelper'.
endDeviceListHelper.destroy();
registrationHelper.destroy();
derListHelper.destroy();
derHelper.destroy();
functionSetAssignmentsListHelper.destroy();
mirrorUsagePointListHelper.destroy();
limiter.destroy();
derControlsHelper.destroy();
},
};
}

0 comments on commit 844dde7

Please sign in to comment.