Skip to content

Commit

Permalink
Merge pull request #421 from badsyntax/wait-for-server
Browse files Browse the repository at this point in the history
Wait for gRPC server to start before connecting client
  • Loading branch information
badsyntax authored May 21, 2020
2 parents e7b8236 + bd59199 commit 6cd108e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
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());
}

0 comments on commit 6cd108e

Please sign in to comment.