-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
158 lines (127 loc) · 4.21 KB
/
main.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
var winston = require('winston');
var assert = require('assert');
var cluster = require('cluster');
var config = require('./config');
var db = require('./db');
var server = require('./server');
var workAsTaskProcessor = (typeof(process.env.IS_TASK_PROCESSOR)!=='undefined');
//var workAsTaskProcessor = true;
//////////// Params:
if(config.get("cluster") && cluster.isMaster && !workAsTaskProcessor) {
// Count the machine's CPUs
//var cpuCount = require('os').cpus().length + 2;
var cpuCount = config.get('cluster_nodes');
// Create a worker for each CPU
for (var i = 0; i < cpuCount; i += 1) {
console.log('-->Starting worker...');
cluster.fork();
}
// Listen for dying workers
cluster.on('exit', function (worker) {
// Replace the dead worker,
// we're not sentimental
console.log('Worker ' + worker.id + ' died :(');
cluster.fork();
});
return;
}
winston.add(winston.transports.File, {
filename: config.get('log_file_path') + config.get('service_name') + '.log'
});
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
var managePreparationForShutdown = function(callback) {
// perform all the cleanup and other operations needed prior to shutdown,
// but do not actually shutdown. Call the callback function only when
// these operations are actually complete.
winston.info('Shutting down server');
server.stop();
winston.info('Closing DB');
db.disconnectDb();
// Everything is OK
callback();
};
process.on('SIGINT', function() {
winston.info('Received SIGINT');
managePreparationForShutdown(function(){
winston.info('Stopping application');
process.exit(0);
});
});
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
// Connect to DB
var db_uri = (process.env.MONGODB_URI || config.get('db:uri'));
var db_user = (process.env.MONGODB_USER || config.get('db:user'));
var db_pass = (process.env.MONGODB_PASS || config.get('db:pass'));
db.connectToDbCallback(
db_uri,
db_user,
db_pass,
function(err){
if(!err){
winston.info("Connected to DB!");
}else{
winston.error('DB connection error:', err.message);
}
if(err){
return;
}
}
);
// Start server
var port = (process.env.PORT || config.get('http_port'));
var tm = 0;
var index = 0;
server.initDb(db);
if(workAsTaskProcessor){
console.log('Starting as a task processor');
// for tests
//addTestTasks(function(err){
tm = setTimeout(onTimeout, 1000, 'processor');
//});
}
if(config.get('enable_http')){
server.startHttp(port,function(err){
winston.info("Listening (http) on " + port);
});
}
if(config.get('enable_https')){
var https_port = config.get('https_port');
server.startHttps(https_port);
winston.info('Listening (https) on ' + https_port);
}
// If we run under root -> reduce rights
// Notice that running under root is required when we start under
// daemon/forever. Or if we use 'priveleged ports' (not recommended!)
//
// If we run this code under Docker container -> then we run
// it under 'non-root' account that is GOOD. Just set some port like 8080
// to EXPOSE from here
var nodeUserGid = config.get('process_user');
var nodeUserUid = config.get('process_group');
if(!process.getuid()){
// crash
//console.log('DO NOT RUN UNDER ROOT!!!');
//assert.equal(0,1);
console.log('WARNING: Reducing rights from ROOT to ' + nodeUserUid);
process.setgid(nodeUserGid);
process.setuid(nodeUserUid);
}
function addTestTasks(cb){
var task = new db.TaskModel;
task.fileName = 'one.csv';
task.fileNameReal = 'test1.csv';
task.status = 0;
task.organizer_inn = '1234567890';
task.save(function(err){
cb(err);
});
}
function onTimeout(){
console.log('Timeout...' + index);
index++;
server.processSingleCsvFileTask(function(err){
tm = setTimeout(onTimeout, 5000, 'processor');
});
}