Skip to content

Commit

Permalink
Merge pull request #1362 from spacemeshos/fix-dont-restart-node-hourly
Browse files Browse the repository at this point in the history
Fix: don't restart node every hour
  • Loading branch information
brusherru authored Jul 14, 2023
2 parents 633811d + e583f39 commit 7733e23
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ Smapp can be started with additional arguments:
_e.g._ `./Spacemesh --test-mode`
It runs Smapp and the Node under the hood in standalone mode, making it much easier to test and debug the application.
Env variable alias: `TEST_MODE`
- `--check-interval` (number)
_e.g._ `./Spacemesh --check-interval=60` to check for updates every 60 seconds
Smapp checks every N seconds for the updates the software updates and new config.
If new config arrived — it automatically merges it with the custom User settings and
restarts the Node.
Default: `3600` seconds, or every hour

To run the application in dev mode with the same behavior set env variables instead:
```
Expand Down
26 changes: 16 additions & 10 deletions desktop/main/sources/fetchDiscovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import {
interval,
map,
Observable,
pairwise,
retry,
startWith,
Subject,
switchMap,
withLatestFrom,
Expand All @@ -23,6 +25,9 @@ import {
import { handleIPC, handlerResult, makeSubscription } from '../rx.utils';
import { fetchNodeConfig } from '../../utils';
import { Managers } from '../app.types';
import Logger from '../../logger';

const logger = Logger({ className: 'fetchDiscovery' });

export const fromNetworkConfig = (net: Network) =>
from(fetchNodeConfig(net.conf)).pipe(
Expand Down Expand Up @@ -81,22 +86,23 @@ export const listNetworksByRequest = ($networks: Subject<Network[]>) =>
(networks) => $networks.next(networks)
);

let cacheNodeConfig: NodeConfig | null = null;
export const listenNodeConfigAndRestartNode = (
$nodeConfig: Observable<NodeConfig>,
$managers: Subject<Managers>
) =>
makeSubscription(
$nodeConfig.pipe(withLatestFrom($managers)),
([nodeConfig, managers]) => {
$nodeConfig.pipe(startWith(null), pairwise(), withLatestFrom($managers)),
([[prevNodeConfig, nextNodeConfig], managers]) => {
(async () => {
if (equals(nodeConfig, cacheNodeConfig)) {
return;
}

cacheNodeConfig = nodeConfig;

if (managers.node.isNodeRunning()) {
if (
prevNodeConfig !== null &&
!equals(prevNodeConfig, nextNodeConfig) &&
managers.node.isNodeRunning()
) {
logger.log(
'listenNodeConfigAndRestartNode',
'Node config changed. Restart the Node'
);
await managers.node.restartNode();
}
})();
Expand Down
7 changes: 6 additions & 1 deletion desktop/main/startApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ const positiveNum = (def: number, n: number) => (n > 0 ? n : def);
const CHECK_UPDATES_INTERVAL =
positiveNum(
3600, // hour
parseInt(app.commandLine.getSwitchValue('checkInterval'), 10)
parseInt(
process.env.CHECK_INTERVAL ||
app.commandLine.getSwitchValue('check-interval') ||
'0',
10
)
) * 1000;

const loadNetworkData = () => {
Expand Down

0 comments on commit 7733e23

Please sign in to comment.