Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1032 failing upload flag for monitor mgr #1040

Merged
merged 28 commits into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
d10181b
1032 failing upload flag for monitor mgr
davegarthsimpson Jun 10, 2022
eed8a69
move upload failure fix logic to frontend
davegarthsimpson Jun 13, 2022
fff6075
misc corrections
davegarthsimpson Jun 13, 2022
cc12bc0
avoid starting monitor when upload is in progress
Jun 14, 2022
209ac69
avoid starting monitor when upload is in progress
Jun 14, 2022
9585991
Merge branch '1032-uploads-failing-with-monitor-open' of https://gith…
davegarthsimpson Jun 14, 2022
48b9c4d
prevent monitor side effects on upload (WIP)
davegarthsimpson Jun 15, 2022
01604ee
send upload req after notifying mgr
davegarthsimpson Jun 15, 2022
1e7fbf1
Merge branch 'main' into 1032-uploads-failing-with-monitor-open
davegarthsimpson Jun 15, 2022
2d5dff2
dispose instead of pause on upld (code not final)
davegarthsimpson Jun 15, 2022
89e51cb
Revert "dispose instead of pause on upld (code not final)"
davegarthsimpson Jun 16, 2022
a9d968c
force wait before upload (test)
davegarthsimpson Jun 16, 2022
bd02a19
always start queued services after uplaod finishes
Jun 16, 2022
97bdb78
test cli with monitor close delay
davegarthsimpson Jun 16, 2022
a562f0c
clean up unnecessary await(s)
davegarthsimpson Jun 17, 2022
725c0fb
remove unused dependency
davegarthsimpson Jun 17, 2022
6d1e930
Merge branch 'main' into 1032-uploads-failing-with-monitor-open
davegarthsimpson Jun 17, 2022
6710f35
revert CLI to 0.23
davegarthsimpson Jun 17, 2022
5260a07
use master cli for testing, await in upload finish
davegarthsimpson Jun 17, 2022
1678da7
remove upload port from pending monitor requests
davegarthsimpson Jun 17, 2022
37f7311
fix startQueuedServices
davegarthsimpson Jun 17, 2022
99dfd4e
refinements queued monitors
davegarthsimpson Jun 17, 2022
6e264a6
clean up monitor mgr state
davegarthsimpson Jun 17, 2022
c892ce3
fix typo from prev cleanup
davegarthsimpson Jun 19, 2022
a7c8806
avoid dupl queued monitor services
davegarthsimpson Jun 19, 2022
3830ba2
variable name changes
davegarthsimpson Jun 21, 2022
fbf3fc6
Merge branch 'main' into 1032-uploads-failing-with-monitor-open
davegarthsimpson Jun 21, 2022
095bbd9
reference latest cli commit in package.json
davegarthsimpson Jun 21, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion arduino-ide-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,11 @@
],
"arduino": {
"cli": {
"version": "0.23.0"
"version": {
"owner": "arduino",
"repo": "arduino-cli",
"commitish": "c1b10f562f1e1a112e215a69b84e2f2b69e3af2d"
}
},
"fwuploader": {
"version": "2.2.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export class ArduinoFirmwareUploaderImpl implements ArduinoFirmwareUploader {
} catch (e) {
throw e;
} finally {
this.monitorManager.notifyUploadFinished(board, port);
await this.monitorManager.notifyUploadFinished(board, port);
return output;
}
}
Expand Down
2 changes: 1 addition & 1 deletion arduino-ide-extension/src/node/core-service-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export class CoreServiceImpl extends CoreClientAware implements CoreService {
return request;
}

async upload(options: CoreService.Upload.Options): Promise<void> {
upload(options: CoreService.Upload.Options): Promise<void> {
return this.doUpload(
options,
() => new UploadRequest(),
Expand Down
13 changes: 8 additions & 5 deletions arduino-ide-extension/src/node/monitor-manager-proxy-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@ export class MonitorManagerProxyImpl implements MonitorManagerProxy {
if (settings) {
await this.changeMonitorSettings(board, port, settings);
}
const status = await this.manager.startMonitor(board, port);
if (status === Status.ALREADY_CONNECTED || status === Status.OK) {
// Monitor started correctly, connect it with the frontend
this.client.connect(this.manager.getWebsocketAddressPort(board, port));
}

const connectToClient = (status: Status) => {
if (status === Status.ALREADY_CONNECTED || status === Status.OK) {
// Monitor started correctly, connect it with the frontend
this.client.connect(this.manager.getWebsocketAddressPort(board, port));
}
};
return this.manager.startMonitor(board, port, connectToClient);
}

/**
Expand Down
164 changes: 148 additions & 16 deletions arduino-ide-extension/src/node/monitor-manager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ILogger } from '@theia/core';
import { inject, injectable, named } from '@theia/core/shared/inversify';
import { Board, Port, Status } from '../common/protocol';
import { Board, BoardsService, Port, Status } from '../common/protocol';
import { CoreClientAware } from './core-client-provider';
import { MonitorService } from './monitor-service';
import { MonitorServiceFactory } from './monitor-service-factory';
Expand All @@ -11,16 +11,34 @@ import {

type MonitorID = string;

type UploadState = 'uploadInProgress' | 'pausedForUpload' | 'disposedForUpload';
type MonitorIDsByUploadState = Record<UploadState, MonitorID[]>;

export const MonitorManagerName = 'monitor-manager';

@injectable()
export class MonitorManager extends CoreClientAware {
@inject(BoardsService)
protected boardsService: BoardsService;

// Map of monitor services that manage the running pluggable monitors.
// Each service handles the lifetime of one, and only one, monitor.
// If either the board or port managed changes, a new service must
// be started.
private monitorServices = new Map<MonitorID, MonitorService>();

private monitorIDsByUploadState: MonitorIDsByUploadState = {
uploadInProgress: [],
pausedForUpload: [],
disposedForUpload: [],
};

private monitorServiceStartQueue: {
monitorID: string;
serviceStartParams: [Board, Port];
connectToClient: (status: Status) => void;
}[] = [];

@inject(MonitorServiceFactory)
private monitorServiceFactory: MonitorServiceFactory;

Expand Down Expand Up @@ -48,6 +66,33 @@ export class MonitorManager extends CoreClientAware {
return false;
}

private uploadIsInProgress(): boolean {
return this.monitorIDsByUploadState.uploadInProgress.length > 0;
}

private addToMonitorIDsByUploadState(
state: UploadState,
monitorID: string
): void {
this.monitorIDsByUploadState[state].push(monitorID);
}

private removeFromMonitorIDsByUploadState(
state: UploadState,
monitorID: string
): void {
this.monitorIDsByUploadState[state] = this.monitorIDsByUploadState[
state
].filter((id) => id !== monitorID);
}

private monitorIDIsInUploadState(
state: UploadState,
monitorID: string
): boolean {
return this.monitorIDsByUploadState[state].includes(monitorID);
}

/**
* Start a pluggable monitor that receives and sends messages
* to the specified board and port combination.
Expand All @@ -56,13 +101,34 @@ export class MonitorManager extends CoreClientAware {
* @returns a Status object to know if the process has been
* started or if there have been errors.
*/
async startMonitor(board: Board, port: Port): Promise<Status> {
async startMonitor(
board: Board,
port: Port,
connectToClient: (status: Status) => void
): Promise<void> {
const monitorID = this.monitorID(board, port);

let monitor = this.monitorServices.get(monitorID);
if (!monitor) {
monitor = this.createMonitor(board, port);
}
return await monitor.start();

if (this.uploadIsInProgress()) {
this.monitorServiceStartQueue = this.monitorServiceStartQueue.filter(
(request) => request.monitorID !== monitorID
);

this.monitorServiceStartQueue.push({
monitorID,
serviceStartParams: [board, port],
connectToClient,
});

return;
}

const result = await monitor.start();
connectToClient(result);
}

/**
Expand Down Expand Up @@ -111,14 +177,18 @@ export class MonitorManager extends CoreClientAware {
// to retrieve if we don't have this information.
return;
}

const monitorID = this.monitorID(board, port);
this.addToMonitorIDsByUploadState('uploadInProgress', monitorID);

const monitor = this.monitorServices.get(monitorID);
if (!monitor) {
// There's no monitor running there, bail
return;
}
monitor.setUploadInProgress(true);
return await monitor.pause();

this.addToMonitorIDsByUploadState('pausedForUpload', monitorID);
return monitor.pause();
}

/**
Expand All @@ -130,19 +200,69 @@ export class MonitorManager extends CoreClientAware {
* started or if there have been errors.
*/
async notifyUploadFinished(board?: Board, port?: Port): Promise<Status> {
if (!board || !port) {
// We have no way of knowing which monitor
// to retrieve if we don't have this information.
return Status.NOT_CONNECTED;
let status: Status = Status.NOT_CONNECTED;
let portDidChangeOnUpload = false;

// We have no way of knowing which monitor
// to retrieve if we don't have this information.
if (board && port) {
const monitorID = this.monitorID(board, port);
this.removeFromMonitorIDsByUploadState('uploadInProgress', monitorID);

const monitor = this.monitorServices.get(monitorID);
if (monitor) {
status = await monitor.start();
}

// this monitorID will only be present in "disposedForUpload"
// if the upload changed the board port
portDidChangeOnUpload = this.monitorIDIsInUploadState(
'disposedForUpload',
monitorID
);
if (portDidChangeOnUpload) {
this.removeFromMonitorIDsByUploadState('disposedForUpload', monitorID);
}

// in case a service was paused but not disposed
this.removeFromMonitorIDsByUploadState('pausedForUpload', monitorID);
}
const monitorID = this.monitorID(board, port);
const monitor = this.monitorServices.get(monitorID);
if (!monitor) {
// There's no monitor running there, bail
return Status.NOT_CONNECTED;

await this.startQueuedServices(portDidChangeOnUpload);
return status;
}

async startQueuedServices(portDidChangeOnUpload: boolean): Promise<void> {
// if the port changed during upload with the monitor open, "startMonitorPendingRequests"
// will include a request for our "upload port', most likely at index 0.
// We remove it, as this port was to be used exclusively for the upload
const queued = portDidChangeOnUpload
? this.monitorServiceStartQueue.slice(1)
: this.monitorServiceStartQueue;
this.monitorServiceStartQueue = [];

for (const {
monitorID,
serviceStartParams: [_, port],
connectToClient,
} of queued) {
const boardsState = await this.boardsService.getState();
const boardIsStillOnPort = Object.keys(boardsState)
.map((connection: string) => {
const portAddress = connection.split('|')[0];
return portAddress;
})
.some((portAddress: string) => port.address === portAddress);

if (boardIsStillOnPort) {
const monitorService = this.monitorServices.get(monitorID);

if (monitorService) {
const result = await monitorService.start();
connectToClient(result);
}
}
}
monitor.setUploadInProgress(false);
return await monitor.start();
}

/**
Expand Down Expand Up @@ -202,6 +322,18 @@ export class MonitorManager extends CoreClientAware {
this.monitorServices.set(monitorID, monitor);
monitor.onDispose(
(() => {
// if a service is disposed during upload and
// we paused it beforehand we know it was disposed
// of because the upload changed the board port
if (
this.uploadIsInProgress() &&
this.monitorIDIsInUploadState('pausedForUpload', monitorID)
) {
this.removeFromMonitorIDsByUploadState('pausedForUpload', monitorID);

this.addToMonitorIDsByUploadState('disposedForUpload', monitorID);
}

this.monitorServices.delete(monitorID);
}).bind(this)
);
Expand Down
14 changes: 0 additions & 14 deletions arduino-ide-extension/src/node/monitor-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ export class MonitorService extends CoreClientAware implements Disposable {
protected readonly onDisposeEmitter = new Emitter<void>();
readonly onDispose = this.onDisposeEmitter.event;

protected uploadInProgress = false;
protected _initialized = new Deferred<void>();
protected creating: Deferred<Status>;

Expand Down Expand Up @@ -114,10 +113,6 @@ export class MonitorService extends CoreClientAware implements Disposable {
return this._initialized.promise;
}

setUploadInProgress(status: boolean): void {
this.uploadInProgress = status;
}

getWebsocketAddressPort(): number {
return this.webSocketProvider.getAddress().port;
}
Expand Down Expand Up @@ -161,15 +156,6 @@ export class MonitorService extends CoreClientAware implements Disposable {
return this.creating.promise;
}

if (this.uploadInProgress) {
this.updateClientsSettings({
monitorUISettings: { connected: false, serialPort: this.port.address },
});

this.creating.resolve(Status.UPLOAD_IN_PROGRESS);
return this.creating.promise;
}

this.logger.info('starting monitor');

// get default monitor settings from the CLI
Expand Down