-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathapp_helper.js
More file actions
119 lines (111 loc) · 3.92 KB
/
app_helper.js
File metadata and controls
119 lines (111 loc) · 3.92 KB
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
110
111
112
113
114
115
116
117
118
119
const mongoose = require('mongoose');
const _ = require('lodash');
const winston = require('winston');
const options = require('./config/mongoose_options').mongooseOptions;
// Logging middleware
const { format, transports } = winston;
winston.loggers.add('default', {
transports: [
new transports.File({
level: 'info',
filename: '/tmp/epic-app.log',
handleExceptions: true,
maxsize: 5242880, // 5MB
maxFiles: 5,
format: format.combine(
format.errors({ stack: true }),
format.splat(),
format.label({ label: 'default' }),
format.timestamp(),
format.json()
)
}),
new transports.Console({
level: 'info',
handleExceptions: true,
format: format.combine(
format.errors({ stack: true }),
format.splat(),
format.label({ label: 'default' }),
format.colorize(),
format.simple()
)
})
]
});
var defaultLog = winston.loggers.get('default');
var dbName = (process.env.MONGODB_DATABASE || 'epic');
var dbConnection = 'mongodb://'
+ (process.env.MONGODB_SERVICE_HOST || process.env.DB_1_PORT_27017_TCP_ADDR || 'localhost')
+ '/'
+ dbName;
var db_username = process.env.MONGODB_USERNAME || '';
var db_password = process.env.MONGODB_PASSWORD || '';
var credentials = {
db_username : db_username,
db_password : db_password
};
// Register all Mongoose models unconditionally at require time.
// Mongoose allows schema registration before a connection exists — models are
// bound to the default connection and become usable once connect() resolves.
// Registering outside the connect callback means models are always available
// even if the initial connect attempt is retried or delayed.
require('./api/helpers/models/audit');
require('./api/helpers/models/list');
require('./api/helpers/models/user');
require('./api/helpers/models/group');
require('./api/helpers/models/pin');
require('./api/helpers/models/organization');
require('./api/helpers/models/vc');
require('./api/helpers/models/inspectionItem');
require('./api/helpers/models/inspection');
require('./api/helpers/models/inspectionElement');
require('./api/helpers/models/project');
require('./api/helpers/models/recentActivity');
require('./api/helpers/models/document');
require('./api/helpers/models/comment');
require('./api/helpers/models/commentperiod');
require('./api/helpers/models/topic');
require('./api/helpers/models/projectNotification');
require('./api/helpers/models/cacUser');
async function loadModels(dbConnection, options, logger) {
const MAX_RETRIES = 10;
const BASE_DELAY_MS = 2000;
for (let attempt = 1; attempt <= MAX_RETRIES; attempt++) {
try {
log(logger, `Connecting to MongoDB (attempt ${attempt}/${MAX_RETRIES}): ${dbConnection}`);
await mongoose.connect(dbConnection, options);
log(logger, 'Database connected');
return; // success
} catch (err) {
log(logger, `MongoDB connect error (attempt ${attempt}): ${err.message}`);
if (attempt === MAX_RETRIES) {
log(logger, 'All MongoDB connect attempts exhausted. Exiting so Kubernetes can restart.');
process.exit(1);
}
const delay = Math.min(BASE_DELAY_MS * Math.pow(2, attempt - 1), 30000);
log(logger, `Retrying in ${delay}ms...`);
await new Promise(resolve => setTimeout(resolve, delay));
}
}
}
function log(logger, msg) {
if (!_.isEmpty(logger)) {
logger.info(msg);
} else {
console.log(msg);
}
}
async function loadMongoose() {
if (!_.isEmpty(credentials)) {
options.user = credentials.db_username;
options.pass = credentials.db_password;
}
await loadModels(dbConnection, options, defaultLog);
}
exports.loadMongoose = loadMongoose;
exports.loadModels = loadModels;
exports.dbName = dbName;
exports.dbConnection = dbConnection;
exports.credentials = credentials;
exports.defaultLog = defaultLog;