Skip to content

Commit

Permalink
Fix server not restarting itself on fatal error
Browse files Browse the repository at this point in the history
This also cleans up the server startup in Proxy since most of the 
restarting logic is now handled by the Atom language client.

References #460
  • Loading branch information
Gert-dev committed May 18, 2019
1 parent ef4986a commit 2865fea
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 49 deletions.
54 changes: 7 additions & 47 deletions lib/Proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ module.exports =

// this.closeServerConnection();

this.phpServer = null;
reject();
});
});
Expand All @@ -149,57 +148,18 @@ module.exports =
}

/**
* Spawns the PHP socket server process.
*
* @param {Number} port
*
* @return {Promise}
*/
spawnPhpServerIfNecessary(port) {
if (this.phpServer) {
this.phpServerPromise = null;
async getSocketConnection() {
return new Promise(async(resolve) => {
const phpServer = await this.spawnPhpServer(this.port);

return new Promise((resolve, reject) => {
return resolve(this.phpServer);
const client = net.createConnection({port: this.port}, () => {
resolve([client, phpServer]);
});

} else if (this.phpServerPromise) {
return this.phpServerPromise;
}

const successHandler = phpServer => {
this.phpServer = phpServer;

return phpServer;
};

const failureHandler = () => {
return this.phpServerPromise = null;
};

this.phpServerPromise = this.spawnPhpServer(port).then(successHandler, failureHandler);

return this.phpServerPromise;
}

/**
* @return {Promise}
*/
getSocketConnection() {
return new Promise((resolve, reject) => {
return this.spawnPhpServerIfNecessary(this.port).then(() => {
if (this.client != null) {
resolve(this.client);
return;
}

this.client = net.createConnection({port: this.port}, () => {
return resolve(this.client);
});

this.client.setNoDelay(true);
this.client.on('error', this.onSocketError.bind(this));
});
client.setNoDelay(true);
client.on('error', this.onSocketError.bind(this));
});
}

Expand Down
6 changes: 4 additions & 2 deletions lib/SerenataClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ class SerenataClient extends AutoLanguageClient
}

async startServerProcess() {
this.socket = await this.proxy.getSocketConnection();
let server;

return this.proxy.phpServer;
[this.socket, server] = await this.proxy.getSocketConnection();

return server;
}

preInitialization(connection) {
Expand Down

0 comments on commit 2865fea

Please sign in to comment.