Skip to content

Commit

Permalink
fixed problem that cb is never called on mh enable/disable (#2557)
Browse files Browse the repository at this point in the history
* fixed problem that cb is never called on mh enable/disable

* fix type
  • Loading branch information
foxriver76 authored Dec 16, 2023
1 parent 884f2ed commit 91a2303
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 48 deletions.
47 changes: 21 additions & 26 deletions packages/cli/src/lib/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2488,11 +2488,11 @@ async function processCommand(
null,
(err, res) => {
if (!err && res?.rows.length) {
for (let i = 0; i < res.rows.length; i++) {
const parts = res.rows[i]!.id.split('.');
for (const row of res.rows) {
const parts = row.id.split('.');
// ignore system.host.name.alive and so on
if (parts.length === 3) {
states.pushMessage(res.rows[i]!.id, {
states.pushMessage(row.id, {
command: 'checkLogging',
message: null,
from: 'console'
Expand Down Expand Up @@ -2641,7 +2641,8 @@ async function processCommand(
});

if (cmd === 's' || cmd === 'status') {
mh.status(() => void callback(EXIT_CODES.CANNOT_CREATE_USER_OR_GROUP));
mh.status();
return void callback();
} else if (cmd === 'b' || cmd === 'browse') {
mh.browse((err: any, list: any) => {
if (err) {
Expand All @@ -2653,39 +2654,33 @@ async function processCommand(
}
});
} else if (cmd === 'e' || cmd === 'enable') {
mh.enable(true, (err: any) => {
mh.enable(true, async (err: any) => {
if (err) {
console.error(err);
return void callback(EXIT_CODES.CANNOT_ENABLE_MULTIHOST);
} else {
states.pushMessage(
`system.host.${tools.getHostName()}`,
{
command: 'updateMultihost',
message: null,
from: 'setup'
},
// @ts-expect-error todo formally we should only call processExit with an exit code
callback
);
await states.pushMessage(`system.host.${tools.getHostName()}`, {
command: 'updateMultihost',
message: null,
from: 'setup'
});

callback();
}
});
} else if (cmd === 'd' || cmd === 'disable') {
mh.enable(false, (err: any) => {
mh.enable(false, async (err: any) => {
if (err) {
console.error(err);
return void callback(EXIT_CODES.CANNOT_ENABLE_MULTIHOST);
} else {
states.pushMessage(
`system.host.${tools.getHostName()}`,
{
command: 'updateMultihost',
message: null,
from: 'setup'
},
// @ts-expect-error todo formally we should only call processExit with an exit code
callback
);
await states.pushMessage(`system.host.${tools.getHostName()}`, {
command: 'updateMultihost',
message: null,
from: 'setup'
});

callback();
}
});
} else if (cmd === 'c' || cmd === 'connect') {
Expand Down
12 changes: 6 additions & 6 deletions packages/cli/src/lib/setup/multihostClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ interface ReceivedMessage {
slave?: boolean;
auth?: string;
salt?: string;
/** The states config of iobroker.json */
states?: Record<string, any>;
/** The objects config of iobroker.json */
objects?: Record<string, any>;
/** The states config of ioBroker.json */
states?: ioBroker.StatesDatabaseOptions;
/** The objects config of ioBroker.json */
objects?: ioBroker.ObjectsDatabaseOptions;
}

export type BrowseResultEntry = Partial<ReceivedMessage>;
Expand Down Expand Up @@ -192,8 +192,8 @@ export class MHClient {
password: string,
callback: (
err: Error | undefined,
objectsConfig?: Record<string, any>,
statesConfig?: Record<string, any>,
objectsConfig?: ioBroker.ObjectsDatabaseOptions,
statesConfig?: ioBroker.StatesDatabaseOptions,
address?: string
) => void
): void {
Expand Down
26 changes: 13 additions & 13 deletions packages/cli/src/lib/setup/setupMultihost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class Multihost {
/**
* Retrive config (iobroker.json content)
*/
getConfig(): Record<string, any> {
getConfig(): ioBroker.IoBrokerJson {
let config;
// read actual configuration
try {
Expand Down Expand Up @@ -92,9 +92,8 @@ export class Multihost {
*
* @param config iob config
* @param changed if config has changed
* @param callback
*/
private showMHState(config: Record<string, any>, changed: boolean, callback: () => void): void {
private showMHState(config: ioBroker.IoBrokerJson, changed: boolean): void {
if (config.multihostService.enabled) {
let warningShown = false;
if (dbTools.isLocalObjectsDbServer(config.objects.type, config.objects.host, true)) {
Expand Down Expand Up @@ -144,7 +143,6 @@ export class Multihost {
);
console.log(`Objects: ${config.objects.type} on ${config.objects.host}`);
console.log(`States: ${config.states.type} on ${config.states.host}`);
callback();
}

/**
Expand Down Expand Up @@ -217,7 +215,7 @@ export class Multihost {
prompt.start();

prompt.get(schema, (err, password) => {
if (password && password.password) {
if (password?.password) {
if (password.password !== password.passwordRepeat) {
callback(new Error('Secret phrases are not equal!'));
} else {
Expand All @@ -226,29 +224,31 @@ export class Multihost {
obj!.native.secret,
password.password as string
);
this.showMHState(config, changed, callback);
this.showMHState(config, changed);
callback();
});
}
} else {
callback(new Error('No secret phrase entered!'));
}
});
} else {
this.showMHState(config, changed, callback);
this.showMHState(config, changed);
callback();
}
} else {
this.showMHState(config, changed, callback);
this.showMHState(config, changed);
callback();
}
}

/**
* Show the MH status
* @param callback
*/
status(callback: () => void): void {
status(): void {
const config = this.getConfig();
config.multihostService = config.multihostService || { enabled: false, secure: true };
this.showMHState(config, false, callback);
this.showMHState(config, false);
}

/**
Expand Down Expand Up @@ -314,11 +314,11 @@ export class Multihost {
} else {
if (tools.isListenAllAddress(config.states.host)) {
// TODO: why we set the remote IP only when the local config allows full connectivity?
config.states.host = ipHost;
config.states.host = ipHost ?? '';
}
if (tools.isListenAllAddress(config.objects.host)) {
// TODO: why we set the remote IP only when the local config allows full connectivity?
config.objects.host = ipHost;
config.objects.host = ipHost ?? '';
}

fs.writeFileSync(this.configName, JSON.stringify(config, null, 2));
Expand Down
5 changes: 3 additions & 2 deletions packages/types-dev/config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ interface JsonlOptions {
};
}

interface DatabaseOptions {
export interface DatabaseOptions {
/** Possible values: 'file' - [port 9001], 'jsonl' - [port 9001], 'redis' - [port 6379 or 26379 for sentinel]. */
type: 'jsonl' | 'file' | 'redis';
'// type': string;
Expand All @@ -66,7 +66,7 @@ interface DatabaseOptions {
jsonlOptions: JsonlOptions;
}

interface ObjectsDatabaseOptions extends DatabaseOptions {
export interface ObjectsDatabaseOptions extends DatabaseOptions {
noFileCache: boolean;
maxQueue: number;
}
Expand Down Expand Up @@ -105,6 +105,7 @@ export interface IoBJson {
enabled: boolean;
secure: boolean;
password: string;
persist: boolean;
};
objects: ObjectsDatabaseOptions;
states: DatabaseOptions;
Expand Down
7 changes: 6 additions & 1 deletion packages/types-dev/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import type * as fs from 'fs';
import './objects';
import type { IoBJson } from './config';
import type { IoBJson, DatabaseOptions, ObjectsDatabaseOptions as ObjectsDbOptions } from './config';

export {}; // avoids exporting AtLeastOne into the global scope

Expand Down Expand Up @@ -511,5 +511,10 @@ declare global {
* The ioBroker global config
*/
type IoBrokerJson = IoBJson;

/** Objects DB options from ioBroker.json */
type ObjectsDatabaseOptions = ObjectsDbOptions;
/** States DB options from ioBroker.json */
type StatesDatabaseOptions = DatabaseOptions;
} // end namespace ioBroker
} // end declare global

0 comments on commit 91a2303

Please sign in to comment.