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(ios): fixed debug log server issues (TIMOB-27074) #10975

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion iphone/TitaniumKit/TitaniumKit/Sources/API/TiApp.m
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,7 @@ - (void)applicationWillTerminate:(UIApplication *)application
[kjsBridge shutdown:condition];

if ([[TiSharedConfig defaultConfig] logServerEnabled]) {
[[TiLogServer defaultLogServer] start];
[[TiLogServer defaultLogServer] stop];
}

//This will shut down the modules.
Expand Down
102 changes: 32 additions & 70 deletions iphone/cli/commands/_build.js
Original file line number Diff line number Diff line change
Expand Up @@ -2505,78 +2505,40 @@ iOSBuilder.prototype.determineLogServerPort = function determineLogServerPort(ne
return next();
}

const _t = this;

this.logger.debug(__('Checking if log server port %d is available', this.tiLogServerPort));

// for simulator builds, the port is shared with the local machine, so we
// just need to detect if the port is available with the help of Node
const server = net.createServer();

server.on('error', function () {
// we weren't able to bidn to the port :(
server.close(function () {
_t.logger.debug(__('Log server port %s is in use, testing if it\'s the app we\'re building', _t.tiLogServerPort));

let client = null;

function die(error) {
client && client.destroy();
if (error && error.code === 'ENOTFOUND') {
_t.logger.error(__('Unable to connect to log server on localhost'));
_t.logger.error(__('Please ensure your /etc/hosts file contains a valid entry for `localhost`'));
} else {
_t.logger.error(__('Another process is currently bound to port %d', _t.tiLogServerPort));
_t.logger.error(__('Set a unique <log-server-port> between 1024 and 65535 in the <ios> section of the tiapp.xml') + '\n');
}
process.exit(1);
}
// The Plan
//
// We are going to try to create a Node.js server to see if the port is available.
//
// If the port is NOT available, then we're gonna randomly try to pick a port until we find an
// open one.

// connect to the port and see if it's a Titanium app...
// - if the port is bound by a Titanium app with the same appid, then assume
// that when we install new build, the old process will be terminated
// - if the port is bound by another process, such as MySQL on port 3306,
// then we will fail out
// - if the port is bound by another process that expects data before the
// response is returned, then we will just timeout and fail out
// - if localhost cannot be resolved then we will fail out and inform
// the user of that
client = net.connect({
host: 'localhost',
port: _t.tiLogServerPort,
timeout: parseInt(_t.config.get('ios.logServerTestTimeout', 1000)) || null
})
.on('data', function (data) {
client.destroy();
try {
const headers = JSON.parse(data.toString().split('\n').shift());
if (headers.appId !== _t.tiapp.id) {
_t.logger.error(__('Another Titanium app "%s" is currently running and using the log server port %d', headers.appId, _t.tiLogServerPort));
_t.logger.error(__('Stop the running Titanium app, then rebuild this app'));
_t.logger.error(__('-or-'));
_t.logger.error(__('Set a unique <log-server-port> between 1024 and 65535 in the <ios> section of the tiapp.xml') + '\n');
process.exit(1);
}
} catch (e) {
die(e);
}
_t.logger.debug(__('The log server port is being used by the app being built, continuing'));
next();
})
.on('error', die)
.on('timeout', die);
});
});
let done = false;
async.whilst(
() => !done,
cb => {
// for simulator builds, the port is shared with the local machine, so we
// just need to detect if the port is available with the help of Node
const server = net.createServer();

server.on('error', () => {
server.close(() => {
this.logger.debug(__('Log server port %s is in use, trying another port', cyan(String(this.tiLogServerPort))));
this.tiLogServerPort = parseInt(Math.random() * 50000) + 10000;
cb();
});
});

server.listen({
host: 'localhost',
port: _t.tiLogServerPort
}, function () {
server.close(function () {
_t.logger.debug(__('Log server port %s is available', _t.tiLogServerPort));
next();
});
});
server.listen({
host: '127.0.0.1',
port: this.tiLogServerPort
}, () => {
this.logger.debug(__('Using log server port %s', cyan(String(this.tiLogServerPort))));
done = true;
server.close(cb);
});
},
next
);
};

iOSBuilder.prototype.loginfo = function loginfo() {
Expand Down