Wait for connections to MongoDB and redis to be established on startup. Added configuration option to specify redis DB.
This commit is contained in:
1 parent
28394af707
commit
1c902af363
5 files changed
+84
-47
No files matched your search
@@ -16,6 +16,7 @@
|
|||||||
###
|
###
|
||||||
|
|
||||||
exports.DATABASE_NAME = 'genieacs'
|
exports.DATABASE_NAME = 'genieacs'
|
||||||
|
exports.REDIS_DB = 0
|
||||||
exports.ACS_PORT = 7547 # CWMP port as assigned by IANA
|
exports.ACS_PORT = 7547 # CWMP port as assigned by IANA
|
||||||
exports.ACS_HTTPS_PORT = 7548
|
exports.ACS_HTTPS_PORT = 7548
|
||||||
exports.ACS_INTERFACE = '0.0.0.0'
|
exports.ACS_INTERFACE = '0.0.0.0'
|
||||||
|
|||||||
+18
-14
@@ -559,20 +559,24 @@ cluster = require 'cluster'
|
|||||||
numCPUs = require('os').cpus().length
|
numCPUs = require('os').cpus().length
|
||||||
|
|
||||||
if cluster.isMaster
|
if cluster.isMaster
|
||||||
db.redisClient.flushdb()
|
db.connect((err) ->
|
||||||
cluster.on('listening', (worker, address) ->
|
throw err if err
|
||||||
util.log("Worker #{worker.process.pid} listening to #{address.address}:#{address.port}")
|
db.redisClient.flushdb()
|
||||||
)
|
db.disconnect()
|
||||||
|
cluster.on('listening', (worker, address) ->
|
||||||
|
util.log("Worker #{worker.process.pid} listening to #{address.address}:#{address.port}")
|
||||||
|
)
|
||||||
|
|
||||||
cluster.on('exit', (worker, code, signal) ->
|
cluster.on('exit', (worker, code, signal) ->
|
||||||
util.log("Worker #{worker.process.pid} died (#{worker.process.exitCode})")
|
util.log("Worker #{worker.process.pid} died (#{worker.process.exitCode})")
|
||||||
setTimeout(()->
|
setTimeout(()->
|
||||||
|
cluster.fork()
|
||||||
|
, config.WORKER_RESPAWN_TIME)
|
||||||
|
)
|
||||||
|
|
||||||
|
for [1 .. numCPUs]
|
||||||
cluster.fork()
|
cluster.fork()
|
||||||
, config.WORKER_RESPAWN_TIME)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
for [1 .. numCPUs]
|
|
||||||
cluster.fork()
|
|
||||||
else
|
else
|
||||||
options = {
|
options = {
|
||||||
key: fs.readFileSync('../config/httpscert.key'),
|
key: fs.readFileSync('../config/httpscert.key'),
|
||||||
@@ -582,8 +586,8 @@ else
|
|||||||
httpServer = http.createServer(listener)
|
httpServer = http.createServer(listener)
|
||||||
httpsServer = https.createServer(options, listener)
|
httpsServer = https.createServer(options, listener)
|
||||||
|
|
||||||
# wait until DB connections are established
|
db.connect((err) ->
|
||||||
setTimeout(() ->
|
throw err if err
|
||||||
httpServer.listen(config.ACS_PORT, config.ACS_INTERFACE)
|
httpServer.listen(config.ACS_PORT, config.ACS_INTERFACE)
|
||||||
httpsServer.listen(config.ACS_HTTPS_PORT, config.ACS_HTTPS_INTERFACE)
|
httpsServer.listen(config.ACS_HTTPS_PORT, config.ACS_HTTPS_INTERFACE)
|
||||||
, 1000)
|
)
|
||||||
+56
-27
@@ -17,43 +17,71 @@
|
|||||||
|
|
||||||
config = require './config'
|
config = require './config'
|
||||||
mongodb = require 'mongodb'
|
mongodb = require 'mongodb'
|
||||||
redisClient = require('redis').createClient(config.REDIS_SOCKET)
|
redis = require('redis')
|
||||||
parameters = require './parameters'
|
parameters = require './parameters'
|
||||||
|
|
||||||
|
redisClient = null
|
||||||
tasksCollection = null
|
tasksCollection = null
|
||||||
devicesCollection = null
|
devicesCollection = null
|
||||||
presetsCollection = null
|
presetsCollection = null
|
||||||
objectsCollection = null
|
objectsCollection = null
|
||||||
|
|
||||||
mongodb.MongoClient.connect("mongodb://#{config.MONGODB_SOCKET}/#{config.DATABASE_NAME}", config.MONGODB_OPTIONS, (err, db) ->
|
|
||||||
exports.mongoDb = db
|
connect = (callback) ->
|
||||||
db.collection('tasks', (err, collection) ->
|
callbackCounter = 6
|
||||||
throw new Error(err) if err?
|
mongodb.MongoClient.connect("mongodb://#{config.MONGODB_SOCKET}/#{config.DATABASE_NAME}", config.MONGODB_OPTIONS, (err, db) ->
|
||||||
exports.tasksCollection = tasksCollection = collection
|
return callback(err) if err
|
||||||
collection.ensureIndex({device: 1, timestamp: 1}, (err) ->
|
exports.mongoDb = db
|
||||||
|
db.collection('tasks', (err, collection) ->
|
||||||
|
exports.tasksCollection = tasksCollection = collection
|
||||||
|
collection?.ensureIndex({device: 1, timestamp: 1}, (err) ->
|
||||||
|
)
|
||||||
|
|
||||||
|
if --callbackCounter == 0 or err
|
||||||
|
callbackCounter = 0
|
||||||
|
return callback(err)
|
||||||
|
)
|
||||||
|
|
||||||
|
db.collection('devices', (err, collection) ->
|
||||||
|
exports.devicesCollection = devicesCollection = collection
|
||||||
|
if --callbackCounter == 0 or err
|
||||||
|
callbackCounter = 0
|
||||||
|
return callback(err)
|
||||||
|
)
|
||||||
|
|
||||||
|
db.collection('presets', (err, collection) ->
|
||||||
|
exports.presetsCollection = presetsCollection = collection
|
||||||
|
if --callbackCounter == 0 or err
|
||||||
|
callbackCounter = 0
|
||||||
|
return callback(err)
|
||||||
|
)
|
||||||
|
|
||||||
|
db.collection('objects', (err, collection) ->
|
||||||
|
exports.objectsCollection = objectsCollection = collection
|
||||||
|
if --callbackCounter == 0 or err
|
||||||
|
callbackCounter = 0
|
||||||
|
return callback(err)
|
||||||
|
)
|
||||||
|
|
||||||
|
db.collection('fs.files', (err, collection) ->
|
||||||
|
exports.filesCollection = filesCollection = collection
|
||||||
|
if --callbackCounter == 0 or err
|
||||||
|
callbackCounter = 0
|
||||||
|
return callback(err)
|
||||||
|
)
|
||||||
|
|
||||||
|
exports.redisClient = redisClient = redis.createClient(config.REDIS_SOCKET)
|
||||||
|
redisClient.select(config.REDIS_DB, (err) ->
|
||||||
|
if --callbackCounter == 0 or err
|
||||||
|
callbackCounter = 0
|
||||||
|
return callback(err)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
db.collection('devices', (err, collection) ->
|
|
||||||
throw new Error(err) if err?
|
|
||||||
exports.devicesCollection = devicesCollection = collection
|
|
||||||
)
|
|
||||||
|
|
||||||
db.collection('presets', (err, collection) ->
|
disconnect = () ->
|
||||||
throw new Error(err) if err?
|
exports.mongoDb.close()
|
||||||
exports.presetsCollection = presetsCollection = collection
|
redisClient.quit()
|
||||||
)
|
|
||||||
|
|
||||||
db.collection('objects', (err, collection) ->
|
|
||||||
throw new Error(err) if err?
|
|
||||||
exports.objectsCollection = objectsCollection = collection
|
|
||||||
)
|
|
||||||
|
|
||||||
db.collection('fs.files', (err, collection) ->
|
|
||||||
throw new Error(err) if err?
|
|
||||||
exports.filesCollection = filesCollection = collection
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
getTask = (taskId, callback) ->
|
getTask = (taskId, callback) ->
|
||||||
@@ -166,7 +194,8 @@ getPresetsObjectsAliases = (callback) ->
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
exports.redisClient = redisClient
|
|
||||||
exports.getTask = getTask
|
exports.getTask = getTask
|
||||||
exports.getPresetsObjectsAliases = getPresetsObjectsAliases
|
exports.getPresetsObjectsAliases = getPresetsObjectsAliases
|
||||||
exports.getAliases = getAliases
|
exports.getAliases = getAliases
|
||||||
|
exports.connect = connect
|
||||||
|
exports.disconnect = disconnect
|
||||||
+5
-5
@@ -28,10 +28,6 @@ mongodb = require 'mongodb'
|
|||||||
querystring = require 'querystring'
|
querystring = require 'querystring'
|
||||||
|
|
||||||
db = null
|
db = null
|
||||||
mongodb.MongoClient.connect("mongodb://#{config.MONGODB_SOCKET}/#{config.DATABASE_NAME}", config.MONGODB_OPTIONS, (err, _db) ->
|
|
||||||
db = _db
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
cluster = require 'cluster'
|
cluster = require 'cluster'
|
||||||
numCPUs = require('os').cpus().length
|
numCPUs = require('os').cpus().length
|
||||||
@@ -70,4 +66,8 @@ else
|
|||||||
response.end('405 Method Not Allowed')
|
response.end('405 Method Not Allowed')
|
||||||
)
|
)
|
||||||
|
|
||||||
server.listen config.FILES_PORT, config.FILES_INTERFACE
|
mongodb.MongoClient.connect("mongodb://#{config.MONGODB_SOCKET}/#{config.DATABASE_NAME}", config.MONGODB_OPTIONS, (err, _db) ->
|
||||||
|
throw err if err
|
||||||
|
db = _db
|
||||||
|
server.listen config.FILES_PORT, config.FILES_INTERFACE
|
||||||
|
)
|
||||||
+4
-1
@@ -398,4 +398,7 @@ else
|
|||||||
response.writeHead 404
|
response.writeHead 404
|
||||||
response.end('404 Not Found')
|
response.end('404 Not Found')
|
||||||
|
|
||||||
server.listen config.API_PORT, config.API_INTERFACE
|
db.connect((err) ->
|
||||||
|
throw err if err
|
||||||
|
server.listen config.API_PORT, config.API_INTERFACE
|
||||||
|
)
|
||||||
Reference in new issue
Block a user