Add start/stop to processes

This commit is contained in:
Ryan Huffman 2015-12-03 13:24:39 -08:00
parent ad164d3093
commit a9de5d212d
3 changed files with 71 additions and 29 deletions

View file

@ -8,12 +8,13 @@
<h1>Console</h1>
<h2>Process Statuses</h2>
<h2>Interface</h2>
<div id="process-status">
<div id="process-interface">
<span class="name">Interface</span>
<span class="status">unknown</span>
<button class="power">Turn On</button>
<button class="power-on">Turn On</button>
<button class="power-off">Turn Off</button>
</div>
</div>

View file

@ -8,12 +8,24 @@ ready = function() {
console.log("update", event, arg);
var state = arg.interface.state;
$('#process-interface .status').text(state);
$('#process-interface .power').prop('disabled', state != 'stopped');
var on = state != 'stopped';
if (on) {
$('#process-interface .power-on').hide();
$('#process-interface .power-off').show();
} else {
$('#process-interface .power-on').show();
$('#process-interface .power-off').hide();
}
}
$('#process-interface .power').click(function() {
$('#process-interface .power-on').click(function() {
ipcRenderer.send('start-process', { name: 'interface' });
});
$('#process-interface .power-off').click(function() {
ipcRenderer.send('stop-process', { name: 'interface' });
});
ipcRenderer.on('process-update', onProcessUpdate);
ipcRenderer.send('update');
};

View file

@ -22,43 +22,63 @@ const ProcessStates = {
};
var ID = 0;
function Process(name, command) {
function Process(name, command, commandArgs) {
events.EventEmitter.call(this);
this.id = ++ID;
this.name = name;
this.command = command;
this.commandArgs = commandArgs ? commandArgs : [];
this.child = null;
this.state = ProcessStates.STOPPED;
};
util.inherits(Process, events.EventEmitter);
Process.prototype.start = function() {
if (this.state != ProcessStates.STOPPED) {
console.warn("Can't start process that is not stopped.");
return;
}
console.log("Starting " + this.command);
try {
this.child = childProcess.spawn(this.command);
this.child.on('close', this.childClosed.bind(this));
this.state = ProcessStates.STARTED;
console.log("Child process started");
} catch (e) {
console.log("Got error starting child process for " + this.name);
this.child = null;
Process.prototype = extend(Process.prototype, {
start: function() {
if (this.state != ProcessStates.STOPPED) {
console.warn("Can't start process that is not stopped.");
return;
}
console.log("Starting " + this.command);
try {
this.child = childProcess.spawn(this.command, this.commandArgs, {
detached: false
});
//console.log("started ", this.child);
this.child.on('error', this.onChildStartError.bind(this));
this.child.on('close', this.onChildClose.bind(this));
this.state = ProcessStates.STARTED;
console.log("Child process started");
} catch (e) {
console.log("Got error starting child process for " + this.name, e);
this.child = null;
this.state = ProcessStates.STOPPED;
}
this.emit('state-update');
},
stop: function() {
if (this.state != ProcessStates.STARTED) {
console.warn("Can't stop process that is not started.");
return;
}
this.child.kill();
this.state = ProcessStates.STOPPING;
},
// Events
onChildStartError: function(error) {
console.log("Child process error ", error);
this.state = ProcessStates.STOPPED;
this.emit('state-update');
},
onChildClose: function(code) {
console.log("Child process closed with code ", code);
this.state = ProcessStates.STOPPED;
this.emit('state-update');
}
this.emit('state-update');
};
// Events
Process.prototype.childClosed = function(code) {
console.log("Child process closed with code ", code);
this.state = ProcessStates.STOPPED;
this.emit('state-update');
};
});
// Quit when all windows are closed.
app.on('window-all-closed', function() {
@ -105,4 +125,13 @@ app.on('ready', function() {
pInterface.start();
sendProcessUpdate();
});
ipcMain.on('stop-process', function(event, arg) {
pInterface.stop();
sendProcessUpdate();
});
ipcMain.on('update', function(event, arg) {
sendProcessUpdate();
});
sendProcessUpdate();
});