-
Notifications
You must be signed in to change notification settings - Fork 2
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
}) | ||
); | ||
|
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved to publisher/extensions/vscode/src/servers.ts Lines 55 to 56 in 95a2a9c
|
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; | ||
} | ||
} | ||
|
||
} |
There was a problem hiding this comment.
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.publisher/extensions/vscode/src/extension.ts
Line 13 in 95a2a9c