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

Removing Forever dependency #971

Merged
merged 10 commits into from
Oct 21, 2017
Merged
Show file tree
Hide file tree
Changes from 5 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: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).
Use the following format for additions: ` - VERSION: [feature/patch (if applicable)] Short description of change. Links to relevant issues/PRs.`

- 1.1.31: Bump dependencies
- 1.1.31:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also bump version in package.json.

- Bump dependencies
- Remove forever-monitor [#961](https://github.com/FredrikNoren/ungit/issues/961)
- 1.1.30:
- move unit tests to es6
- Add squash feature [#129](https://github.com/FredrikNoren/ungit/issues/129)
Expand Down
2 changes: 0 additions & 2 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,6 @@ module.exports = (grunt) => {
const keys = Object.keys(tempPackageJson.dependencies).concat(Object.keys(tempPackageJson.devDependencies))

const bumps = Bluebird.map(keys, (dep) => {
// https://github.com/FredrikNoren/ungit/pull/964
if (dep == 'forever-monitor') return;
// Superagent 1.x has a new api, need to upgrade to that if we want to bump
if (dep == 'superagent') return
// Octicon moved to SCSS instead of less
Expand Down
138 changes: 58 additions & 80 deletions bin/ungit
Original file line number Diff line number Diff line change
@@ -1,107 +1,85 @@
#!/usr/bin/env node
var startLaunchTime = Date.now();
var forever = require('forever-monitor');
var config = require('../src/config');
var open = require('open');
var path = require('path');
var child_process = require('child_process');
var async = require('async');
const startLaunchTime = Date.now();
const config = require('../src/config');
const open = require('open');
const path = require('path');
const child_process = require('child_process');

var BugTracker = require('../src/bugtracker');
var bugtracker = new BugTracker('launcher');
var usageStatistics = require('../src/usage-statistics');
const BugTracker = require('../src/bugtracker');
const bugtracker = new BugTracker('launcher');
const usageStatistics = require('../src/usage-statistics');
const Bluebird = require('bluebird');
// Fastest way to find out if a port is used or not/i.e. if ungit is running
const net = require('net');
const server = net.createServer();
const cleanExit = () => process.exit();

var child = new (forever.Monitor)(path.join(__dirname, '..', 'src', 'server.js'), {
silent: false,
minUptime: 2000,
max: config.maxNAutoRestartOnCrash,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is maxNAutoRestartOnCrash used anywhere else?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really... From my tests forever monitor actually never kicked in and process didn't die during errors. Errors seems to be captured and handled so..

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, so that means the config setting can be removed.

cwd: path.join(process.cwd(), '..'),
options: process.argv.slice(2),
env: { LANG: 'en_US.UTF-8' }
});
process.on('SIGINT', cleanExit); // catch ctrl-c
process.on('SIGTERM', cleanExit); // catch kill
process.on('uncaughtException', (err) => {
console.error(err.stack.toString());

process.on('exit', function() {
child.kill();
console.log('Exit: killed children')
Bluebird.all([
Bluebird.resolve().then(() => bugtracker.notify(err, 'ungit-launcher')),
Bluebird.resolve().then(() => usageStatistics.addEvent('launcher-exception'))
]).then(cleanExit)
});

var cleanExit = function() { process.exit() };
process.on('SIGINT', cleanExit); // catch ctrl-c
process.on('SIGTERM', cleanExit); // catch kill
const launch = () => {
const child = child_process.spawn('node',
[path.join(__dirname, '..', 'src', 'server.js')].concat(process.argv.slice(2)),
{ cwd: path.join(process.cwd(), '..') }
);

process.on('uncaughtException', function(err) {
console.error(err.stack.toString());
async.parallel([
bugtracker.notify.bind(bugtracker, err, 'ungit-launcher'),
usageStatistics.addEvent.bind(usageStatistics, 'launcher-exception')
], function() {
process.exit();
child.on('exit', (res) => {
console.log('Stopped keeping ungit alive');
});
});

child.on('exit', function (res) {
console.log('Stopped keeping ungit alive');
});
let currentUrl = `${config.urlBase}:${config.port}${config.rootPath}`
if (config.forcedLaunchPath === undefined) {
currentUrl += `/#/repository?path=${encodeURIComponent(process.cwd())}`
} else if (config.forcedLaunchPath !== null && config.forcedLaunchPath !== '') {
currentUrl += `/#/repository?path=${encodeURIComponent(config.forcedLaunchPath)}`
}

function launch(callback) {
var currentUrl = config.urlBase + ':' + config.port + config.rootPath;
if (config.forcedLaunchPath === undefined) currentUrl += '/#/repository?path=' + encodeURIComponent(process.cwd());
else if (config.forcedLaunchPath !== null && config.forcedLaunchPath !== '') currentUrl += '/#/repository?path=' + encodeURIComponent(config.forcedLaunchPath);
console.log('Browse to ' + currentUrl);
if (config.launchBrowser && !config.launchCommand) {
console.log(`Browse to ${currentUrl}`);
open(currentUrl);
} else if (config.launchCommand) {
var command = config.launchCommand.replace(/%U/g, currentUrl);
console.log('Running custom launch command: ' + command);
child_process.exec(command, function(err, stdout, stderr) {
const command = config.launchCommand.replace(/%U/g, currentUrl);
console.log(`Running custom launch command: ${command}`);
child_process.exec(command, (err, stdout, stderr) => {
if (err) {
callback(err);
console.log(err)
return;
}
if (config.launchBrowser)
if (config.launchBrowser) {
open(currentUrl);
}
});
}
}

function startupListener(data) {
if (data.toString().indexOf('## Ungit started ##') >= 0) {
launch(function(err) {
if (err) console.log(err);
});
child.removeListener('stdout', startupListener);
var launchTime = (Date.now() - startLaunchTime);
console.log('Took ' + launchTime + 'ms to start server.');
usageStatistics.addEvent('server-start', { launchTimeMs: launchTime });
const startupListener = (data) => {
if (data.toString().indexOf('## Ungit started ##') >= 0) {
child.removeListener('stdout', startupListener);
child.stdout.on('data', (data) => console.log(data.toString().trim()))
const launchTime = (Date.now() - startLaunchTime);
console.log(data.toString());
console.log(`Took ${launchTime}ms to start server.`);
usageStatistics.addEvent('server-start', { launchTimeMs: launchTime });
}
}
}

child.on('stdout', startupListener);

function checkIfUngitIsRunning(callback) {
// Fastest way to find out if a port is used or not/i.e. if ungit is running
var net = require('net');
var server = net.createServer(function(c) { });
server.listen(config.port, function(err) {
server.close(function() {
callback(null, false);
});
});
server.on('error', function (e) {
if (e.code == 'EADDRINUSE') {
callback(null, true);
}
});
child.stdout.on('data', startupListener);
child.stderr.on('data', (data) => console.log(`stderr: ${data.toString().trim()}`))
}

checkIfUngitIsRunning(function(err1, ungitRunning) {
if (ungitRunning) {
server.listen(config.port, (err) => {
server.close(launch);
});
server.on('error', (e) => {
if (e.code == 'EADDRINUSE') {
console.log('Ungit server already running');
launch(function(err) {
if (err) console.log(err);
});
}
else {
child.start();
cleanExit();
}
});
Loading