-
Notifications
You must be signed in to change notification settings - Fork 204
/
server.js
executable file
·111 lines (90 loc) · 3.27 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
'use strict';
var mongoose = require('mongoose');
var async = require('async');
var dbsObject = {};
var models = require('./helpers/models')(dbsObject);
var dbsNames = {};
var connectOptions;
var mainDb;
var app;
require('pmx').init();
process.env.NODE_ENV = process.env.NODE_ENV || 'production';
require('./config/environment/' + process.env.NODE_ENV);
connectOptions = {
db : {native_parser: true},
server: {poolSize: 5},
w : 1,
j : true
};
mainDb = mongoose.createConnection(process.env.MAIN_DB_HOST, process.env.MAIN_DB_NAME, process.env.DB_PORT, connectOptions);
mainDb.on('error', function (err) {
err = err || 'connection error';
console.error(err);
process.exit(1, err);
});
mainDb.once('open', function callback() {
var mainDBSchema;
var port = parseInt(process.env.PORT, 10) || 8089;
var instance = parseInt(process.env.NODE_APP_INSTANCE, 10) || 0;
var main;
port += instance;
mainDb.dbsObject = dbsObject;
dbsObject.mainDB = mainDb;
console.log('Connection to mainDB is success');
require('./models/index.js');
mainDBSchema = mongoose.Schema({
_id : Number,
url : {type: String, default: 'localhost'},
DBname: {type: String, default: ''},
pass : {type: String, default: ''},
user : {type: String, default: ''},
port : Number
}, {collection: 'easyErpDBS'});
main = mainDb.model('easyErpDBS', mainDBSchema);
main.find().exec(function (err, result) {
if (err) {
process.exit(1, err);
}
async.each(result, function (_db, eachCb) {
var dbInfo = {
DBname: '',
url : ''
};
var opts = {
db : {native_parser: true},
server: {poolSize: 5},
w : 1,
j : true
};
var dbObject = mongoose.createConnection(_db.url, _db.DBname, _db.port, opts);
dbObject.on('error', function (err) {
console.error(err);
eachCb(err);
});
dbObject.once('open', function () {
console.log('Connection to ' + _db.DBname + ' is success');
dbInfo.url = _db.url;
dbInfo.DBname = _db.DBname;
dbsObject[_db.DBname] = dbObject;
dbsNames[_db.DBname] = dbInfo;
eachCb();
});
}, function (err) {
if (err) {
return console.error(err);
}
app = require('./app')(mainDb, dbsNames);
app.listen(port, function () {
var Scheduler = require('./services/scheduler')(models);
var scheduler = new Scheduler(dbsObject);
console.log('==============================================================');
console.log('|| server instance=' + instance + ' start success on port=' + port + ' in ' + process.env.NODE_ENV + ' version ||');
console.log('==============================================================\n');
if (result.length > 0) {
scheduler.initEveryDayScheduler();
}
});
});
});
mainDb.mongoose = mongoose;
});