@ancon/wildcat-utils 1.40.20 → 1.40.21
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.
|
@@ -35,6 +35,7 @@ declare class HangfirePoller {
|
|
|
35
35
|
url: string;
|
|
36
36
|
headers: APIHeaders;
|
|
37
37
|
}): Promise<unknown>;
|
|
38
|
+
/** Trigger a worker to execute immediately, ignoring busy state */
|
|
38
39
|
triggerWorker(workerId: string): void;
|
|
39
40
|
/** May be called either before or after `createWorker()` */
|
|
40
41
|
enableWorkers(groupId: string): void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var f=Object.defineProperty;var p=(
|
|
1
|
+
"use strict";var f=Object.defineProperty;var p=(i,e,r)=>e in i?f(i,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):i[e]=r;var o=(i,e,r)=>(p(i,typeof e!="symbol"?e+"":e,r),r);Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const b=require("axios"),n=require("@ancon/wildcat-types"),u=require("../error/createCodedError.js"),m=require("../api/isNoContentResponse.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 u(new Error("Timer already started"),n.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(s=>this.executeWorkerRequest(s))}),this.groups.size<1&&this.stopTimer()},5e3)}async executeWorkerRequest(e,r=!1){const s=this.config.getIsNetworkOnline();!(r!==!0&&this.busyWorkers.has(e.workerId))&&s&&(this.busyWorkers.add(e.workerId),b({url:e.url,method:"get",headers:{...await this.config.getRequestHeaders(),...e.headers}}).then(t=>{t&&!m(t)?(e.resolve(t),this.deleteWorker(e.groupId,e.workerId)):this.busyWorkers.delete(e.workerId)}).catch(t=>{e.reject(t),this.deleteWorker(e.groupId,e.workerId)}))}deleteWorker(e,r){const s=this.groups.get(e);s&&(this.config.debug("*delete worker*",s.size,s),s.delete(r),this.workerByIdMap.delete(r),this.busyWorkers.delete(r),s.size<1&&(this.groups.delete(e),this.enabledGroups.delete(e),this.enabledGroups.size<1&&this.stopTimer()))}createWorker(e,{workerId:r,url:s,headers:h}){if(!this.groups.has(e)){if(this.groups.size>=this.config.maxGroupsSize){const a=u(new Error("Max number of Hangfire groups exceeded"),n.ErrorCode.GenericUnexpectedHangfireError,{count:this.groups.size});throw this.destroy(),a}this.groups.set(e,new Map)}const t=this.groups.get(e);return new Promise((a,l)=>{if(t.has(r))throw u(new Error("Duplicate Hangfire worker ID"),n.ErrorCode.GenericUnexpectedHangfireError);if(t.size>=this.config.maxWorkersSize){const c=u(new Error("Max number of Hangfire workers in group exceeded"),n.ErrorCode.GenericUnexpectedHangfireError,{groupId:e,count:t.size});throw l(c),this.destroy(),c}const d={workerId:r,groupId:e,url:s,resolve:a,reject:l,headers:h};t.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)}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 k(i){return new g(i)}exports.HangfirePoller=g;exports.default=k;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
var
|
|
2
|
-
var f = (
|
|
3
|
-
var
|
|
1
|
+
var g = Object.defineProperty;
|
|
2
|
+
var f = (i, e, r) => e in i ? g(i, e, { enumerable: !0, configurable: !0, writable: !0, value: r }) : i[e] = r;
|
|
3
|
+
var o = (i, e, r) => (f(i, typeof e != "symbol" ? e + "" : e, r), r);
|
|
4
4
|
import p from "axios";
|
|
5
5
|
import { ErrorCode as n } from "@ancon/wildcat-types";
|
|
6
6
|
import h from "../error/createCodedError.mjs";
|
|
@@ -8,15 +8,15 @@ import m from "../api/isNoContentResponse.mjs";
|
|
|
8
8
|
class b {
|
|
9
9
|
constructor(e) {
|
|
10
10
|
/** Workers currently executing a request */
|
|
11
|
-
|
|
11
|
+
o(this, "busyWorkers", /* @__PURE__ */ new Set());
|
|
12
12
|
/** Groups with enabled workers */
|
|
13
|
-
|
|
13
|
+
o(this, "enabledGroups", /* @__PURE__ */ new Set());
|
|
14
14
|
/** Workers are grouped by entity ID (check ID or order ID) */
|
|
15
|
-
|
|
15
|
+
o(this, "groups", /* @__PURE__ */ new Map());
|
|
16
16
|
/** Workers mapped by worker ID */
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
o(this, "workerByIdMap", /* @__PURE__ */ new Map());
|
|
18
|
+
o(this, "timer", null);
|
|
19
|
+
o(this, "config", {
|
|
20
20
|
maxGroupsSize: 50,
|
|
21
21
|
maxWorkersSize: 20,
|
|
22
22
|
getIsNetworkOnline() {
|
|
@@ -54,19 +54,19 @@ class b {
|
|
|
54
54
|
}), this.groups.size < 1 && this.stopTimer();
|
|
55
55
|
}, 5e3);
|
|
56
56
|
}
|
|
57
|
-
async executeWorkerRequest(e) {
|
|
58
|
-
const
|
|
59
|
-
!this.busyWorkers.has(e.workerId) &&
|
|
57
|
+
async executeWorkerRequest(e, r = !1) {
|
|
58
|
+
const s = this.config.getIsNetworkOnline();
|
|
59
|
+
!(r !== !0 && this.busyWorkers.has(e.workerId)) && s && (this.busyWorkers.add(e.workerId), p({
|
|
60
60
|
url: e.url,
|
|
61
61
|
method: "get",
|
|
62
62
|
headers: {
|
|
63
63
|
...await this.config.getRequestHeaders(),
|
|
64
64
|
...e.headers
|
|
65
65
|
}
|
|
66
|
-
}).then((
|
|
67
|
-
|
|
68
|
-
}).catch((
|
|
69
|
-
e.reject(
|
|
66
|
+
}).then((t) => {
|
|
67
|
+
t && !m(t) ? (e.resolve(t), this.deleteWorker(e.groupId, e.workerId)) : this.busyWorkers.delete(e.workerId);
|
|
68
|
+
}).catch((t) => {
|
|
69
|
+
e.reject(t), this.deleteWorker(e.groupId, e.workerId);
|
|
70
70
|
}));
|
|
71
71
|
}
|
|
72
72
|
deleteWorker(e, r) {
|
|
@@ -76,7 +76,7 @@ class b {
|
|
|
76
76
|
createWorker(e, {
|
|
77
77
|
workerId: r,
|
|
78
78
|
url: s,
|
|
79
|
-
headers:
|
|
79
|
+
headers: u
|
|
80
80
|
}) {
|
|
81
81
|
if (!this.groups.has(e)) {
|
|
82
82
|
if (this.groups.size >= this.config.maxGroupsSize) {
|
|
@@ -89,36 +89,37 @@ class b {
|
|
|
89
89
|
}
|
|
90
90
|
this.groups.set(e, /* @__PURE__ */ new Map());
|
|
91
91
|
}
|
|
92
|
-
const
|
|
93
|
-
return new Promise((a,
|
|
94
|
-
if (
|
|
92
|
+
const t = this.groups.get(e);
|
|
93
|
+
return new Promise((a, l) => {
|
|
94
|
+
if (t.has(r))
|
|
95
95
|
throw h(
|
|
96
96
|
new Error("Duplicate Hangfire worker ID"),
|
|
97
97
|
n.GenericUnexpectedHangfireError
|
|
98
98
|
);
|
|
99
|
-
if (
|
|
100
|
-
const
|
|
99
|
+
if (t.size >= this.config.maxWorkersSize) {
|
|
100
|
+
const c = h(
|
|
101
101
|
new Error("Max number of Hangfire workers in group exceeded"),
|
|
102
102
|
n.GenericUnexpectedHangfireError,
|
|
103
|
-
{ groupId: e, count:
|
|
103
|
+
{ groupId: e, count: t.size }
|
|
104
104
|
);
|
|
105
|
-
throw
|
|
105
|
+
throw l(c), this.destroy(), c;
|
|
106
106
|
}
|
|
107
|
-
const
|
|
107
|
+
const d = {
|
|
108
108
|
workerId: r,
|
|
109
109
|
groupId: e,
|
|
110
110
|
url: s,
|
|
111
111
|
resolve: a,
|
|
112
|
-
reject:
|
|
113
|
-
headers:
|
|
112
|
+
reject: l,
|
|
113
|
+
headers: u
|
|
114
114
|
};
|
|
115
|
-
|
|
115
|
+
t.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();
|
|
116
116
|
});
|
|
117
117
|
}
|
|
118
|
+
/** Trigger a worker to execute immediately, ignoring busy state */
|
|
118
119
|
triggerWorker(e) {
|
|
119
120
|
this.config.debug("*triggerWorker*");
|
|
120
121
|
const r = this.workerByIdMap.get(e);
|
|
121
|
-
r && this.executeWorkerRequest(r);
|
|
122
|
+
r && this.executeWorkerRequest(r, !0);
|
|
122
123
|
}
|
|
123
124
|
/** May be called either before or after `createWorker()` */
|
|
124
125
|
enableWorkers(e) {
|
|
@@ -131,8 +132,8 @@ class b {
|
|
|
131
132
|
this.config.debug("*destroy*"), this.groups.clear(), this.stopTimer();
|
|
132
133
|
}
|
|
133
134
|
}
|
|
134
|
-
function x(
|
|
135
|
-
return new b(
|
|
135
|
+
function x(i) {
|
|
136
|
+
return new b(i);
|
|
136
137
|
}
|
|
137
138
|
export {
|
|
138
139
|
b as HangfirePoller,
|