-
Notifications
You must be signed in to change notification settings - Fork 6
/
vkWorker.js
191 lines (148 loc) · 4.47 KB
/
vkWorker.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
"use strict";
var Worker = {
setup: function() {
self.onmessage = this.onMessage.bind(this);
self.callback = this.runLoop.bind(this);
}
,busy : false
,ids : []
,started : 0
,waited : 0
,eta : 0
,times : [] // last 3 api call times
,samples : [] // estimates for a total job time
,onMessage: function(e) {
var mandatory = "oid,mass,token,code,v".split(','), prop;
if( !e.data) {
postMessage({ error: "Missing event data"});
throw "Missing event data";
return;
}
// verify all properties are in place
for( prop=0; prop<mandatory.length; prop++) {
if( !e.data.hasOwnProperty( mandatory[prop])) {
postMessage({ error: "missing property " + mandatory[prop] });
return;
}
}
// is worker available?
if( this.busy) {
postMessage({ error: "Worker busy"});
return;
}
this.init( e.data);
this.runLoop();
}
/**
* Reset prior each new group run
*/
,init: function( props) {
this.busy = true;
this.started = (new Date()).getTime();
this.ids = [];
this.waited = 0;
this.oid = props.oid > 0 ? props.oid : -props.oid;
this.token = props.token;
this.mass = props.mass ? props.mass : 0;
this.code = encodeURIComponent( props.code.replace(/\s+/g,' '));
this.v = props.v;
this.offset = 0;
}
// in case of bad errors
,abort: function( msg, r) {
console.log( msg, r);
postMessage({abort: 1, msg: msg});
self.close();
return;
}
,cbSum: function(sum, val) { return sum + val; }
,runLoop: function(r) {
var loop, from, lastId, lengthBefore, url, now, diff, progress, eta;
now = (new Date()).getTime();
if(r) { // VK api call returned smth
// respect VK limit of max 3 api calls within 1 second
this.times.push(now);
this.times = this.times.slice(-3);
//console.log("runLoop got", r, "offset:"+this.offset, "mass:"+this.mass);
// check for errors
if( r.execute_errors) return this.abort("Execute errors", r);
if( r.error) {
if( r.error.error_code == 6) {
console.log("Times:", this.times);
}
return this.abort("Error", r, this.times);
}
if( !r.response) {
return this.abort("No response property", r);
} else {
if( r.response.ids && r.response.ids.length>0) {
lengthBefore = this.ids.length;
for( loop=0; loop < r.response.ids.length; loop++) { // max 25
if( this.ids.length) {
lastId = this.ids[ this.ids.length - 1];
from = r.response.ids[ loop].indexOf( lastId);
if( from == -1) {
console.log('Last id ' + lastId + ' was not found in loop ', loop, this.ids, r.response.ids[loop]);
this.ids = this.ids.concat( r.response.ids[ loop]);
} else {
this.ids = this.ids.concat( r.response.ids[ loop].slice( from + 1));
}
} else {
this.ids = r.response.ids[ loop].slice();
}
}
this.offset = r.response.next;
}
if( r.response.mass) this.mass = r.response.mass;
// Before next api call
now = (new Date()).getTime();
// Progress and ETA
if( this.mass) {
progress = this.offset / this.mass;
if( this.offset) {
this.samples.push( this.mass * ( now - this.started) / this.offset);
this.samples = this.samples.slice(-5);
eta = Math.round(((this.samples.reduce( this.cbSum, 0) / this.samples.length) - now + this.started) / 1000);
} else {
eta = 0;
}
postMessage({
progress : progress,
eta : eta,
mass : this.mass,
});
}
if( this.offset >= this.mass) { // done
console.log('Worker done.');
postMessage({ids: this.ids, elapsed: (new Date()).getTime() - this.started, slept: this.waited});
self.close();
return;
}
}
}
// jsonp call to VK API
url = 'https://api.vk.com/method/execute'
+'?access_token=' + this.token
+'&oid=' + this.oid
+'&offset=' + this.offset
// TODO overlap must depend on the offset size: min 2, growing up to 10 for a 1E6 offset
+'&overlap=' + 2 // this.offset > 0 ? Math.floor( Math.log( this.offset)) : 0
+'&mass=' + this.mass
+'&code=' + this.code
+'&v=' + this.v
+'&callback=callback'
;
if( this.times.length >= 3) {
diff = 1100 - (new Date()).getTime() + this.times[0];
if( diff > 0) {
diff += 100; // just in case
this.waited += diff;
self.setTimeout( this.runLoop.bind(this), diff); // wait
return;
}
}
// do the jsonp VK API call
setTimeout( function(){ importScripts( url)}, 0);
}
};
Worker.setup();