-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsimpleBatch.ts
70 lines (67 loc) · 3.01 KB
/
simpleBatch.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
///<reference path='c:\development\Netsuite\SuiteScriptAPITS.d.ts'/>
function simpleBatch(arr : any[], proc : (t :any, idx: number, a:any[]) => void, reserve? :number, maxMinutes? :number){
if(!arr || !arr.length) return;
var maxUsage = reserve || 0;
var breakTime = maxMinutes ? (new Date().getTime() + 60000*maxMinutes) : 0;
var startUsage = nlapiGetContext().getRemainingUsage();
for(var i = 0; i< arr.length; i++){
if(startUsage < (maxUsage + 20) || (breakTime && new Date().getTime() > breakTime)){
var ys = nlapiYieldScript();
if(ys.status == 'FAILURE'){
nlapiLogExecution('ERROR', "Unable to Yield "+ ys.reason, ys.information);
}
nlapiLogExecution("AUDIT", "After resume had: "+ startUsage +" remaining vs max: "+ maxUsage);
startUsage = nlapiGetContext().getRemainingUsage();
breakTime = maxMinutes ? (new Date().getTime() + 60000*maxMinutes) : 0;
}
proc(arr[i], i, arr);
if(nlapiGetContext().getExecutionContext() == "scheduled") nlapiGetContext().setPercentComplete( ((100*i)/arr.length ).toFixed(1));
var endUsage = nlapiGetContext().getRemainingUsage();
var runUsage = startUsage - endUsage;
if(maxUsage < runUsage) maxUsage = runUsage;
startUsage = endUsage;
}
}
function batchIterator(iter:()=>any, proc : (t :any, idx:number) => void, reserve? :number, maxMinutes? :number, beforeYield ?:() => void, afterYield ?:() => void){
var maxUsage = reserve || 0;
var breakTime = maxMinutes ? (new Date().getTime() + 60000*maxMinutes) : 0;
var startUsage = nlapiGetContext().getRemainingUsage();
var isScheduled = "scheduled" == nlapiGetContext().getExecutionContext();
var elemsProcessed = 0;
var elem = iter();
while(elem && typeof elem != 'undefined'){
if(startUsage < (maxUsage + 20) || (breakTime && new Date().getTime() > breakTime)){
if(!isScheduled){
nlapiLogExecution("SYSTEM", "non-Scheduled run ending with "+startUsage +" units remaining");
break;
}
if(beforeYield) beforeYield();
var ys = nlapiYieldScript();
if(ys.status == 'FAILURE'){
nlapiLogExecution('ERROR', "Unable to Yield "+ ys.reason, ys.information);
}
nlapiLogExecution("AUDIT", "After resume had: "+ startUsage +" remaining vs max: "+ maxUsage);
startUsage = nlapiGetContext().getRemainingUsage();
breakTime = maxMinutes ? (new Date().getTime() + 60000*maxMinutes) : 0;
if(afterYield) afterYield();
}
proc(elem, elemsProcessed);
elemsProcessed++;
var endUsage = nlapiGetContext().getRemainingUsage();
if(isScheduled) nlapiGetContext().setPercentComplete( (100*(startUsage - endUsage)/(startUsage)).toFixed(1));
var runUsage = startUsage - endUsage;
if(maxUsage < runUsage) maxUsage = runUsage;
startUsage = endUsage;
elem = iter();
}
}
function dynamicBatch(arr : any[], proc : (t :any, idx:number) => void, reserve? :number, maxMinutes? :number, beforeYield ?:() => void, afterYield ?:() => void){
if(!arr || !arr.length) return;
batchIterator(
function(){
if(arr.length) return arr.shift();
return null;
},
proc,
reserve, maxMinutes, beforeYield, afterYield);
}