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

fix(vscode): recovers server process if terminated #850

Merged
merged 2 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 5 additions & 3 deletions extensions/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
"Machine Learning",
"Other"
],
"activationEvents": [],
"activationEvents": [
"onStartupFinished"
],
Comment on lines +20 to +22
Copy link
Collaborator Author

@tdstein tdstein Jan 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The activate method is invoked when the startup is finished.

export async function activate(context: vscode.ExtensionContext) {

"main": "./out/extension.js",
"contributes": {
"commands": [
{
"command": "posit.publisher.start",
"command": "posit.publisher.open",
"title": "Open Publisher",
"category": "Posit",
"icon": {
Expand All @@ -48,7 +50,7 @@
"menus": {
"editor/title": [
{
"command": "posit.publisher.start",
"command": "posit.publisher.open",
"group": "navigation"
}
]
Expand Down
52 changes: 0 additions & 52 deletions extensions/vscode/src/assistants.ts

This file was deleted.

11 changes: 7 additions & 4 deletions extensions/vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Import the module and reference it with the alias vscode in your code below
import * as vscode from 'vscode';

import * as ports from './ports';
import { Service } from './services';

// Once the extension is activate, hang on to the service so that we can stop it on deactivation.
Expand All @@ -11,16 +12,18 @@ let service: Service;
// Your extension is activated the very first time the command is executed
export async function activate(context: vscode.ExtensionContext) {

const port = await ports.acquire();
service = new Service(port);
await service.start(context);
Comment on lines +15 to +17
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the activation event, the service is started, which handles starting the server.


context.subscriptions.push(
vscode.commands.registerCommand('posit.publisher.start', async () => {
service = await Service.get(context);
await service.start();
vscode.commands.registerCommand('posit.publisher.open', async () => {
await service.open(context);
})
);

context.subscriptions.push(
vscode.commands.registerCommand('posit.publisher.close', async () => {
service = await Service.get(context);
await service.stop();
})
);
Expand Down
26 changes: 8 additions & 18 deletions extensions/vscode/src/panels.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,21 @@
import * as vscode from 'vscode';

const DEFAULT_COLUMN = vscode.ViewColumn.Beside;
import { HOST } from '.';

export interface IPanel extends vscode.Disposable {
show: () => Promise<undefined>;
}
const DEFAULT_COLUMN = vscode.ViewColumn.Beside;

export class Panel implements IPanel {
export class Panel {

private readonly context: vscode.ExtensionContext;
private readonly url: string;

private column: vscode.ViewColumn = DEFAULT_COLUMN;
private panel?: vscode.WebviewPanel;

/**
* Creates a Panel implementation.
*
* @param {vscode.ExtensionContext} context - The extension content
* @param {string} url - The server url (i.e., http://localhost:8080)
*/
constructor(context: vscode.ExtensionContext, url: string) {
this.context = context;
this.url = url;
constructor(port: number) {
this.url = `http://${HOST}:${port}`;
}

async show(): Promise<undefined> {
async show(context: vscode.ExtensionContext): Promise<undefined> {
// reveal panel if defined
if (this.panel !== undefined) {
this.panel.reveal(this.column);
Expand Down Expand Up @@ -55,7 +45,7 @@ export class Panel implements IPanel {
this.column = event.webviewPanel.viewColumn || DEFAULT_COLUMN;
},
null,
this.context.subscriptions
context.subscriptions
);

// register dispose
Expand All @@ -65,7 +55,7 @@ export class Panel implements IPanel {
this.panel = undefined;
},
null,
this.context.subscriptions
context.subscriptions
);
}

Expand Down
18 changes: 0 additions & 18 deletions extensions/vscode/src/ports.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,5 @@
import getPort = require('get-port');
import * as wait from 'wait-on';

import { HOST } from '.';

export const acquire = async (): Promise<number> => {
return getPort();
};

export const ping = async (port: number, timeout: number = 30 * 1000): Promise<boolean> => {
try {
await wait({
resources: [
`http-get://${HOST}:${port}`
],
timeout: timeout
});
return true;
} catch (e) {
console.warn("failed waiting for port", e);
return false;
}
};
Comment on lines -10 to -23
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to

private async isRunning(): Promise<boolean> {
try {

70 changes: 70 additions & 0 deletions extensions/vscode/src/servers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import * as retry from 'retry';
import * as vscode from 'vscode';
import * as wait from 'wait-on';

import { HOST } from '.';

import * as commands from './commands';
import { Terminal } from './terminals';
import * as workspaces from './workspaces';

export class Server implements Server, vscode.Disposable {

readonly port: number;
readonly terminal: Terminal;

constructor (port: number) {
this.port = port;
this.terminal = new Terminal();
}

async start(context: vscode.ExtensionContext): Promise<void> {
const isRunning = await this.isRunning();
if (!isRunning) {
const message = vscode.window.setStatusBarMessage("Starting Posit Publisher. Please wait...");
const path = workspaces.path();
const command: commands.Command = await commands.create(context, path!, this.port);
this.terminal.get().sendText(command);
await this.isRunning();
// The server will respond as ready before the API has fully initialized. Wait an additional second for good measure.
await new Promise(_ => setTimeout(_, 1000));
message.dispose();
}
}

async stop(): Promise<void> {
const message = vscode.window.setStatusBarMessage("Shutting down Posit Publisher. Please wait...");
const operation = retry.operation();
operation.attempt(async () => {
// send "CTRL+C" command
this.terminal.get().sendText("\u0003");
const isRunning = await this.isRunning();

if (isRunning) {
// throw error to invoke retry
throw Error("application is still running");
}
});
message.dispose();
}

dispose() {
this.terminal.dispose();
}

private async isRunning(): Promise<boolean> {
try {
await wait({
resources: [
`http-get://${HOST}:${this.port}`
],
timeout: 1000
});
return true;
} catch (e) {
console.warn("failed waiting for port", e);
return false;
}
}

}
Loading