-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
96 lines (87 loc) · 3.33 KB
/
app.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
var _ = require('underscore');
var Promise = require('bluebird');
var Worker = require('./mi5-modules/worker');
Worker.prototype.accept = function(){
var self = this;
return Promise.all([self.getInProgressOrders(),
self.getAcceptedOrders(),
self.getPendingOrders()
.then(self.filterOrdersAccordingConfig)])
.spread(function(ordersInProgress,acceptedOrders,filteredOrders){
var promise;
if (filteredOrders.length>0){ //(ordersInProgress.length + acceptedOrders.length) < maxOrdersProcessing && filteredOrders.length>0){
console.log("Accepting one order.");
promise = self.getPendingOrders()
.then(self.filterOrdersAccordingConfig)
.then(self.selectOneOrderByIncomingOrder)
.then(self.computeTimeUntilCompletion)
.spread(self.acceptOrder)
.catch(TypeError, function (err) {
console.log('catchedTypeError', err);
})
.catch(function (err) {
console.log('error accepting orders: ', err);
});
}
else{
console.log("No orders to accept... waiting");
promise = Promise.resolve();
}
return promise;
}) // spread
.catch(function(err){
console.log("Error in accept: " +err);
})
.delay(1000)
.then(function(){
return self.accept();
});
}; // accept
Worker.prototype.execute = function(){
var self = this;
var maxOrdersProcessing = this.config.processing.maxOrdersProcessing;
return Promise.all([self.getInProgressOrders(),
self.getAcceptedOrders()])
.spread(function(ordersInProgress,acceptedOrders){
var promise;
if (acceptedOrders.length>0 && ordersInProgress.length<maxOrdersProcessing){
// Execute order
promise = self.selectOneOrderByIncomingOrder(acceptedOrders)
.then(function(order){
console.log("Setting order " + order.orderId + " in progress.");
return Promise.all([
self.setInProgressToOrder(order),
self.executeOrder(order)]);
})
.then(function(results){
console.log('afterSetInProgress and after execute:', results);
});
}
else if (acceptedOrders.length>0){
console.log("Already " + ordersInProgress.length + " orders in progress... waiting");
promise = Promise.resolve();
}
else {
// No order to be executed
console.log("No order to be executed... waiting");
promise = Promise.resolve();
}
return promise;
})
.catch(TypeError, function(err){
console.log('Execute: CatchedTypeError', err);
})
.catch(function(err){
console.log('Execute: Error', err);
})
.delay(1000)
.then(function(){
return self.execute();
});
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////
var CLW = new Worker(); // CloudLinkWorker
CLW.accept();
CLW.execute();