-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
67 lines (56 loc) · 1.74 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
const db = require('./models/db').db;
const Person = require('./models/person')
const child_process = require('child_process');
const Card = require('./models/card');
const kue = require('kue');
const queue = kue.createQueue();
kue.app.listen(8001);
const moment = require('moment');
let avg = 0;
let i = 0;
let sum = 0;
queue.on('job enqueue', function(id, type) {
console.log('Job %s got queued of type %s', id, type);
});
const processAggregate = () => {
return new Promise((resolve,reject) => {
const cardQuery = Card.aggregate([{$group: {_id: null,total: {$sum: "$number"}}}])
cardQuery.option({allowDiskUse: true});
cardQuery.exec((err, res) => {
if (err) return reject(err);
resolve(res);
});
});
};
queue.process('aggregate',32,(job,done) => {
let time = moment();
processAggregate().then(res => {
console.log(res);
i++;
sum += Math.round(moment().diff(time) / 1000);
avg = sum / i;
console.log(`compute time: ${Math.round(moment().diff(time) / 1000)} avg: ${avg} job id: ${job.id}`);
done(null,res)
}).catch(err => done(err));
});
// Card.find({}).exec((err,res) => {
// if(err) return console.log(err);
// console.log("MAIN THREAD CARDS",res)
// });
// let personObj = new Person({
// name: 'Test',
// mobile: '1234567890'
// })
// personObj.save();
// console.log(db)
const Thread = child_process.fork('./thread');
const SecondThread = child_process.fork('./anotherThread');
Person.find({}).exec((err,res) => {
if(err) return console.log(err)
console.log(res)
});
const aggregateToDb = () => {
// console.log('setinterval')
queue.create('aggregate',{title: 'aggregate'}).on('failed',() => console.log('Error')).removeOnComplete(true).save();
};
setInterval(aggregateToDb,500);