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

Wait for gRPC server to start before connecting client #421

Merged
merged 1 commit into from
May 21, 2020
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
4 changes: 1 addition & 3 deletions extension/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ import { LoggerStream } from './LoggerSteam';
const localize = nls.loadMessageBundle();

export class GradleTasksClient implements vscode.Disposable {
// The connectDeadline is a high number because, even though the Java process has been
// started, the gGRPC server is not yet ready to accept connections.
private connectDeadline = 120;
private connectDeadline = 5; // seconds
private grpcClient: GrpcClient | null = null;
private _onConnect: vscode.EventEmitter<null> = new vscode.EventEmitter<
null
Expand Down
15 changes: 11 additions & 4 deletions extension/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as nls from 'vscode-nls';

import { logger } from './logger';
import { buildGradleServerTask } from './tasks';
import { isDebuggingServer } from './util';
import { isDebuggingServer, waitOnTcp } from './util';

const localize = nls.loadMessageBundle();

Expand Down Expand Up @@ -38,11 +38,18 @@ export class GradleTasksServer implements vscode.Disposable {
private readonly context: vscode.ExtensionContext
) {
context.subscriptions.push(
vscode.tasks.onDidStartTaskProcess((event) => {
vscode.tasks.onDidStartTaskProcess(async (event) => {
if (event.execution.task.name === SERVER_TASK_NAME) {
if (isProcessRunning(event.processId)) {
logger.debug('Gradle server process started');
this.fireOnReady();
logger.debug(
'Gradle server process started, waiting for server to start'
);
try {
await waitOnTcp('localhost', this.port!);
this.fireOnReady();
} catch (e) {
logger.error('Gradle server not started:', e.message);
}
} else {
logger.error('Gradle server processes not started');
}
Expand Down
2 changes: 1 addition & 1 deletion extension/src/test/testUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function createTestRunner(pattern: string) {
// Create the mocha test
const mocha = new Mocha({
ui: 'bdd',
timeout: 130000,
timeout: 60000,
color: true,
});
mocha.bail(true);
Expand Down
14 changes: 11 additions & 3 deletions extension/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const isTest = (): boolean =>
export const isDebuggingServer = (): boolean =>
process.env.VSCODE_DEBUG_SERVER?.toLowerCase() === 'true';

const maximumTimeout = 120000; // 2 minutes
const tcpTimeout = 300;

function tcpExists(host: string, port: number): Promise<boolean> {
Expand All @@ -27,14 +28,21 @@ function tcpExists(host: string, port: number): Promise<boolean> {
});
}

async function tryConnect(host: string, port: number): Promise<void> {
async function tryConnect(
host: string,
port: number,
startTime: number
): Promise<void> {
const connected = await tcpExists(host, port);
if (connected) {
return;
}
await tryConnect(host, port);
if (Date.now() - startTime >= maximumTimeout) {
throw new Error('Unable to wait on tcp due to maxmium timeout reached');
}
await tryConnect(host, port, startTime);
}

export function waitOnTcp(host: string, port: number): Promise<void> {
return tryConnect(host, port);
return tryConnect(host, port, Date.now());
}