-
Notifications
You must be signed in to change notification settings - Fork 2
/
polling.js
67 lines (62 loc) · 1.76 KB
/
polling.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
/*
Requiring necessary helpers.
*/
var Appbase = require('appbase-js');
var helper = require("./helper.js");
/*
Including appbase credentials stored in json file.
*/
var appbase_credentials = require('./appbase_credentials.json');
/*
Appbase Credentials. Just make new account at appbase and configure it according to your account.
Creating object of appbase, passing appbase_credentials.
*/
var appbaseRef = new Appbase(appbase_credentials);
var productList = []
/*
This function is for starting polling of all the productsand storing it into the appbase databse.
The time interval of polling is set to 1000 seconds.
*/
function startPolling() {
function poll() {
setTimeout(function() {
for (productId in productList) {
console.log("Starting polling for the product with Id: " + productList[productId]);
helper.indexProduct(productId);
}
poll();
}, 1000000);
};
poll();
}
/*
Main function responsible for listing of products for whch polling is to be started.
*/
function fetchProducts() {
var requestObject = {
type: appbase_credentials.type,
body: {
query: {
match_all: {}
}
}
};
appbaseRef.search(requestObject).on('data', function(response) {
productList = response.hits.hits.map(function(hit) {
return hit._id;
});
startPolling();
appbaseRef.searchStream(requestObject).on('data', function(stream) {
console.log("Starting polling for new product streamed: " + stream._id);
productList.push(stream._id);
}).on('error', function(error) {
console.log("searchStream() failed with: ", error);
});
}).on('error', function(error) {
console.log("search() failed with: ", error)
});
}
/*
Call to the starter function.
*/
fetchProducts();