2015-12-16 22:57:58 +08:00
|
|
|
'use strict';
|
|
|
|
|
2016-07-07 20:14:46 +08:00
|
|
|
var debug = require('./util/debug')('pubsub:publisher');
|
|
|
|
var error = require('./util/debug')('pubsub:publisher:error');
|
2016-07-07 16:44:17 +08:00
|
|
|
|
2016-07-07 20:14:46 +08:00
|
|
|
var DB = 0;
|
2016-07-07 16:44:17 +08:00
|
|
|
|
2016-07-07 20:14:46 +08:00
|
|
|
function JobPublisher(pool) {
|
|
|
|
this.pool = pool;
|
2015-12-29 22:46:04 +08:00
|
|
|
this.channel = 'batch:hosts';
|
2015-12-16 22:57:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
JobPublisher.prototype.publish = function (host) {
|
2016-07-07 16:44:17 +08:00
|
|
|
var self = this;
|
|
|
|
|
2016-07-07 20:14:46 +08:00
|
|
|
this.pool.acquire(DB, function (err, client) {
|
2016-07-07 16:44:17 +08:00
|
|
|
if (err) {
|
2016-07-07 20:14:46 +08:00
|
|
|
return error('Error adquiring redis client: ' + err.message);
|
2016-07-07 16:44:17 +08:00
|
|
|
}
|
|
|
|
|
2016-07-07 20:14:46 +08:00
|
|
|
client.publish(self.channel, host, function (err) {
|
|
|
|
if (err) {
|
|
|
|
return error('Error publishing to ' + self.channel + ':' + host + ', ' + err.message);
|
|
|
|
}
|
|
|
|
|
|
|
|
debug('publish to ' + self.channel + ':' + host);
|
|
|
|
self.pool.release(DB, client);
|
|
|
|
});
|
2016-07-07 16:44:17 +08:00
|
|
|
});
|
2015-12-16 22:57:58 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = JobPublisher;
|