Skip to content

Commit

Permalink
[DDW-780] Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Main committed Oct 25, 2021
1 parent 32550a5 commit 53691ab
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 57 deletions.
26 changes: 15 additions & 11 deletions source/main/cardano/CardanoNode.js
Expand Up @@ -353,18 +353,19 @@ export class CardanoNode {
} else {
try {
const node = await CardanoWalletLauncher({
...this._config,
nodeImplementation,
nodeConfig: this._config.nodeConfig,
cluster: this._config.cluster,
stateDir: this._config.stateDir,
tlsPath: this._config.tlsPath,
configPath: this._config.configPath,
syncTolerance: this._config.syncTolerance,
nodeLogFile,
walletLogFile,
cliBin: this._config.cliBin,
isStaging: this._config.isStaging,
metadataUrl: this._config.metadataUrl,
// nodeConfig: this._config.nodeConfig,
// cluster: this._config.cluster,
// stateDir: this._config.stateDir,
// tlsPath: this._config.tlsPath,
// configPath: this._config.configPath,
// syncTolerance: this._config.syncTolerance,
// cliBin: this._config.cliBin,
// isStaging: this._config.isStaging,
// metadataUrl: this._config.metadataUrl,
});

this._node = node;
Expand Down Expand Up @@ -537,8 +538,11 @@ export class CardanoNode {
}
} catch (error) {
_log.error('CardanoNode#restart: Could not restart cardano-node', error);
const { code, signal } = error || {};
await this._handleCardanoNodeError(code, signal);
// const { code, signal } = error || {};
// await this._handleCardanoNodeError(code, signal);
if (this._state !== CardanoNodeStates.UNRECOVERABLE) {
this._changeToState(CardanoNodeStates.ERRORED); // TODO: What is the intention here?
}
return Promise.reject(error);
}
}
Expand Down
45 changes: 1 addition & 44 deletions source/main/index.js
Expand Up @@ -35,7 +35,6 @@ import { getStateDirectoryPathChannel } from './ipc/getStateDirectoryPathChannel
import { getDesktopDirectoryPathChannel } from './ipc/getDesktopDirectoryPathChannel';
import { getSystemLocaleChannel } from './ipc/getSystemLocaleChannel';
import { CardanoNodeStates } from '../common/types/cardano-node.types';
import type { CheckDiskSpaceResponse } from '../common/types/no-disk-space.types';
import type {
GenerateWalletMigrationReportRendererRequest,
SetStateSnapshotLogMainResponse,
Expand All @@ -55,7 +54,6 @@ import {
// Global references to windows to prevent them from being garbage collected
let mainWindow: BrowserWindow;
let cardanoNode: ?CardanoNode;
let hadNotEnoughSpaceLeft: boolean = false;

const {
isDev,
Expand Down Expand Up @@ -164,48 +162,7 @@ const onAppReady = async () => {

cardanoNode = setupCardanoNode(launcherConfig, mainWindow);

const onCheckDiskSpace = async ({
isNotEnoughDiskSpace,
}: CheckDiskSpaceResponse) => {
if (cardanoNode) {
if (isNotEnoughDiskSpace) {
hadNotEnoughSpaceLeft = true;
if (
cardanoNode.state !== CardanoNodeStates.STOPPING &&
cardanoNode.state !== CardanoNodeStates.STOPPED
) {
try {
logger.info('[DISK-SPACE-DEBUG] Stopping cardano node');
await cardanoNode.stop();
} catch (error) {
logger.error('[DISK-SPACE-DEBUG] Cannot stop cardano node', error);
}
}
} else {
if (
// Happens after the user made more disk space
cardanoNode.state !== CardanoNodeStates.STARTING &&
cardanoNode.state !== CardanoNodeStates.RUNNING &&
hadNotEnoughSpaceLeft
) {
try {
logger.info(
'[DISK-SPACE-DEBUG] restart cardano node after freeing ug disk space'
);
if (cardanoNode._startupTries > 0) await cardanoNode.restart();
else await cardanoNode.start();
} catch (error) {
logger.error(
'[DISK-SPACE-DEBUG] Daedalus tried to restart, but failed',
error
);
}
}
hadNotEnoughSpaceLeft = false;
}
}
};
const handleCheckDiskSpace = handleDiskSpace(mainWindow, onCheckDiskSpace);
const handleCheckDiskSpace = handleDiskSpace(mainWindow, cardanoNode);
const onMainError = (error: string) => {
if (error.indexOf('ENOSPC') > -1) {
handleCheckDiskSpace();
Expand Down
42 changes: 40 additions & 2 deletions source/main/utils/handleDiskSpace.js
Expand Up @@ -13,10 +13,48 @@ import {
DISK_SPACE_RECOMMENDED_PERCENTAGE,
stateDirectoryPath,
} from '../config';
import { CardanoNodeStates } from '../../common/types/cardano-node.types';
import { CardanoNode } from '../cardano/CardanoNode';

const startStopCardanoNode = async (
cardanoNode: CardanoNode,
isNotEnoughDiskSpace: boolean
) => {
if (isNotEnoughDiskSpace) {
if (
cardanoNode.state !== CardanoNodeStates.STOPPING &&
cardanoNode.state !== CardanoNodeStates.STOPPED
) {
try {
logger.info('[DISK-SPACE-DEBUG] Stopping cardano node');
await cardanoNode.stop();
} catch (error) {
logger.error('[DISK-SPACE-DEBUG] Cannot stop cardano node', error);
}
}
} else if (
// Happens after the user made more disk space
cardanoNode.state !== CardanoNodeStates.STARTING &&
cardanoNode.state !== CardanoNodeStates.RUNNING
) {
try {
logger.info(
'[DISK-SPACE-DEBUG] restart cardano node after freeing up disk space'
);
if (cardanoNode._startupTries > 0) await cardanoNode.restart();
else await cardanoNode.start();
} catch (error) {
logger.error(
'[DISK-SPACE-DEBUG] Daedalus tried to restart, but failed',
error
);
}
}
};

export const handleDiskSpace = (
mainWindow: BrowserWindow,
onCheckDiskSpace: Function
cardanoNode: CardanoNode
) => {
let diskSpaceCheckInterval;
let diskSpaceCheckIntervalLength = DISK_SPACE_CHECK_LONG_INTERVAL; // Default check interval
Expand Down Expand Up @@ -74,7 +112,7 @@ export const handleDiskSpace = (
};
if (isNotEnoughDiskSpace)
logger.info('Not enough disk space', { response });
await onCheckDiskSpace?.(response);
await startStopCardanoNode(cardanoNode, isNotEnoughDiskSpace);
await getDiskSpaceStatusChannel.send(response, mainWindow.webContents);
return response;
} catch (error) {
Expand Down

0 comments on commit 53691ab

Please sign in to comment.