@zwa73/utils 1.0.46 → 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.
@@ -108,6 +108,9 @@ var UtilFunc;
108
108
  * @returns {Promise<T|null>} - 结果 null 为全部失败/超时
109
109
  */
110
110
  async function repeatPromise(procFn, verifyFn, repeatCount = 3, repeatTime = 180) {
111
+ //计时
112
+ const timeflag = "repeatPromise " + UtilFunc.genUUID();
113
+ UtilLogger_1.SLogger.time(timeflag);
111
114
  //转换为毫秒
112
115
  const hasRepeatTime = (repeatTime >= 10);
113
116
  if (hasRepeatTime)
@@ -130,58 +133,70 @@ var UtilFunc;
130
133
  resolveFn = null;
131
134
  };
132
135
  //进行中的请求
133
- const plist = new Array(repeatCount)
134
- .fill(UtilFunc.getNeverResolvedPromise());
135
- for (let i = 0; i < repeatCount;) {
136
- UtilLogger_1.SLogger.info("开始第 " + (i + 1) + " 次 repeatPromise");
137
- //创建当前任务
138
- plist[i] = procFn().then(result => ({ result, stat: verifyFn(result), index: i }));
139
- //创建定时器
140
- if (timerP == null) {
141
- timerP = new Promise(function (resolve, rejecte) {
142
- resolveFn = resolve;
143
- timer = setTimeout(() => resolve("Timeout"), hasRepeatTime ? repeatTime : Infinity); //无限制则无限时间
144
- });
145
- }
146
- //等待完成
147
- const currObj = await Promise.race([...plist, timerP]);
148
- //超时处理
149
- if (currObj == "Timeout") {
150
- UtilLogger_1.SLogger.warn(`第 ${(i + 1)} repeatPromise 超时 ${repeatTime} ms 开始重试`);
151
- clearTimer();
152
- i++;
153
- continue;
154
- }
155
- else {
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
+ }
156
161
  const poststat = await currObj.stat;
157
162
  switch (poststat) {
158
163
  case "Completed": //完成
159
- UtilLogger_1.SLogger.info("第 " + (currObj.index + 1) + " 次 repeatPromise 成功");
160
- clearTimer();
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}`);
161
168
  return currObj.result;
162
169
  case "Terminated": //终止
163
- UtilLogger_1.SLogger.warn("第 " + (currObj.index + 1) + " 次 repeatPromise 终止 停止重试");
164
- clearTimer();
170
+ UtilLogger_1.SLogger.warn(`第 ${currObj.index + 1} 次 repeatPromise 终止 停止重试`);
165
171
  return currObj.result;
166
172
  case "Failed": //验证失败
173
+ //抛弃失败
174
+ plist[currObj.index] = UtilFunc.getNeverResolvedPromise();
167
175
  //是当前
168
176
  if (currObj.index == i) {
169
- UtilLogger_1.SLogger.warn("第 " + (currObj.index + 1) + " 次 repeatPromise 失败 开始重试");
177
+ UtilLogger_1.SLogger.warn(`第 ${currObj.index + 1} 次 repeatPromise 失败 开始重试`);
170
178
  clearTimer();
171
179
  i++;
172
180
  continue;
173
181
  }
174
182
  //非当前
175
- UtilLogger_1.SLogger.warn("第 " + (currObj.index + 1) + " 次 repeatPromise 失败");
176
- //抛弃失败
177
- plist[currObj.index] = UtilFunc.getNeverResolvedPromise();
183
+ UtilLogger_1.SLogger.warn(`第 ${currObj.index + 1} 次 repeatPromise 失败`);
178
184
  continue;
179
185
  }
180
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);
181
199
  }
182
- clearTimer();
183
- //全部失败或超时则返回null
184
- return null;
185
200
  }
186
201
  UtilFunc.repeatPromise = repeatPromise;
187
202
  })(UtilFunc = exports.UtilFunc || (exports.UtilFunc = {}));
@@ -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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zwa73/utils",
3
- "version": "1.0.46",
3
+ "version": "1.0.47",
4
4
  "description": "my utils",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/src/UtilCom.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { JObject, ProcessingPromise, PromiseVerifyFn, stringifyJToken } from "./UtilInterfaces";
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";
@@ -1,5 +1,5 @@
1
1
  import * as crypto from "crypto";
2
- import { JToken, ProcessingPromise, PromiseProcFn, PromiseVerifyFn } from "./UtilInterfaces";
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,6 +98,16 @@ export function getNeverResolvedPromise<T>():Promise<T>{
98
98
  return NeverResolvedPromise as Promise<T>;
99
99
  }
100
100
 
101
+ /**进行中的请求 */
102
+ type ProcessingPromise<T> = {
103
+ /**主体请求 */
104
+ result:T;
105
+ /**请求状态 */
106
+ stat:PromiseStat|Promise<PromiseStat>;
107
+ /**请求下标/序号 */
108
+ index:number;
109
+ };
110
+
101
111
  /**重复尝试promise
102
112
  * @async
103
113
  * @param {PromiseProcFn<T>} [procFn] - 发起函数
@@ -108,6 +118,11 @@ export function getNeverResolvedPromise<T>():Promise<T>{
108
118
  */
109
119
  export async function repeatPromise<T>(procFn:PromiseProcFn<T>,verifyFn?:PromiseVerifyFn<T>,
110
120
  repeatCount:number=3,repeatTime:number=180):Promise<T|null>{
121
+
122
+ //计时
123
+ const timeflag = "repeatPromise "+UtilFunc.genUUID();
124
+ SLogger.time(timeflag);
125
+
111
126
  //转换为毫秒
112
127
  const hasRepeatTime = (repeatTime>=10);
113
128
  if(hasRepeatTime) repeatTime*=1000;
@@ -132,60 +147,72 @@ export async function repeatPromise<T>(procFn:PromiseProcFn<T>,verifyFn?:Promise
132
147
  }
133
148
 
134
149
  //进行中的请求
135
- const plist:Promise<ProcessingPromise<T>>[] = new Array(repeatCount)
136
- .fill(UtilFunc.getNeverResolvedPromise());
137
- for(let i=0;i<repeatCount;){
138
- SLogger.info("开始第 "+(i+1)+" 次 repeatPromise");
139
- //创建当前任务
140
- plist[i] = procFn().then(result =>
141
- ({result, stat:verifyFn!(result), index:i}));
142
-
143
- //创建定时器
144
- if(timerP==null){
145
- timerP = new Promise<"Timeout">(function(resolve, rejecte){
146
- resolveFn = resolve;
147
- timer = setTimeout(()=>resolve("Timeout"),
148
- hasRepeatTime? repeatTime:Infinity);//无限制则无限时间
149
- })
150
- }
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
+ }
151
170
 
152
- //等待完成
153
- const currObj = await Promise.race([...plist, timerP]);
171
+ //等待完成
172
+ const currObj = await Promise.race([...plist, timerP]);
154
173
 
155
- //超时处理
156
- if(currObj=="Timeout"){
157
- SLogger.warn(`第 ${(i+1)} 次 repeatPromise 超时 ${repeatTime} ms 开始重试`);
158
- clearTimer(); i++;
159
- continue;
160
- }else{
174
+ //超时处理
175
+ if(currObj=="Timeout"){
176
+ SLogger.warn(`第 ${i+1} 次 repeatPromise 超时 ${repeatTime} ms 开始重试`);
177
+ clearTimer(); i++;
178
+ continue;
179
+ }
161
180
  const poststat = await currObj.stat;
162
181
  switch(poststat){
163
182
  case "Completed"://完成
164
- SLogger.info("第 "+(currObj.index+1)+" 次 repeatPromise 成功");
165
- clearTimer();
183
+ SLogger.info(`第 ${currObj.index+1} 次 repeatPromise 成功`);
184
+ //非当前
185
+ if(currObj.index!=i)
186
+ SLogger.info(`成功的 promise 非当前 promise 考虑增大重试间隔\n当前index: ${i}\n当前间隔: ${repeatTime}`);
166
187
  return currObj.result;
167
188
  case "Terminated"://终止
168
- SLogger.warn("第 "+(currObj.index+1)+" 次 repeatPromise 终止 停止重试");
169
- clearTimer();
189
+ SLogger.warn(`第 ${currObj.index+1} 次 repeatPromise 终止 停止重试`);
170
190
  return currObj.result;
171
191
  case "Failed"://验证失败
192
+ //抛弃失败
193
+ plist[currObj.index] = UtilFunc.getNeverResolvedPromise();
172
194
  //是当前
173
195
  if(currObj.index==i){
174
- SLogger.warn("第 "+(currObj.index+1)+" 次 repeatPromise 失败 开始重试");
196
+ SLogger.warn(`第 ${currObj.index+1} 次 repeatPromise 失败 开始重试`);
175
197
  clearTimer(); i++;
176
198
  continue;
177
199
  }
178
200
  //非当前
179
- SLogger.warn("第 "+(currObj.index+1)+" 次 repeatPromise 失败");
180
- //抛弃失败
181
- plist[currObj.index] = UtilFunc.getNeverResolvedPromise();
201
+ SLogger.warn(`第 ${currObj.index+1} 次 repeatPromise 失败`);
182
202
  continue;
183
203
  }
184
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);
185
215
  }
186
- clearTimer();
187
- //全部失败或超时则返回null
188
- return null;
189
216
  }
190
217
 
191
218
  }
@@ -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
  * 失败 将重试