@ancon/wildcat-utils 1.40.27 → 1.40.28
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.
|
@@ -28,6 +28,7 @@ declare class HangfirePoller {
|
|
|
28
28
|
constructor(options?: Partial<HangfirePollerConfig>);
|
|
29
29
|
private stopTimer;
|
|
30
30
|
private startTimer;
|
|
31
|
+
private waitForNetworkOnline;
|
|
31
32
|
private executeWorkerRequest;
|
|
32
33
|
private deleteWorker;
|
|
33
34
|
createWorker(groupId: string, { workerId, url, headers, }: {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var f=Object.defineProperty;var p=(
|
|
1
|
+
"use strict";var f=Object.defineProperty;var p=(s,e,r)=>e in s?f(s,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):s[e]=r;var o=(s,e,r)=>(p(s,typeof e!="symbol"?e+"":e,r),r);Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const b=require("axios"),u=require("@ancon/wildcat-types"),a=require("../error/createCodedError.js"),k=require("../api/isNoContentResponse.js"),w=require("../shared/wait.js");class g{constructor(e){o(this,"busyWorkers",new Set);o(this,"enabledGroups",new Set);o(this,"groups",new Map);o(this,"workerByIdMap",new Map);o(this,"timer",null);o(this,"config",{maxGroupsSize:50,maxWorkersSize:20,getIsNetworkOnline(){return!0},async getRequestHeaders(){return{}},debug(...e){console.log(...e)}});this.config={...this.config,...e??null}}stopTimer(){this.timer!=null&&(this.config.debug("*stop timer*"),clearInterval(this.timer),this.timer=null)}startTimer(){if(this.timer!=null)throw a(new Error("Timer already started"),u.ErrorCode.GenericUnexpectedHangfireError);this.config.debug("*start timer*"),this.timer=setInterval(()=>{this.config.debug("*tick*",{busyWorkers:this.busyWorkers.size,enabledGroups:this.enabledGroups.size,groups:this.groups}),this.enabledGroups.forEach(e=>{const r=this.groups.get(e);r&&r.forEach(t=>this.executeWorkerRequest(t))}),this.groups.size<1&&this.stopTimer()},5e3)}async waitForNetworkOnline(e=5){let r=0;for(;!this.config.getIsNetworkOnline()&&r<e+1;)r+=1,await w(1e3)}async executeWorkerRequest(e,r=!1){const t=r!==!0&&this.busyWorkers.has(e.workerId);this.config.debug("*executeWorkerRequest*",{workerId:e.workerId,isBusy:t,force:r}),t?this.config.debug("*executeWorkerRequest* worker is busy",e.workerId):(this.busyWorkers.add(e.workerId),await this.waitForNetworkOnline(),this.config.debug("*executeWorkerRequest* request",{isNetworkOnline:this.config.getIsNetworkOnline()}),b({url:e.url,method:"get",headers:{...await this.config.getRequestHeaders(),...e.headers}}).then(i=>{i&&!k(i)?(e.resolve(i),this.deleteWorker(e.groupId,e.workerId)):this.busyWorkers.delete(e.workerId)}).catch(i=>{e.reject(i),this.deleteWorker(e.groupId,e.workerId)}))}deleteWorker(e,r){const t=this.groups.get(e);t&&(this.config.debug("*delete worker*",t.size,t),t.delete(r),this.workerByIdMap.delete(r),this.busyWorkers.delete(r),t.size<1&&(this.groups.delete(e),this.enabledGroups.delete(e),this.enabledGroups.size<1&&this.stopTimer()))}createWorker(e,{workerId:r,url:t,headers:i}){if(!this.groups.has(e)){if(this.groups.size>=this.config.maxGroupsSize){const h=a(new Error("Max number of Hangfire groups exceeded"),u.ErrorCode.GenericUnexpectedHangfireError,{count:this.groups.size});throw this.destroy(),h}this.groups.set(e,new Map)}const n=this.groups.get(e);return new Promise((h,l)=>{if(n.has(r))throw a(new Error("Duplicate Hangfire worker ID"),u.ErrorCode.GenericUnexpectedHangfireError);if(n.size>=this.config.maxWorkersSize){const c=a(new Error("Max number of Hangfire workers in group exceeded"),u.ErrorCode.GenericUnexpectedHangfireError,{groupId:e,count:n.size});throw l(c),this.destroy(),c}const d={workerId:r,groupId:e,url:t,resolve:h,reject:l,headers:i};n.set(r,d),this.workerByIdMap.set(r,d),this.enabledGroups.has(e)||this.enabledGroups.add(e),this.timer==null&&this.enabledGroups.size>0&&this.startTimer()})}triggerWorker(e){this.config.debug("*triggerWorker*");const r=this.workerByIdMap.get(e);r?this.executeWorkerRequest(r,!0):this.config.debug("*triggerWorker* worker not found",e)}enableWorkers(e){this.config.debug("*enable*",e),this.enabledGroups.add(e),this.groups.has(e)&&this.timer==null&&this.startTimer()}disableWorkers(e){this.config.debug("*disable*",e),this.enabledGroups.delete(e),this.enabledGroups.size<1&&this.stopTimer()}destroy(){this.config.debug("*destroy*"),this.groups.clear(),this.stopTimer()}}function m(s){return new g(s)}exports.HangfirePoller=g;exports.default=m;
|
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
var
|
|
2
|
-
var f = (
|
|
3
|
-
var o = (
|
|
1
|
+
var c = Object.defineProperty;
|
|
2
|
+
var f = (s, e, r) => e in s ? c(s, e, { enumerable: !0, configurable: !0, writable: !0, value: r }) : s[e] = r;
|
|
3
|
+
var o = (s, e, r) => (f(s, typeof e != "symbol" ? e + "" : e, r), r);
|
|
4
4
|
import p from "axios";
|
|
5
|
-
import { ErrorCode as
|
|
5
|
+
import { ErrorCode as a } from "@ancon/wildcat-types";
|
|
6
6
|
import h from "../error/createCodedError.mjs";
|
|
7
|
-
import
|
|
8
|
-
|
|
7
|
+
import b from "../api/isNoContentResponse.mjs";
|
|
8
|
+
import m from "../shared/wait.mjs";
|
|
9
|
+
class k {
|
|
9
10
|
constructor(e) {
|
|
10
11
|
/** Workers currently executing a request */
|
|
11
12
|
o(this, "busyWorkers", /* @__PURE__ */ new Set());
|
|
@@ -41,7 +42,7 @@ class b {
|
|
|
41
42
|
if (this.timer != null)
|
|
42
43
|
throw h(
|
|
43
44
|
new Error("Timer already started"),
|
|
44
|
-
|
|
45
|
+
a.GenericUnexpectedHangfireError
|
|
45
46
|
);
|
|
46
47
|
this.config.debug("*start timer*"), this.timer = setInterval(() => {
|
|
47
48
|
this.config.debug("*tick*", {
|
|
@@ -50,76 +51,90 @@ class b {
|
|
|
50
51
|
groups: this.groups
|
|
51
52
|
}), this.enabledGroups.forEach((e) => {
|
|
52
53
|
const r = this.groups.get(e);
|
|
53
|
-
r && r.forEach((
|
|
54
|
+
r && r.forEach((t) => this.executeWorkerRequest(t));
|
|
54
55
|
}), this.groups.size < 1 && this.stopTimer();
|
|
55
56
|
}, 5e3);
|
|
56
57
|
}
|
|
58
|
+
async waitForNetworkOnline(e = 5) {
|
|
59
|
+
let r = 0;
|
|
60
|
+
for (; !this.config.getIsNetworkOnline() && r < e + 1; )
|
|
61
|
+
r += 1, await m(1e3);
|
|
62
|
+
}
|
|
57
63
|
async executeWorkerRequest(e, r = !1) {
|
|
58
|
-
const
|
|
59
|
-
|
|
64
|
+
const t = r !== !0 && this.busyWorkers.has(e.workerId);
|
|
65
|
+
this.config.debug("*executeWorkerRequest*", {
|
|
66
|
+
workerId: e.workerId,
|
|
67
|
+
isBusy: t,
|
|
68
|
+
force: r
|
|
69
|
+
}), t ? this.config.debug(
|
|
70
|
+
"*executeWorkerRequest* worker is busy",
|
|
71
|
+
e.workerId
|
|
72
|
+
) : (this.busyWorkers.add(e.workerId), await this.waitForNetworkOnline(), this.config.debug("*executeWorkerRequest* request", {
|
|
73
|
+
isNetworkOnline: this.config.getIsNetworkOnline()
|
|
74
|
+
}), p({
|
|
60
75
|
url: e.url,
|
|
61
76
|
method: "get",
|
|
62
77
|
headers: {
|
|
63
78
|
...await this.config.getRequestHeaders(),
|
|
64
79
|
...e.headers
|
|
65
80
|
}
|
|
66
|
-
}).then((
|
|
67
|
-
|
|
68
|
-
}).catch((
|
|
69
|
-
e.reject(
|
|
81
|
+
}).then((i) => {
|
|
82
|
+
i && !b(i) ? (e.resolve(i), this.deleteWorker(e.groupId, e.workerId)) : this.busyWorkers.delete(e.workerId);
|
|
83
|
+
}).catch((i) => {
|
|
84
|
+
e.reject(i), this.deleteWorker(e.groupId, e.workerId);
|
|
70
85
|
}));
|
|
71
86
|
}
|
|
72
87
|
deleteWorker(e, r) {
|
|
73
|
-
const
|
|
74
|
-
|
|
88
|
+
const t = this.groups.get(e);
|
|
89
|
+
t && (this.config.debug("*delete worker*", t.size, t), t.delete(r), this.workerByIdMap.delete(r), this.busyWorkers.delete(r), t.size < 1 && (this.groups.delete(e), this.enabledGroups.delete(e), this.enabledGroups.size < 1 && this.stopTimer()));
|
|
75
90
|
}
|
|
76
91
|
createWorker(e, {
|
|
77
92
|
workerId: r,
|
|
78
|
-
url:
|
|
79
|
-
headers:
|
|
93
|
+
url: t,
|
|
94
|
+
headers: i
|
|
80
95
|
}) {
|
|
81
96
|
if (!this.groups.has(e)) {
|
|
82
97
|
if (this.groups.size >= this.config.maxGroupsSize) {
|
|
83
|
-
const
|
|
98
|
+
const u = h(
|
|
84
99
|
new Error("Max number of Hangfire groups exceeded"),
|
|
85
|
-
|
|
100
|
+
a.GenericUnexpectedHangfireError,
|
|
86
101
|
{ count: this.groups.size }
|
|
87
102
|
);
|
|
88
|
-
throw this.destroy(),
|
|
103
|
+
throw this.destroy(), u;
|
|
89
104
|
}
|
|
90
105
|
this.groups.set(e, /* @__PURE__ */ new Map());
|
|
91
106
|
}
|
|
92
|
-
const
|
|
93
|
-
return new Promise((
|
|
94
|
-
if (
|
|
107
|
+
const n = this.groups.get(e);
|
|
108
|
+
return new Promise((u, l) => {
|
|
109
|
+
if (n.has(r))
|
|
95
110
|
throw h(
|
|
96
111
|
new Error("Duplicate Hangfire worker ID"),
|
|
97
|
-
|
|
112
|
+
a.GenericUnexpectedHangfireError
|
|
98
113
|
);
|
|
99
|
-
if (
|
|
100
|
-
const
|
|
114
|
+
if (n.size >= this.config.maxWorkersSize) {
|
|
115
|
+
const d = h(
|
|
101
116
|
new Error("Max number of Hangfire workers in group exceeded"),
|
|
102
|
-
|
|
103
|
-
{ groupId: e, count:
|
|
117
|
+
a.GenericUnexpectedHangfireError,
|
|
118
|
+
{ groupId: e, count: n.size }
|
|
104
119
|
);
|
|
105
|
-
throw l(
|
|
120
|
+
throw l(d), this.destroy(), d;
|
|
106
121
|
}
|
|
107
|
-
const
|
|
122
|
+
const g = {
|
|
108
123
|
workerId: r,
|
|
109
124
|
groupId: e,
|
|
110
|
-
url:
|
|
111
|
-
resolve:
|
|
125
|
+
url: t,
|
|
126
|
+
resolve: u,
|
|
112
127
|
reject: l,
|
|
113
|
-
headers:
|
|
128
|
+
headers: i
|
|
114
129
|
};
|
|
115
|
-
|
|
130
|
+
n.set(r, g), this.workerByIdMap.set(r, g), this.enabledGroups.has(e) || this.enabledGroups.add(e), this.timer == null && this.enabledGroups.size > 0 && this.startTimer();
|
|
116
131
|
});
|
|
117
132
|
}
|
|
118
133
|
/** Trigger a worker to execute immediately, ignoring busy state */
|
|
119
134
|
triggerWorker(e) {
|
|
120
135
|
this.config.debug("*triggerWorker*");
|
|
121
136
|
const r = this.workerByIdMap.get(e);
|
|
122
|
-
r
|
|
137
|
+
r ? this.executeWorkerRequest(r, !0) : this.config.debug("*triggerWorker* worker not found", e);
|
|
123
138
|
}
|
|
124
139
|
/** May be called either before or after `createWorker()` */
|
|
125
140
|
enableWorkers(e) {
|
|
@@ -132,10 +147,10 @@ class b {
|
|
|
132
147
|
this.config.debug("*destroy*"), this.groups.clear(), this.stopTimer();
|
|
133
148
|
}
|
|
134
149
|
}
|
|
135
|
-
function
|
|
136
|
-
return new
|
|
150
|
+
function E(s) {
|
|
151
|
+
return new k(s);
|
|
137
152
|
}
|
|
138
153
|
export {
|
|
139
|
-
|
|
140
|
-
|
|
154
|
+
k as HangfirePoller,
|
|
155
|
+
E as default
|
|
141
156
|
};
|
package/hangfire/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./isHangfireResponse.js"),r=require("./createHangfirePoller.js"),i=require("./createMakeHangfireRequest.js");require("../api/isAcceptedResponse.js");require("axios");require("@ancon/wildcat-types");require("../error/createCodedError.js");require("../api/isNoContentResponse.js");exports.isHangfireResponse=e;exports.createHangfirePoller=r.default;exports.createMakeHangfireRequest=i;
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./isHangfireResponse.js"),r=require("./createHangfirePoller.js"),i=require("./createMakeHangfireRequest.js");require("../api/isAcceptedResponse.js");require("axios");require("@ancon/wildcat-types");require("../error/createCodedError.js");require("../api/isNoContentResponse.js");require("../shared/wait.js");exports.isHangfireResponse=e;exports.createHangfirePoller=r.default;exports.createMakeHangfireRequest=i;
|
package/hangfire/index.mjs
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
import { default as
|
|
2
|
-
import { default as
|
|
3
|
-
import { default as
|
|
1
|
+
import { default as m } from "./isHangfireResponse.mjs";
|
|
2
|
+
import { default as l } from "./createHangfirePoller.mjs";
|
|
3
|
+
import { default as u } from "./createMakeHangfireRequest.mjs";
|
|
4
4
|
import "../api/isAcceptedResponse.mjs";
|
|
5
5
|
import "axios";
|
|
6
6
|
import "@ancon/wildcat-types";
|
|
7
7
|
import "../error/createCodedError.mjs";
|
|
8
8
|
import "../api/isNoContentResponse.mjs";
|
|
9
|
+
import "../shared/wait.mjs";
|
|
9
10
|
export {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
l as createHangfirePoller,
|
|
12
|
+
u as createMakeHangfireRequest,
|
|
13
|
+
m as isHangfireResponse
|
|
13
14
|
};
|