Skip to content

Commit

Permalink
Avoid recreation of containers
Browse files Browse the repository at this point in the history
  • Loading branch information
dappnodedev committed Sep 23, 2024
1 parent 8dbca12 commit c714337
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 50 deletions.
6 changes: 2 additions & 4 deletions packages/stakers/src/consensus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,8 @@ export class Consensus extends StakerComponent {

const userSettings = await this.getUserSettings(network, currentConsensusDnpName);

await this.persistSelectedIfInstalled({
dnpName: currentConsensusDnpName,
userSettings
});
await this.setStakerPkgConfig({ dnpName: currentConsensusDnpName, isInstalled, userSettings });

await this.DbHandlers[network].set(currentConsensusDnpName);
}
}
Expand Down
8 changes: 4 additions & 4 deletions packages/stakers/src/execution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ export class Execution extends StakerComponent {
return;
}

await this.persistSelectedIfInstalled({
dnpName: currentExecutionDnpName,
userSettings: this.getUserSettings(network, currentExecutionDnpName)
});
const userSettings = this.getUserSettings(network, currentExecutionDnpName);

await this.setStakerPkgConfig({ dnpName: currentExecutionDnpName, isInstalled, userSettings });

await this.DbHandlers[network].set(currentExecutionDnpName);
}
}
Expand Down
27 changes: 15 additions & 12 deletions packages/stakers/src/mevBoost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,25 @@ export class MevBoost extends StakerComponent {
}

async persistMevBoostIfInstalledAndRunning(network: Network): Promise<void> {
const currentMevBoostDnpName = MevBoost.CompatibleMevBoost[network]?.dnpName;
if (currentMevBoostDnpName) {
const isInstalledAndRunning = (
await listPackageNoThrow({
dnpName: currentMevBoostDnpName
})
)?.containers.some((container) => container.running);

if (!isInstalledAndRunning) {
const mevBoostDnpName = MevBoost.CompatibleMevBoost[network]?.dnpName;

if (mevBoostDnpName) {
const mevBoostDnp = await listPackageNoThrow({ dnpName: mevBoostDnpName });
const isRunning = mevBoostDnp?.containers.some((container) => container.running);

if (!isRunning) {
this.DbHandlers[network].set(false);
return;
}
await this.persistSelectedIfInstalled({
dnpName: currentMevBoostDnpName,
userSettings: this.getUserSettings(network, null)

const userSettings = this.getUserSettings(network, null);

await this.setStakerPkgConfig({
dnpName: mevBoostDnpName,
isInstalled: true,
userSettings
});

this.DbHandlers[network].set(true);
}
}
Expand Down
21 changes: 10 additions & 11 deletions packages/stakers/src/signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,16 @@ export class Signer extends StakerComponent {
}

async persistSignerIfInstalledAndRunning(network: Network): Promise<void> {
if (
(
await listPackageNoThrow({
dnpName: Signer.CompatibleSigners[network].dnpName
})
)?.containers.some((container) => container.running)
)
await this.persistSelectedIfInstalled({
dnpName: Signer.CompatibleSigners[network].dnpName,
userSettings: this.getUserSettings(network)
});
const signerDnpName = Signer.CompatibleSigners[network].dnpName;
const signerDnp = await listPackageNoThrow({ dnpName: signerDnpName });
const isRunning = signerDnp?.containers.some((container) => container.running);

if (isRunning) {
const dnpName = Signer.CompatibleSigners[network].dnpName;
const userSettings = this.getUserSettings(network);

await this.setStakerPkgConfig({ dnpName, isInstalled: true, userSettings });
}
}

async setNewSigner(network: Network, newWeb3signerDnpName: string | null) {
Expand Down
28 changes: 9 additions & 19 deletions packages/stakers/src/stakerComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { dockerComposeUpPackage, listPackageNoThrow, listPackages } from "@dappn
import { ComposeFileEditor } from "@dappnode/dockercompose";
import { DappnodeInstaller, packageGetData, packageInstall } from "@dappnode/installer";
import { logs } from "@dappnode/logger";
import { InstalledPackageData, UserSettingsAllDnps, StakerItem, UserSettings, Network } from "@dappnode/types";
import { InstalledPackageData, StakerItem, UserSettings, Network } from "@dappnode/types";
import { getIsInstalled, getIsUpdated, getIsRunning, fileToGatewayUrl } from "@dappnode/utils";
import { lt } from "semver";
import { isMatch } from "lodash-es";
Expand Down Expand Up @@ -53,17 +53,6 @@ export class StakerComponent {
);
}

protected async persistSelectedIfInstalled({
dnpName,
userSettings
}: {
dnpName: string;
userSettings: UserSettings;
}): Promise<void> {
logs.info(`Persisting ${dnpName}`);
await this.setStakerPkgConfig({ dnpName, isInstalled: true, userSettings });
}

protected async setNew({
newStakerDnpName,
dockerNetworkName,
Expand Down Expand Up @@ -130,7 +119,7 @@ export class StakerComponent {
* - adds the staker network to the docker-compose file
* - starts the staker pkg
*/
private async setStakerPkgConfig({
protected async setStakerPkgConfig({
dnpName,
isInstalled,
userSettings
Expand Down Expand Up @@ -161,14 +150,15 @@ export class StakerComponent {
if (userSettings) {
const composeEditor = new ComposeFileEditor(dnpName, false);

const previousSettings: UserSettingsAllDnps = {
[dnpName]: composeEditor.getUserSettings()
};
const previousSettings = composeEditor.getUserSettings();

composeEditor.applyUserSettings(userSettings, { dnpName });
const newSettings = composeEditor.getUserSettings();

if (!isMatch(previousSettings, userSettings)) {
composeEditor.applyUserSettings(userSettings, { dnpName });
if (!isMatch(previousSettings, newSettings)) {
composeEditor.write();
forceRecreate = true; // Only recreate if userSettings changed
forceRecreate = true;
logs.info(`Settings for ${dnpName} have changed. Forcing recreation of containers.`);
}
}

Expand Down

0 comments on commit c714337

Please sign in to comment.