Permalink
Browse files

OS-4542 create and use node sysevent library

Reviewed by: Joshua M. Clulow <jmc@joyent.com>
Reviewed by: Josh Wilsdon <jwilsdon@joyent.com>
  • Loading branch information...
bahamas10 committed Aug 3, 2015
1 parent d1bcc8c commit 209e06c1276f349203ff3f0667928911f9599bcd
View
@@ -81,6 +81,7 @@ JS_CHECK_TARGETS=\
vm/sbin/*.js \
vm/node_modules/{dladm,expander,proptable,utils,VM,qmp,openonerrlogger}.js \
vm/node_modules/vmload/*.js \
vm/node_modules/sysevent-stream.js \
img/lib/*.js \
img/sbin/imgadm \
vm/common/nictag.js \
View
@@ -500,6 +500,7 @@ f usr/vm/node_modules/openonerrlogger.js 0444 root bin
f usr/vm/node_modules/props.js 0444 root bin
f usr/vm/node_modules/proptable.js 0444 root bin
f usr/vm/node_modules/qmp.js 0444 root bin
f usr/vm/node_modules/sysevent-stream.js 0444 root bin
f usr/vm/node_modules/utils.js 0444 root bin
d usr/vm/node_modules/vasync 0755 root root
d usr/vm/node_modules/vasync/lib 0755 root root
@@ -792,6 +793,7 @@ f usr/node/0.10/node_modules/lockfd_binding.node 0444 root bin
f usr/node/0.10/node_modules/microtime.node 0444 root bin
f usr/node/0.10/node_modules/net-boot-config.js 0444 root bin
f usr/node/0.10/node_modules/onlyif.js 0444 root bin
f usr/node/0.10/node_modules/linestream.js 0444 root bin
f usr/node/0.10/node_modules/sprintf.js 0444 root bin
f usr/node/0.10/node_modules/sqlite3.js 0444 root bin
f usr/node/0.10/node_modules/sqlite3_trace.js 0444 root bin

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -210,7 +210,7 @@ MetadataAgent.prototype.startDeletedZonesPurger = function () {
MetadataAgent.prototype.start = function () {
var self = this;
var zwatch = this.zwatch = new ZWatch();
var zwatch = this.zwatch = new ZWatch(self.log);
self.createServersOnExistingZones();
self.startDeletedZonesPurger();
@@ -241,8 +241,6 @@ MetadataAgent.prototype.start = function () {
}
}
});
zwatch.start(self.log);
};
MetadataAgent.prototype.stop = function () {
@@ -1,52 +1,45 @@
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var spawn = require('child_process').spawn;
var common = require('./common');
var ZWatch = module.exports = function () {
process.EventEmitter.call(this);
};
var SyseventStream = require('/usr/vm/node_modules/sysevent-stream');
util.inherits(ZWatch, process.EventEmitter);
module.exports = ZWatch;
ZWatch.prototype.start = function (log) {
function ZWatch(logger) {
var self = this;
var handler = function (event) {
if (event.newstate === 'shutting_down'
&& event.oldstate === 'running') {
event.cmd = 'stop';
} else if (event.newstate === 'running'
&& event.oldstate === 'ready') {
event.cmd = 'start';
} else {
event.cmd = 'unknown';
}
// become an event emitter
EventEmitter.call(self);
self.emit('zone_transition', event);
// create a Sysevent event emitter
var opts = {
logger: logger,
class: 'status',
channel: 'com.sun:zones:status'
};
self.se = new SyseventStream(opts);
self.se.on('readable', function () {
var ev;
while ((ev = self.se.read()) !== null) {
var data = ev.data;
if (data.newstate === 'shutting_down'
&& data.oldstate === 'running') {
data.cmd = 'stop';
} else if (data.newstate === 'running'
&& data.oldstate === 'ready') {
data.cmd = 'start';
} else {
data.cmd = 'unknown';
}
self.emit('zone_transition', data);
}
});
}
util.inherits(ZWatch, EventEmitter);
function start() {
log.info('Starting zwatch');
delete self.zwatch;
var zwatch = self.zwatch = spawn('/usr/vm/sbin/zoneevent');
zwatch.stdout.on('data',
common.createJsonChunkParser(log, handler, '\n'));
zwatch.stderr.on('data', function (data) {
log.error('error: ' + data.toString());
});
zwatch.on('exit', function (code) {
log.info('Detected zoneevent exiting (%d)', code);
});
}
start();
};
ZWatch.prototype.stop = function () {
this.zwatch.kill();
ZWatch.prototype.stop = function stop() {
return this.se.stop();
};
Oops, something went wrong.

0 comments on commit 209e06c

Please sign in to comment.