-
Notifications
You must be signed in to change notification settings - Fork 0
/
clListings.js
94 lines (71 loc) · 2.11 KB
/
clListings.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
var express = require('express');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var async = require('async');
var mongoose = require('mongoose');
var db = mongoose.connect('mongodb://localhost/clListings');
var Listing = require('./models/listing');
var importantFns = require('./importantFns');
var ebaySearch = require('./ebaySearch');
var port = process.env.PORT || 5000; // Use the port that Heroku
server.listen(port);
console.log('listening on ' + port);
app.use(express.static(__dirname + '/public'));
var cronJobSearches = [];
importantFns.getAllCRONS(function(crons) {
console.log(crons);
var needsToRun = [];
crons.forEach(function(cron) {
if (cron.nextRun < Date.now()) {
needsToRun.push(cron);
}
});
console.log('needs', needsToRun);
async.forEachSeries(needsToRun, function(cron, cb) {
importantFns.runSearch(cron.params, {roomName: cron.socketRoom, io: io}, function() {
console.log('done waiting 1000 then next');
setTimeout(cb, 1000);
});
});
});
io.sockets.on('connection', function (socket) {
console.log('connected socket');
var log = function(s) {
socket.emit('info', s);
console.log(s);
};
socket.on('getBestDealsInSection', function(searchQuery) {
importantFns.getBestDealsInSection(searchQuery, function(data) {
socket.emit('bestDealsInSection', {
deals: data,
sec: searchQuery.section,
query: searchQuery.searchText
});
});
});
socket.on('queryCl', function(params) {
importantFns.runSearch(params, {socket: socket, io: io});
});
socket.on('ignore', function(data) {
Listing.update(data, {
ignore: true
}, function(err) {
if (err) throw err;
});
});
socket.on('joinCRONS', function(arr) {
arr.forEach(function(cronId) {
console.log('joined ' + cronId);
socket.join(cronId);
});
});
socket.on('cancelCron', function(id) {
console.log('canceling', id);
importantFns.cancelCron(id, function(err) {
if (!err) {
socket.emit('successCancel', id);
}
});
});
});