workaround: stop discoveries before install/uninstall boards/libs (#674)

This commit is contained in:
Francesco Stasi 2021-12-10 17:03:24 +01:00 committed by GitHub
parent 49d12d99ff
commit a8ae0bb4e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 70 additions and 18 deletions

View File

@ -60,11 +60,29 @@ export class BoardDiscovery extends CoreClientAware {
this.startBoardListWatch(coreClient); this.startBoardListWatch(coreClient);
} }
stopBoardListWatch(coreClient: CoreClientProvider.Client): Promise<void> {
return new Promise((resolve, reject) => {
if (!this.boardWatchDuplex) {
return resolve();
}
const { instance } = coreClient;
const req = new BoardListWatchRequest();
req.setInstance(instance);
try {
this.boardWatchDuplex.write(req.setInterrupt(true), resolve);
} catch (e) {
this.discoveryLogger.error(e);
resolve();
}
});
}
startBoardListWatch(coreClient: CoreClientProvider.Client): void { startBoardListWatch(coreClient: CoreClientProvider.Client): void {
if (this.watching) { if (this.watching) {
// We want to avoid starting the board list watch process multiple // We want to avoid starting the board list watch process multiple
// times to meet unforseen consequences // times to meet unforseen consequences
return return;
} }
this.watching = true; this.watching = true;
const { client, instance } = coreClient; const { client, instance } = coreClient;
@ -73,9 +91,19 @@ export class BoardDiscovery extends CoreClientAware {
this.boardWatchDuplex = client.boardListWatch(); this.boardWatchDuplex = client.boardListWatch();
this.boardWatchDuplex.on('end', () => { this.boardWatchDuplex.on('end', () => {
this.watching = false; this.watching = false;
console.info('board watch ended') console.info('board watch ended');
}) });
this.boardWatchDuplex.on('close', () => {
this.watching = false;
console.info('board watch ended');
});
this.boardWatchDuplex.on('data', (resp: BoardListWatchResponse) => { this.boardWatchDuplex.on('data', (resp: BoardListWatchResponse) => {
if (resp.getEventType() === 'quit') {
this.watching = false;
console.info('board watch ended');
return;
}
const detectedPort = resp.getPort(); const detectedPort = resp.getPort();
if (detectedPort) { if (detectedPort) {
let eventType: 'add' | 'remove' | 'unknown' = 'unknown'; let eventType: 'add' | 'remove' | 'unknown' = 'unknown';
@ -96,7 +124,7 @@ export class BoardDiscovery extends CoreClientAware {
const address = (detectedPort as any).getPort().getAddress(); const address = (detectedPort as any).getPort().getAddress();
const protocol = (detectedPort as any).getPort().getProtocol(); const protocol = (detectedPort as any).getPort().getProtocol();
const label = (detectedPort as any).getPort().getLabel();; const label = (detectedPort as any).getPort().getLabel();
const port = { address, protocol, label }; const port = { address, protocol, label };
const boards: Board[] = []; const boards: Board[] = [];
for (const item of detectedPort.getMatchingBoardsList()) { for (const item of detectedPort.getMatchingBoardsList()) {
@ -111,7 +139,9 @@ export class BoardDiscovery extends CoreClientAware {
if (newState[port.address]) { if (newState[port.address]) {
const [, knownBoards] = newState[port.address]; const [, knownBoards] = newState[port.address];
console.warn( console.warn(
`Port '${port.address}' was already available. Known boards before override: ${JSON.stringify( `Port '${
port.address
}' was already available. Known boards before override: ${JSON.stringify(
knownBoards knownBoards
)}` )}`
); );

View File

@ -45,7 +45,8 @@ import { InstallWithProgress } from './grpc-installable';
@injectable() @injectable()
export class BoardsServiceImpl export class BoardsServiceImpl
extends CoreClientAware extends CoreClientAware
implements BoardsService { implements BoardsService
{
@inject(ILogger) @inject(ILogger)
protected logger: ILogger; protected logger: ILogger;
@ -247,7 +248,10 @@ export class BoardsServiceImpl
return boards; return boards;
} }
async getBoardUserFields(options: { fqbn: string, protocol: string }): Promise<BoardUserField[]> { async getBoardUserFields(options: {
fqbn: string;
protocol: string;
}): Promise<BoardUserField[]> {
await this.coreClientProvider.initialized; await this.coreClientProvider.initialized;
const coreClient = await this.coreClient(); const coreClient = await this.coreClient();
const { client, instance } = coreClient; const { client, instance } = coreClient;
@ -257,25 +261,23 @@ export class BoardsServiceImpl
supportedUserFieldsReq.setFqbn(options.fqbn); supportedUserFieldsReq.setFqbn(options.fqbn);
supportedUserFieldsReq.setProtocol(options.protocol); supportedUserFieldsReq.setProtocol(options.protocol);
const supportedUserFieldsResp = await new Promise<SupportedUserFieldsResponse>( const supportedUserFieldsResp =
(resolve, reject) => { await new Promise<SupportedUserFieldsResponse>((resolve, reject) => {
client.supportedUserFields(supportedUserFieldsReq, (err, resp) => { client.supportedUserFields(supportedUserFieldsReq, (err, resp) => {
(!!err ? reject : resolve)(!!err ? err : resp) (!!err ? reject : resolve)(!!err ? err : resp);
}) });
} });
); return supportedUserFieldsResp.getUserFieldsList().map((e) => {
return supportedUserFieldsResp.getUserFieldsList().map(e => {
return { return {
toolId: e.getToolId(), toolId: e.getToolId(),
name: e.getName(), name: e.getName(),
label: e.getLabel(), label: e.getLabel(),
secret: e.getSecret(), secret: e.getSecret(),
value: "", value: '',
}; };
}); });
} }
async search(options: { query?: string }): Promise<BoardsPackage[]> { async search(options: { query?: string }): Promise<BoardsPackage[]> {
await this.coreClientProvider.initialized; await this.coreClientProvider.initialized;
const coreClient = await this.coreClient(); const coreClient = await this.coreClient();
@ -408,6 +410,10 @@ export class BoardsServiceImpl
req.setVersion(version); req.setVersion(version);
console.info('>>> Starting boards package installation...', item); console.info('>>> Starting boards package installation...', item);
// stop the board discovery
await this.boardDiscovery.stopBoardListWatch(coreClient);
const resp = client.platformInstall(req); const resp = client.platformInstall(req);
resp.on( resp.on(
'data', 'data',
@ -418,7 +424,7 @@ export class BoardsServiceImpl
); );
await new Promise<void>((resolve, reject) => { await new Promise<void>((resolve, reject) => {
resp.on('end', () => { resp.on('end', () => {
this.boardDiscovery.startBoardListWatch(coreClient) this.boardDiscovery.startBoardListWatch(coreClient);
resolve(); resolve();
}); });
resp.on('error', (error) => { resp.on('error', (error) => {
@ -456,6 +462,10 @@ export class BoardsServiceImpl
req.setPlatformPackage(platform); req.setPlatformPackage(platform);
console.info('>>> Starting boards package uninstallation...', item); console.info('>>> Starting boards package uninstallation...', item);
// stop the board discovery
await this.boardDiscovery.stopBoardListWatch(coreClient);
const resp = client.platformUninstall(req); const resp = client.platformUninstall(req);
resp.on( resp.on(
'data', 'data',
@ -466,7 +476,7 @@ export class BoardsServiceImpl
); );
await new Promise<void>((resolve, reject) => { await new Promise<void>((resolve, reject) => {
resp.on('end', () => { resp.on('end', () => {
this.boardDiscovery.startBoardListWatch(coreClient) this.boardDiscovery.startBoardListWatch(coreClient);
resolve(); resolve();
}); });
resp.on('error', reject); resp.on('error', reject);

View File

@ -271,6 +271,10 @@ export class LibraryServiceImpl
req.setNoDeps(!options.installDependencies); req.setNoDeps(!options.installDependencies);
console.info('>>> Starting library package installation...', item); console.info('>>> Starting library package installation...', item);
// stop the board discovery
await this.boardDiscovery.stopBoardListWatch(coreClient);
const resp = client.libraryInstall(req); const resp = client.libraryInstall(req);
resp.on( resp.on(
'data', 'data',
@ -322,6 +326,10 @@ export class LibraryServiceImpl
if (typeof overwrite === 'boolean') { if (typeof overwrite === 'boolean') {
req.setOverwrite(overwrite); req.setOverwrite(overwrite);
} }
// stop the board discovery
await this.boardDiscovery.stopBoardListWatch(coreClient);
const resp = client.zipLibraryInstall(req); const resp = client.zipLibraryInstall(req);
resp.on( resp.on(
'data', 'data',
@ -354,6 +362,10 @@ export class LibraryServiceImpl
req.setVersion(item.installedVersion!); req.setVersion(item.installedVersion!);
console.info('>>> Starting library package uninstallation...', item); console.info('>>> Starting library package uninstallation...', item);
// stop the board discovery
await this.boardDiscovery.stopBoardListWatch(coreClient);
const resp = client.libraryUninstall(req); const resp = client.libraryUninstall(req);
resp.on( resp.on(
'data', 'data',