@zwa73/utils 1.0.45 → 1.0.47
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/UtilFunctions.d.ts +8 -1
- package/dist/UtilFunctions.js +57 -35
- package/dist/UtilInterfaces.d.ts +0 -9
- package/package.json +1 -1
- package/src/UtilCom.ts +1 -1
- package/src/UtilFunctions.ts +71 -37
- package/src/UtilInterfaces.ts +0 -10
package/dist/UtilFunctions.d.ts
CHANGED
|
@@ -51,6 +51,13 @@ export declare namespace UtilFunc {
|
|
|
51
51
|
}>;
|
|
52
52
|
/**获得一个永不完成的Promise单例 */
|
|
53
53
|
function getNeverResolvedPromise<T>(): Promise<T>;
|
|
54
|
-
/**重复尝试promise
|
|
54
|
+
/**重复尝试promise
|
|
55
|
+
* @async
|
|
56
|
+
* @param {PromiseProcFn<T>} [procFn] - 发起函数
|
|
57
|
+
* @param {PromiseVerifyFn<T>} [verifyFn] - 验证函数
|
|
58
|
+
* @param {number} [repeatCount] - 重试次数
|
|
59
|
+
* @param {number} [repeatTime] - 超时时间/秒 最小为10秒
|
|
60
|
+
* @returns {Promise<T|null>} - 结果 null 为全部失败/超时
|
|
61
|
+
*/
|
|
55
62
|
function repeatPromise<T>(procFn: PromiseProcFn<T>, verifyFn?: PromiseVerifyFn<T>, repeatCount?: number, repeatTime?: number): Promise<T | null>;
|
|
56
63
|
}
|
package/dist/UtilFunctions.js
CHANGED
|
@@ -99,8 +99,18 @@ var UtilFunc;
|
|
|
99
99
|
return NeverResolvedPromise;
|
|
100
100
|
}
|
|
101
101
|
UtilFunc.getNeverResolvedPromise = getNeverResolvedPromise;
|
|
102
|
-
/**重复尝试promise
|
|
102
|
+
/**重复尝试promise
|
|
103
|
+
* @async
|
|
104
|
+
* @param {PromiseProcFn<T>} [procFn] - 发起函数
|
|
105
|
+
* @param {PromiseVerifyFn<T>} [verifyFn] - 验证函数
|
|
106
|
+
* @param {number} [repeatCount] - 重试次数
|
|
107
|
+
* @param {number} [repeatTime] - 超时时间/秒 最小为10秒
|
|
108
|
+
* @returns {Promise<T|null>} - 结果 null 为全部失败/超时
|
|
109
|
+
*/
|
|
103
110
|
async function repeatPromise(procFn, verifyFn, repeatCount = 3, repeatTime = 180) {
|
|
111
|
+
//计时
|
|
112
|
+
const timeflag = "repeatPromise " + UtilFunc.genUUID();
|
|
113
|
+
UtilLogger_1.SLogger.time(timeflag);
|
|
104
114
|
//转换为毫秒
|
|
105
115
|
const hasRepeatTime = (repeatTime >= 10);
|
|
106
116
|
if (hasRepeatTime)
|
|
@@ -123,58 +133,70 @@ var UtilFunc;
|
|
|
123
133
|
resolveFn = null;
|
|
124
134
|
};
|
|
125
135
|
//进行中的请求
|
|
126
|
-
const plist =
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
136
|
+
const plist = [];
|
|
137
|
+
//开始处理
|
|
138
|
+
try {
|
|
139
|
+
for (let i = 0; i < repeatCount;) {
|
|
140
|
+
UtilLogger_1.SLogger.info(`开始第 ${i + 1} 次 repeatPromise`);
|
|
141
|
+
//创建当前任务
|
|
142
|
+
if (plist.length < i + 1) {
|
|
143
|
+
plist.push(procFn().then(result => ({ result, stat: verifyFn(result), index: i })));
|
|
144
|
+
}
|
|
145
|
+
//创建定时器
|
|
146
|
+
if (timerP == null) {
|
|
147
|
+
timerP = new Promise(function (resolve, rejecte) {
|
|
148
|
+
resolveFn = resolve;
|
|
149
|
+
timer = setTimeout(() => resolve("Timeout"), hasRepeatTime ? repeatTime : Infinity); //无限制则无限时间
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
//等待完成
|
|
153
|
+
const currObj = await Promise.race([...plist, timerP]);
|
|
154
|
+
//超时处理
|
|
155
|
+
if (currObj == "Timeout") {
|
|
156
|
+
UtilLogger_1.SLogger.warn(`第 ${i + 1} 次 repeatPromise 超时 ${repeatTime} ms 开始重试`);
|
|
157
|
+
clearTimer();
|
|
158
|
+
i++;
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
149
161
|
const poststat = await currObj.stat;
|
|
150
162
|
switch (poststat) {
|
|
151
163
|
case "Completed": //完成
|
|
152
|
-
UtilLogger_1.SLogger.info(
|
|
153
|
-
|
|
164
|
+
UtilLogger_1.SLogger.info(`第 ${currObj.index + 1} 次 repeatPromise 成功`);
|
|
165
|
+
//非当前
|
|
166
|
+
if (currObj.index != i)
|
|
167
|
+
UtilLogger_1.SLogger.info(`成功的 promise 非当前 promise 考虑增大重试间隔\n当前index: ${i}\n当前间隔: ${repeatTime}`);
|
|
154
168
|
return currObj.result;
|
|
155
169
|
case "Terminated": //终止
|
|
156
|
-
UtilLogger_1.SLogger.warn(
|
|
157
|
-
clearTimer();
|
|
170
|
+
UtilLogger_1.SLogger.warn(`第 ${currObj.index + 1} 次 repeatPromise 终止 停止重试`);
|
|
158
171
|
return currObj.result;
|
|
159
172
|
case "Failed": //验证失败
|
|
173
|
+
//抛弃失败
|
|
174
|
+
plist[currObj.index] = UtilFunc.getNeverResolvedPromise();
|
|
160
175
|
//是当前
|
|
161
176
|
if (currObj.index == i) {
|
|
162
|
-
UtilLogger_1.SLogger.warn(
|
|
177
|
+
UtilLogger_1.SLogger.warn(`第 ${currObj.index + 1} 次 repeatPromise 失败 开始重试`);
|
|
163
178
|
clearTimer();
|
|
164
179
|
i++;
|
|
165
180
|
continue;
|
|
166
181
|
}
|
|
167
182
|
//非当前
|
|
168
|
-
UtilLogger_1.SLogger.warn(
|
|
169
|
-
//抛弃失败
|
|
170
|
-
plist[currObj.index] = UtilFunc.getNeverResolvedPromise();
|
|
183
|
+
UtilLogger_1.SLogger.warn(`第 ${currObj.index + 1} 次 repeatPromise 失败`);
|
|
171
184
|
continue;
|
|
172
185
|
}
|
|
173
186
|
}
|
|
187
|
+
//全部失败或超时则返回null
|
|
188
|
+
UtilLogger_1.SLogger.warn(`${repeatCount} 次 repeatPromise 尝试均失败`);
|
|
189
|
+
return null;
|
|
190
|
+
}
|
|
191
|
+
catch (err) {
|
|
192
|
+
UtilLogger_1.SLogger.warn(`repeatPromise 发生错误`, err);
|
|
193
|
+
return null;
|
|
194
|
+
}
|
|
195
|
+
finally {
|
|
196
|
+
//清理
|
|
197
|
+
clearTimer();
|
|
198
|
+
UtilLogger_1.SLogger.timeEnd(timeflag);
|
|
174
199
|
}
|
|
175
|
-
clearTimer();
|
|
176
|
-
//全部失败或超时则返回null
|
|
177
|
-
return null;
|
|
178
200
|
}
|
|
179
201
|
UtilFunc.repeatPromise = repeatPromise;
|
|
180
202
|
})(UtilFunc = exports.UtilFunc || (exports.UtilFunc = {}));
|
package/dist/UtilInterfaces.d.ts
CHANGED
|
@@ -54,15 +54,6 @@ type ExclusiveRecursive<B, T, K extends string[], R extends unknown[] = []> = R[
|
|
|
54
54
|
export type ExclusiveRecord<B, T, K extends string[], TMP = ExclusiveRecursive<B, T, K>> = TMP[keyof TMP];
|
|
55
55
|
/**符合JObject约束的互斥表 */
|
|
56
56
|
export type ExclusiveJObject<B extends JObject, T extends JToken, K extends string[], TMP extends JArray = ExclusiveRecursive<B, T, K>> = TMP[number];
|
|
57
|
-
/**进行中的请求 */
|
|
58
|
-
export type ProcessingPromise<T> = {
|
|
59
|
-
/**主体请求 */
|
|
60
|
-
result: T;
|
|
61
|
-
/**请求状态 */
|
|
62
|
-
stat: PromiseStat | Promise<PromiseStat>;
|
|
63
|
-
/**请求下标/序号 */
|
|
64
|
-
index: number;
|
|
65
|
-
};
|
|
66
57
|
/**请求完成状态 成功/失败/终止
|
|
67
58
|
* 成功/终止 将直接返回
|
|
68
59
|
* 失败 将重试
|
package/package.json
CHANGED
package/src/UtilCom.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { JObject,
|
|
1
|
+
import { JObject, PromiseVerifyFn, stringifyJToken } from "./UtilInterfaces";
|
|
2
2
|
import * as https from 'https';
|
|
3
3
|
import * as http from 'http';
|
|
4
4
|
import { SLogger } from "./UtilLogger";
|
package/src/UtilFunctions.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as crypto from "crypto";
|
|
2
|
-
import { JToken,
|
|
2
|
+
import { JToken, PromiseProcFn, PromiseStat, PromiseVerifyFn } from "./UtilInterfaces";
|
|
3
3
|
import * as cp from "child_process";
|
|
4
4
|
import { SLogger } from "./UtilLogger";
|
|
5
5
|
|
|
@@ -98,9 +98,31 @@ export function getNeverResolvedPromise<T>():Promise<T>{
|
|
|
98
98
|
return NeverResolvedPromise as Promise<T>;
|
|
99
99
|
}
|
|
100
100
|
|
|
101
|
-
|
|
101
|
+
/**进行中的请求 */
|
|
102
|
+
type ProcessingPromise<T> = {
|
|
103
|
+
/**主体请求 */
|
|
104
|
+
result:T;
|
|
105
|
+
/**请求状态 */
|
|
106
|
+
stat:PromiseStat|Promise<PromiseStat>;
|
|
107
|
+
/**请求下标/序号 */
|
|
108
|
+
index:number;
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
/**重复尝试promise
|
|
112
|
+
* @async
|
|
113
|
+
* @param {PromiseProcFn<T>} [procFn] - 发起函数
|
|
114
|
+
* @param {PromiseVerifyFn<T>} [verifyFn] - 验证函数
|
|
115
|
+
* @param {number} [repeatCount] - 重试次数
|
|
116
|
+
* @param {number} [repeatTime] - 超时时间/秒 最小为10秒
|
|
117
|
+
* @returns {Promise<T|null>} - 结果 null 为全部失败/超时
|
|
118
|
+
*/
|
|
102
119
|
export async function repeatPromise<T>(procFn:PromiseProcFn<T>,verifyFn?:PromiseVerifyFn<T>,
|
|
103
120
|
repeatCount:number=3,repeatTime:number=180):Promise<T|null>{
|
|
121
|
+
|
|
122
|
+
//计时
|
|
123
|
+
const timeflag = "repeatPromise "+UtilFunc.genUUID();
|
|
124
|
+
SLogger.time(timeflag);
|
|
125
|
+
|
|
104
126
|
//转换为毫秒
|
|
105
127
|
const hasRepeatTime = (repeatTime>=10);
|
|
106
128
|
if(hasRepeatTime) repeatTime*=1000;
|
|
@@ -125,60 +147,72 @@ export async function repeatPromise<T>(procFn:PromiseProcFn<T>,verifyFn?:Promise
|
|
|
125
147
|
}
|
|
126
148
|
|
|
127
149
|
//进行中的请求
|
|
128
|
-
const plist:Promise<ProcessingPromise<T>>[] =
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
150
|
+
const plist:Promise<ProcessingPromise<T>>[] = [];
|
|
151
|
+
|
|
152
|
+
//开始处理
|
|
153
|
+
try{
|
|
154
|
+
for(let i=0;i<repeatCount;){
|
|
155
|
+
SLogger.info(`开始第 ${i+1} 次 repeatPromise`);
|
|
156
|
+
//创建当前任务
|
|
157
|
+
if(plist.length<i+1){
|
|
158
|
+
plist.push(procFn().then(result =>
|
|
159
|
+
({result, stat:verifyFn!(result), index:i})));
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
//创建定时器
|
|
163
|
+
if(timerP==null){
|
|
164
|
+
timerP = new Promise<"Timeout">(function(resolve, rejecte){
|
|
165
|
+
resolveFn = resolve;
|
|
166
|
+
timer = setTimeout(()=>resolve("Timeout"),
|
|
167
|
+
hasRepeatTime? repeatTime:Infinity);//无限制则无限时间
|
|
168
|
+
})
|
|
169
|
+
}
|
|
144
170
|
|
|
145
|
-
|
|
146
|
-
|
|
171
|
+
//等待完成
|
|
172
|
+
const currObj = await Promise.race([...plist, timerP]);
|
|
147
173
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
174
|
+
//超时处理
|
|
175
|
+
if(currObj=="Timeout"){
|
|
176
|
+
SLogger.warn(`第 ${i+1} 次 repeatPromise 超时 ${repeatTime} ms 开始重试`);
|
|
177
|
+
clearTimer(); i++;
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
154
180
|
const poststat = await currObj.stat;
|
|
155
181
|
switch(poststat){
|
|
156
182
|
case "Completed"://完成
|
|
157
|
-
SLogger.info(
|
|
158
|
-
|
|
183
|
+
SLogger.info(`第 ${currObj.index+1} 次 repeatPromise 成功`);
|
|
184
|
+
//非当前
|
|
185
|
+
if(currObj.index!=i)
|
|
186
|
+
SLogger.info(`成功的 promise 非当前 promise 考虑增大重试间隔\n当前index: ${i}\n当前间隔: ${repeatTime}`);
|
|
159
187
|
return currObj.result;
|
|
160
188
|
case "Terminated"://终止
|
|
161
|
-
SLogger.warn(
|
|
162
|
-
clearTimer();
|
|
189
|
+
SLogger.warn(`第 ${currObj.index+1} 次 repeatPromise 终止 停止重试`);
|
|
163
190
|
return currObj.result;
|
|
164
191
|
case "Failed"://验证失败
|
|
192
|
+
//抛弃失败
|
|
193
|
+
plist[currObj.index] = UtilFunc.getNeverResolvedPromise();
|
|
165
194
|
//是当前
|
|
166
195
|
if(currObj.index==i){
|
|
167
|
-
SLogger.warn(
|
|
196
|
+
SLogger.warn(`第 ${currObj.index+1} 次 repeatPromise 失败 开始重试`);
|
|
168
197
|
clearTimer(); i++;
|
|
169
198
|
continue;
|
|
170
199
|
}
|
|
171
200
|
//非当前
|
|
172
|
-
SLogger.warn(
|
|
173
|
-
//抛弃失败
|
|
174
|
-
plist[currObj.index] = UtilFunc.getNeverResolvedPromise();
|
|
201
|
+
SLogger.warn(`第 ${currObj.index+1} 次 repeatPromise 失败`);
|
|
175
202
|
continue;
|
|
176
203
|
}
|
|
177
204
|
}
|
|
205
|
+
//全部失败或超时则返回null
|
|
206
|
+
SLogger.warn(`${repeatCount} 次 repeatPromise 尝试均失败`);
|
|
207
|
+
return null;
|
|
208
|
+
}catch(err){
|
|
209
|
+
SLogger.warn(`repeatPromise 发生错误`,err);
|
|
210
|
+
return null;
|
|
211
|
+
}finally{
|
|
212
|
+
//清理
|
|
213
|
+
clearTimer();
|
|
214
|
+
SLogger.timeEnd(timeflag);
|
|
178
215
|
}
|
|
179
|
-
clearTimer();
|
|
180
|
-
//全部失败或超时则返回null
|
|
181
|
-
return null;
|
|
182
216
|
}
|
|
183
217
|
|
|
184
218
|
}
|
package/src/UtilInterfaces.ts
CHANGED
|
@@ -72,16 +72,6 @@ export type ExclusiveJObject<B extends JObject,T extends JToken,K extends string
|
|
|
72
72
|
TMP extends JArray = ExclusiveRecursive<B,T,K>> = TMP[number];
|
|
73
73
|
|
|
74
74
|
|
|
75
|
-
/**进行中的请求 */
|
|
76
|
-
export type ProcessingPromise<T> = {
|
|
77
|
-
/**主体请求 */
|
|
78
|
-
result:T;
|
|
79
|
-
/**请求状态 */
|
|
80
|
-
stat:PromiseStat|Promise<PromiseStat>;
|
|
81
|
-
/**请求下标/序号 */
|
|
82
|
-
index:number;
|
|
83
|
-
};
|
|
84
|
-
|
|
85
75
|
/**请求完成状态 成功/失败/终止
|
|
86
76
|
* 成功/终止 将直接返回
|
|
87
77
|
* 失败 将重试
|