forked from polonel/trudesk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
246 lines (206 loc) · 8.2 KB
/
app.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/usr/bin/env node
/*
. .o8 oooo
.o8 "888 `888
.o888oo oooo d8b oooo oooo .oooo888 .ooooo. .oooo.o 888 oooo
888 `888""8P `888 `888 d88' `888 d88' `88b d88( "8 888 .8P'
888 888 888 888 888 888 888ooo888 `"Y88b. 888888.
888 . 888 888 888 888 888 888 .o o. )88b 888 `88b.
"888" d888b `V88V"V8P' `Y8bod88P" `Y8bod8P' 8""888P' o888o o888o
========================================================================
**/
var async = require('async'),
path = require('path'),
fs = require('fs'),
winston = require('winston'),
nconf = require('nconf'),
pkg = require('./package.json'),
ws = require('./src/webserver');
//`var memory = require('./src/memory');
global.forks = [];
nconf.argv().env();
global.env = process.env.NODE_ENV || 'development';
//global.env = process.env.NODE_ENV || 'production';
winston.setLevels(winston.config.cli.levels);
winston.remove(winston.transports.Console);
winston.add(winston.transports.Console, {
colorize: true,
timestamp: function() {
var date = new Date();
return (date.getMonth() + 1) + '/' + date.getDate() + ' ' + date.toTimeString().substr(0,8) + ' [' + global.process.pid + ']';
},
level: global.env === 'production' ? 'info' : 'verbose'
});
winston.add(winston.transports.File, {
filename: 'logs/error.log',
level: 'error'
});
winston.err = function (err) {
winston.error(err.stack);
};
process.on('message', function(msg) {
if (msg === 'shutdown') {
winston.debug('Closing all connections...');
if (ws.server)
ws.server.close();
throw new Error('Server has shutdown.');
}
});
if (!process.env.FORK) {
winston.info(' . .o8 oooo');
winston.info(' .o8 "888 `888');
winston.info('.o888oo oooo d8b oooo oooo .oooo888 .ooooo. .oooo.o 888 oooo');
winston.info(' 888 `888""8P `888 `888 d88\' `888 d88\' `88b d88( "8 888 .8P\'');
winston.info(' 888 888 888 888 888 888 888ooo888 `"Y88b. 888888.');
winston.info(' 888 . 888 888 888 888 888 888 .o o. )88b 888 `88b.');
winston.info(' "888" d888b `V88V"V8P\' `Y8bod88P" `Y8bod8P\' 8""888P\' o888o o888o');
winston.info('==========================================================================');
winston.info('trudesk v' + pkg.version + ' Copyright (C) 2014-2018 Chris Brame');
winston.info('');
winston.info('Running in: ' + global.env);
winston.info('Server Time: ' + new Date());
}
var configFile = path.join(__dirname, '/config.json'),
configExists;
if (nconf.get('config'))
configFile = path.resolve(__dirname, nconf.get('config'));
configExists = fs.existsSync(configFile);
if (process.env.HEROKU) {
//Build Config for Heroku
var configHeroku = {
'url': 'http://localhost:8118',
'port': '8118'
};
winston.info('Creating heroku config file...');
var config = JSON.stringify(configHeroku, null, 4);
if (configExists)
fs.unlinkSync(configFile);
fs.writeFileSync(configFile, config);
start();
}
if (nconf.get('install') || !configExists && !process.env.HEROKU) {
ws.installServer(function() {
return winston.info('Trudesk Install Server Running...');
});
}
if (!nconf.get('setup') && !nconf.get('install') && !nconf.get('upgrade') && !nconf.get('reset') && configExists)
start();
function loadConfig() {
nconf.file({
file: configFile
});
nconf.defaults({
base_dir: __dirname
});
}
function start() {
loadConfig();
var _db = require('./src/database');
_db.init(function(err, db) {
if (err) {
winston.error('FETAL: ' + err.message);
winston.warn('Retrying to connect to MongoDB in 10secs...');
return setTimeout(function() {
_db.init(dbCallback);
}, 10000);
} else
dbCallback(err, db);
});
}
function dbCallback(err, db) {
if (err)
return start();
ws.init(db, function(err) {
if (err) {
winston.error(err);
return;
}
async.series([
function(next) {
require('./src/socketserver')(ws);
return next();
},
function(next) {
//Start Check Mail
var settingSchema = require('./src/models/setting');
settingSchema.getSetting('mailer:check:enable', function(err, mailCheckEnabled) {
if (err) {
winston.warn(err);
return next();
}
if (mailCheckEnabled && mailCheckEnabled.value) {
settingSchema.getSettings(function(err, settings) {
if (err) return next();
var mailCheck = require('./src/mailer/mailCheck');
winston.debug('Starting MailCheck...');
mailCheck.init(settings);
});
}
return next();
});
},
function(next) {
require('./src/settings/defaults').init(next);
},
function(next) {
winston.debug('Building dynamic sass...');
require('./src/sass/buildsass').build(next);
},
function(next) {
//Start Task Runners
require('./src/taskrunner');
return next();
},
function(next) {
//var pm2 = require('pm2');
//pm2.connect(true, function(err) {
// if (err) throw err;
// pm2.start({
// script: path.join(__dirname, '/src/cache/index.js'),
// name: 'trudesk:cache',
// output: path.join(__dirname, '/logs/cache.log'),
// error: path.join(__dirname, '/logs/cache.log'),
// env: {
// FORK: 1,
// NODE_ENV: global.env
// }
// }, function(err) {
// pm2.disconnect();
// if (err) throw err;
//
// process.on('message', function(message) {
// if (message.data.cache) {
// var nodeCache = require('./src/cache/node-cache');
// global.cache = new nodeCache({
// data: message.data.cache.data,
// checkperiod: 0
// });
// }
// });
//
// next();
// });
//});
var fork = require('child_process').fork;
var n;
if (process.env.MONGOHQ_URL)
n = fork(path.join(__dirname, '/src/cache/index.js'), { execArgv: ['--max-old-space-size=4096'], env: { FORK: 1, NODE_ENV: global.env, MONGOHQ_URL: process.env.MONGOHQ_URL } } );
else
n = fork(path.join(__dirname, '/src/cache/index.js'), { execArgv: ['--max-old-space-size=4096'], env: { FORK: 1, NODE_ENV: global.env } } );
global.forks.push({name: 'cache', fork: n});
n.on('message', function(data) {
if (data.cache) {
var NodeCache = require('./src/cache/node-cache');
global.cache = new NodeCache({
data: data.cache.data,
checkperiod: 0
});
}
});
return next();
}
], function() {
winston.info('trudesk Ready');
});
});
}