-
Notifications
You must be signed in to change notification settings - Fork 1
/
batchCursorCluster.ts
122 lines (89 loc) · 3.71 KB
/
batchCursorCluster.ts
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/// <reference path="./batch.d.ts" />
import * as cluster from 'cluster';
import {BatchActivity, BatchSession} from './batch';
export abstract class BatchActivityCursorCluster<T, U extends BatchSession> extends BatchActivity<T, U> {
private offset = 0;
public numberBS = 20;
public packSize = 20;
public currentWorker = -1;
public startingTime = 0;
public selectData(callback, params?) {}
public splitData(datas) { return null; }
public abstract getCountData( callback : {(count : number): void}, params?: any ): void;
public abstract getData( starting: number, size: number ,callback : {(datas : T[]): void}, params?: any ): void;
public execute(params?: any){
this.log("Start cursor cluster");
let that = this;
if(this.packSize <= 0) this.packSize = 1
this.getCountData((count) => {
this.log(`total of elements : ${count}`);
if(count == null || count <= 0){
this.finishBA();
return;
}
var startingTime = new Date().getTime();
let nbBS = Math.min( Math.ceil(count / this.packSize), this.numberBS) ;
this.currentWorker = nbBS;
that.startingTime = new Date().getTime();
require('./cluster')(this, count, nbBS, this.packSize, params);
}, params);
}
public startBS(startIndex: number, limit: number, params?: any){
let that = this;
if(cluster.isMaster){
cluster.on('message', (messageData) => {
console.log('message from worker', messageData);
if((<any>messageData).action = 'finishBS'){
that.finishBS((<any>messageData).returningCode);
}
});
}
this.currentBS++;
let bs = this.createBatchSession();
bs.dirChaine = this.dirChaine;
that.startingTime = new Date().getTime();
var offset = startIndex;
var returningCode = 0;
let next = () => {
if(offset >= limit){ //Fin batch
this.finishBS(returningCode);
return;
}
if(that.packSize <= 0) that.packSize = 1
let startGet = offset;
offset += that.packSize;
that.getData(startGet, that.packSize, ( data ) => {
if(data == null){
that.getData(startGet, that.packSize, (dataRetry) => {
if(dataRetry == null){
next()
}else{
bs.execute(dataRetry);
}
}, params)
}else{
bs.execute(data);
}
}, params);
}
bs.on('end', (rCode) => {
returningCode = rCode;
next();
});
next();
}
public finishBS(returningCode : number){
this.log(`Finish ${cluster.isMaster ? 'master' : cluster.worker.id}`)
if(cluster.isWorker){
process.send({action: 'finishBS', returningCode});
return;
}
this.currentWorker -= 1;
if(returningCode > this.returningCode) this.returningCode = returningCode;
if(this.currentWorker <= 0){
cluster.removeAllListeners('message');
this.log('All BS cluster are finished ?');
this.emit('end', this.returningCode);
}
}
}