mirror of
https://github.com/openstf/stf
synced 2025-10-04 02:09:32 +02:00
Make all zmq endpoints use srv records.
This commit is contained in:
parent
f0aae7d4e5
commit
e43ae34d4b
8 changed files with 118 additions and 32 deletions
|
@ -1,8 +1,10 @@
|
|||
var syrup = require('stf-syrup')
|
||||
|
||||
var zmq = require('zmq')
|
||||
var Promise = require('bluebird')
|
||||
|
||||
var logger = require('../../../util/logger')
|
||||
var srv = require('../../../util/srv')
|
||||
|
||||
module.exports = syrup.serial()
|
||||
.define(function(options) {
|
||||
|
@ -10,9 +12,14 @@ module.exports = syrup.serial()
|
|||
|
||||
// Output
|
||||
var push = zmq.socket('push')
|
||||
options.endpoints.push.forEach(function(endpoint) {
|
||||
log.info('Sending output to %s', endpoint)
|
||||
push.connect(endpoint)
|
||||
Promise.map(options.endpoints.push, function(endpoint) {
|
||||
return srv.resolve(endpoint).then(function(records) {
|
||||
return srv.attempt(records, function(record) {
|
||||
log.info('Sending output to "%s"', record.url)
|
||||
push.connect(record.url)
|
||||
return Promise.resolve(true)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
return push
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
var syrup = require('stf-syrup')
|
||||
|
||||
var zmq = require('zmq')
|
||||
var Promise = require('bluebird')
|
||||
|
||||
var logger = require('../../../util/logger')
|
||||
var wireutil = require('../../../wire/util')
|
||||
var srv = require('../../../util/srv')
|
||||
|
||||
module.exports = syrup.serial()
|
||||
.define(function(options) {
|
||||
|
@ -11,9 +13,14 @@ module.exports = syrup.serial()
|
|||
|
||||
// Input
|
||||
var sub = zmq.socket('sub')
|
||||
options.endpoints.sub.forEach(function(endpoint) {
|
||||
log.info('Receiving input from %s', endpoint)
|
||||
sub.connect(endpoint)
|
||||
Promise.map(options.endpoints.sub, function(endpoint) {
|
||||
return srv.resolve(endpoint).then(function(records) {
|
||||
return srv.attempt(records, function(record) {
|
||||
log.info('Receiving input from "%s"', record.url)
|
||||
sub.connect(record.url)
|
||||
return Promise.resolve(true)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
// Establish always-on channels
|
||||
|
|
|
@ -9,6 +9,7 @@ var wire = require('../../wire')
|
|||
var wirerouter = require('../../wire/router')
|
||||
var wireutil = require('../../wire/util')
|
||||
var lifecycle = require('../../util/lifecycle')
|
||||
var srv = require('../../util/srv')
|
||||
|
||||
var COLORS = {
|
||||
1: 'gray'
|
||||
|
@ -28,9 +29,14 @@ module.exports = function(options) {
|
|||
|
||||
// Input
|
||||
var sub = zmq.socket('sub')
|
||||
options.endpoints.sub.forEach(function(endpoint) {
|
||||
log.info('Receiving input from %s', endpoint)
|
||||
sub.connect(endpoint)
|
||||
Promise.map(options.endpoints.sub, function(endpoint) {
|
||||
return srv.resolve(endpoint).then(function(records) {
|
||||
return srv.attempt(records, function(record) {
|
||||
log.info('Receiving input from "%s"', record.url)
|
||||
sub.connect(record.url)
|
||||
return Promise.resolve(true)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
// Establish always-on channels
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
var Promise = require('bluebird')
|
||||
var zmq = require('zmq')
|
||||
|
||||
var logger = require('../../util/logger')
|
||||
|
@ -6,6 +7,7 @@ var wirerouter = require('../../wire/router')
|
|||
var wireutil = require('../../wire/util')
|
||||
var dbapi = require('../../db/api')
|
||||
var lifecycle = require('../../util/lifecycle')
|
||||
var srv = require('../../util/srv')
|
||||
|
||||
module.exports = function(options) {
|
||||
var log = logger.createLogger('processor')
|
||||
|
@ -16,9 +18,14 @@ module.exports = function(options) {
|
|||
|
||||
// App side
|
||||
var appDealer = zmq.socket('dealer')
|
||||
options.endpoints.appDealer.forEach(function(endpoint) {
|
||||
log.info('App dealer connected to %s', endpoint)
|
||||
appDealer.connect(endpoint)
|
||||
Promise.map(options.endpoints.appDealer, function(endpoint) {
|
||||
return srv.resolve(endpoint).then(function(records) {
|
||||
return srv.attempt(records, function(record) {
|
||||
log.info('App dealer connected to "%s"', record.url)
|
||||
appDealer.connect(record.url)
|
||||
return Promise.resolve(true)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
appDealer.on('message', function(channel, data) {
|
||||
|
@ -27,9 +34,14 @@ module.exports = function(options) {
|
|||
|
||||
// Device side
|
||||
var devDealer = zmq.socket('dealer')
|
||||
options.endpoints.devDealer.forEach(function(endpoint) {
|
||||
log.info('Device dealer connected to %s', endpoint)
|
||||
devDealer.connect(endpoint)
|
||||
Promise.map(options.endpoints.devDealer, function(endpoint) {
|
||||
return srv.resolve(endpoint).then(function(records) {
|
||||
return srv.attempt(records, function(record) {
|
||||
log.info('Device dealer connected to "%s"', record.url)
|
||||
devDealer.connect(record.url)
|
||||
return Promise.resolve(true)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
devDealer.on('message', wirerouter()
|
||||
|
|
|
@ -11,6 +11,7 @@ var wireutil = require('../../wire/util')
|
|||
var wirerouter = require('../../wire/router')
|
||||
var procutil = require('../../util/procutil')
|
||||
var lifecycle = require('../../util/lifecycle')
|
||||
var srv = require('../../util/srv')
|
||||
|
||||
module.exports = function(options) {
|
||||
var log = logger.createLogger('provider')
|
||||
|
@ -71,16 +72,26 @@ module.exports = function(options) {
|
|||
|
||||
// Output
|
||||
var push = zmq.socket('push')
|
||||
options.endpoints.push.forEach(function(endpoint) {
|
||||
log.info('Sending output to %s', endpoint)
|
||||
push.connect(endpoint)
|
||||
Promise.map(options.endpoints.push, function(endpoint) {
|
||||
return srv.resolve(endpoint).then(function(records) {
|
||||
return srv.attempt(records, function(record) {
|
||||
log.info('Sending output to "%s"', record.url)
|
||||
push.connect(record.url)
|
||||
return Promise.resolve(true)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
// Input
|
||||
var sub = zmq.socket('sub')
|
||||
options.endpoints.sub.forEach(function(endpoint) {
|
||||
log.info('Receiving input from %s', endpoint)
|
||||
sub.connect(endpoint)
|
||||
Promise.map(options.endpoints.sub, function(endpoint) {
|
||||
return srv.resolve(endpoint).then(function(records) {
|
||||
return srv.attempt(records, function(record) {
|
||||
log.info('Receiving input from "%s"', record.url)
|
||||
sub.connect(record.url)
|
||||
return Promise.resolve(true)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
// Establish always-on channels
|
||||
|
|
|
@ -6,6 +6,7 @@ var wire = require('../../wire')
|
|||
var wireutil = require('../../wire/util')
|
||||
var dbapi = require('../../db/api')
|
||||
var lifecycle = require('../../util/lifecycle')
|
||||
var srv = require('../../util/srv')
|
||||
|
||||
module.exports = function(options) {
|
||||
var log = logger.createLogger('reaper')
|
||||
|
@ -17,9 +18,14 @@ module.exports = function(options) {
|
|||
|
||||
// Output
|
||||
var push = zmq.socket('push')
|
||||
options.endpoints.push.forEach(function(endpoint) {
|
||||
log.info('Sending output to %s', endpoint)
|
||||
push.connect(endpoint)
|
||||
Promise.map(options.endpoints.push, function(endpoint) {
|
||||
return srv.resolve(endpoint).then(function(records) {
|
||||
return srv.attempt(records, function(record) {
|
||||
log.info('Sending output to "%s"', record.url)
|
||||
push.connect(record.url)
|
||||
return Promise.resolve(true)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
function reap() {
|
||||
|
|
|
@ -15,6 +15,7 @@ var wireutil = require('../../wire/util')
|
|||
var wirerouter = require('../../wire/router')
|
||||
var dbapi = require('../../db/api')
|
||||
var datautil = require('../../util/datautil')
|
||||
var srv = require('../../util/srv')
|
||||
var cookieSession = require('./middleware/cookie-session')
|
||||
var ip = require('./middleware/remote-ip')
|
||||
var auth = require('./middleware/auth')
|
||||
|
@ -30,16 +31,26 @@ module.exports = function(options) {
|
|||
|
||||
// Output
|
||||
var push = zmq.socket('push')
|
||||
options.endpoints.push.forEach(function(endpoint) {
|
||||
log.info('Sending output to %s', endpoint)
|
||||
push.connect(endpoint)
|
||||
Promise.map(options.endpoints.push, function(endpoint) {
|
||||
return srv.resolve(endpoint).then(function(records) {
|
||||
return srv.attempt(records, function(record) {
|
||||
log.info('Sending output to "%s"', record.url)
|
||||
push.connect(record.url)
|
||||
return Promise.resolve(true)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
// Input
|
||||
var sub = zmq.socket('sub')
|
||||
options.endpoints.sub.forEach(function(endpoint) {
|
||||
log.info('Receiving input from %s', endpoint)
|
||||
sub.connect(endpoint)
|
||||
Promise.map(options.endpoints.sub, function(endpoint) {
|
||||
return srv.resolve(endpoint).then(function(records) {
|
||||
return srv.attempt(records, function(record) {
|
||||
log.info('Receiving input from "%s"', record.url)
|
||||
sub.connect(record.url)
|
||||
return Promise.resolve(true)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
// Establish always-on channels
|
||||
|
|
|
@ -4,6 +4,8 @@ var util = require('util')
|
|||
var Promise = require('bluebird')
|
||||
var dns = Promise.promisifyAll(require('dns'))
|
||||
|
||||
var srv = module.exports = Object.create(null)
|
||||
|
||||
function groupByPriority(records) {
|
||||
function sortByPriority(a, b) {
|
||||
return a.priority - b.priority
|
||||
|
@ -61,11 +63,21 @@ function flatten(groupedRecords) {
|
|||
}, [])
|
||||
}
|
||||
|
||||
module.exports.sort = function(records) {
|
||||
function NEXT() {
|
||||
Error.call(this)
|
||||
this.name = 'NEXT'
|
||||
Error.captureStackTrace(this, NEXT)
|
||||
}
|
||||
|
||||
util.inherits(NEXT, Error)
|
||||
|
||||
srv.NEXT = NEXT
|
||||
|
||||
srv.sort = function(records) {
|
||||
return flatten(groupByPriority(records).map(shuffleWeighted))
|
||||
}
|
||||
|
||||
module.exports.resolve = function(domain) {
|
||||
srv.resolve = function(domain) {
|
||||
var parsedUrl = url.parse(domain)
|
||||
|
||||
if (!parsedUrl.protocol) {
|
||||
|
@ -97,3 +109,17 @@ module.exports.resolve = function(domain) {
|
|||
}])
|
||||
}
|
||||
}
|
||||
|
||||
srv.attempt = function(records, fn) {
|
||||
function next(i) {
|
||||
if (i >= records.length) {
|
||||
throw new Error('No more records left to try')
|
||||
}
|
||||
|
||||
return fn(records[i]).catch(srv.NEXT, function() {
|
||||
return next(++i)
|
||||
})
|
||||
}
|
||||
|
||||
return next(0)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue