-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
74 lines (59 loc) · 2.18 KB
/
index.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
'use strict';
// Prepare configuration
var nconf = require('nconf');
nconf.argv()
.env()
.file({file: './config.hjson', format: require('hjson').rt});
// Initialize logger with specified configuration
global.logger = require('./lib/logger');
var Pool = require('./lib/pool'),
Worker = require('./lib/Worker'),
TorrentsQueue = require('./lib/TorrentsQueue'),
torrentsService = require('./lib/torrentsService');
logger.info('Starting up application');
nextIteration(); // start it
function nextIteration() {
logger.debug('nextIteration');
var workersCount = Number(nconf.get('app:workers')),
workersBusyCount = workersCount,
workers = [],
startTime = Date.now();
var pool = Pool.create(workersCount,
nconf.get('database:host'),
nconf.get('database:name'),
nconf.get('database:user'),
nconf.get('database:password'));
torrentsService.fetchAll(pool, function (err, torrents) {
if (err) {
logger.error(err);
process.exit(1);
}
var torrentsQueue = new TorrentsQueue(torrents);
for (var i = 0, worker; i < workersCount; ++i) {
worker = new Worker(i, pool, torrentsQueue, torrentsService);
worker.start(workerOnDoneCallback);
workers.push(worker);
}
});
function workerOnDoneCallback() {
workersBusyCount -= 1;
if (workersBusyCount == 0) {
next(); // that's all
}
}
function next() {
logger.debug('All workers are done');
torrentsService.flushBuffer(pool, function () {
pool.end(function (err) {
if (err) logger.error(err);
workers = pool = null;
logger.debug('Iteration time:', Math.floor((Date.now() - startTime) / 1000), 'seconds');
var sleepTime = Number(nconf.get('app:sleepTime'));
if (sleepTime > 0) {
return setTimeout(nextIteration, sleepTime * 1000 * 60);
}
process.nextTick(nextIteration);
});
});
}
}