-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathloopjob.js
126 lines (108 loc) · 3.64 KB
/
loopjob.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
var LoopJob = function () {
var logutil = require("./logutil").config('loopjob');
var simplehttp = require('./simplehttp');
var loopCounter = 0;
var errorCounter = 0;
var jobStatus = {
url: null,
httpMethod: "GET",
parallelRequests: 1,
loopStarted: false,
loopPaused: false,
lastRequestTime: 0,
isRequestSending: false,
loopInterval: 500,
intervalObj: null,
timeout: 500,
urlInjection: function (parallelIndex, url) {
return url
},
optionsInjection: function (parallelIndex, options) {
return options
},
responseHandler: null
};
var parallelRequests = 1;
function loopIntervalHandler() {
// logutil.log("loopIntervalHandler started...")
for (var i = 0; i < jobStatus.parallelRequests; i++) {
loopWork(i);
}
}
function loopWork(parallelIndex) {
if (jobStatus.isRequestSending) {
return;
}
if (jobStatus.loopPaused) {
//logutil.log("loopPaused!")
return;
}
var timeStemp = new Date().getTime();
jobStatus.lastRequestTime = timeStemp;
var url = jobStatus.urlInjection(parallelIndex, jobStatus.url, jobStatus);
jobStatus.isRequestSending = true;
loopCounter++;
if (loopCounter % 1000 === 0) {
// console.log("loopWork...", loopCounter);
logutil.warn("loopjob loopWork :", loopCounter, errorCounter, jobStatus.url);
}
var options = {
"timeout": jobStatus.timeout,
"cookieJar": null
};
jobStatus.optionsInjection(parallelIndex, options);
var startTime = new Date();
simplehttp[jobStatus.httpMethod](url, options, function (error, response, body) {
// logutil.log("loopjob duraiton", new Date()-startTime, error, body);
if (error) {
errorCounter++;
}
jobStatus.isRequestSending = false;
jobStatus.responseHandler(error, response, body);
});
};
this.config = function (options) {
for (var att in options) {
jobStatus[att] = options[att];
}
return this;
}
this.startLooping = function () {
if (jobStatus.loopStarted) {
console.log("startLoanLoop: Already started!");
return;
}
jobStatus.loopStarted = true;
jobStatus.intervalObj = setInterval(loopIntervalHandler, jobStatus.loopInterval);
return this;
}
this.stopLooping = function () {
if (!jobStatus.loopStarted) {
console.log("stopLooping: Already stopped!");
return this;
}
jobStatus.loopStarted = false;
clearInterval(jobStatus.intervalObj);
return this;
}
this.isLoopingStarted = function () {
return jobStatus.loopStarted;
}
this.pause = function (msd, info) {
if (this.timeoutObj) clearTimeout(this.timeoutObj)
if (!msd) {
logutil.warn("job pause===========end", msd, new Date().toTimeString(), info);
jobStatus.loopPaused = false;
this.timeoutObj = null;
return;
}
logutil.warn("job pause===========", msd, new Date().toTimeString(), info);
jobStatus.loopPaused = true;
this.timeoutObj = setTimeout(function () {
logutil.warn("job pause===========end", new Date().toTimeString(), info);
jobStatus.loopPaused = false;
this.timeoutObj = null;
}, msd)
}
}
module.exports = LoopJob;