@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=(t,e,r)=>e in t?f(t,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[e]=r;var i=(t,e,r)=>(p(t,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"),a=require("../error/createCodedError.js"),m=require("../api/isNoContentResponse.js");class c{constructor(e){i(this,"busyWorkers",new Set);i(this,"enabledGroups",new Set);i(this,"groups",new Map);i(this,"workerByIdMap",new Map);i(this,"timer",null);i(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"),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){const r=this.config.getIsNetworkOnline();!this.busyWorkers.has(e.workerId)&&r&&(this.busyWorkers.add(e.workerId),b({url:e.url,method:"get",headers:{...await this.config.getRequestHeaders(),...e.headers}}).then(s=>{s&&!m(s)?(e.resolve(s),this.deleteWorker(e.groupId,e.workerId)):this.busyWorkers.delete(e.workerId)}).catch(s=>{e.reject(s),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:g}){if(!this.groups.has(e)){if(this.groups.size>=this.config.maxGroupsSize){const u=a(new Error("Max number of Hangfire groups exceeded"),n.ErrorCode.GenericUnexpectedHangfireError,{count:this.groups.size});throw this.destroy(),u}this.groups.set(e,new Map)}const o=this.groups.get(e);return new Promise((u,h)=>{if(o.has(r))throw a(new Error("Duplicate Hangfire worker ID"),n.ErrorCode.GenericUnexpectedHangfireError);if(o.size>=this.config.maxWorkersSize){const d=a(new Error("Max number of Hangfire workers in group exceeded"),n.ErrorCode.GenericUnexpectedHangfireError,{groupId:e,count:o.size});throw h(d),this.destroy(),d}const l={workerId:r,groupId:e,url:s,resolve:u,reject:h,headers:g};o.set(r,l),this.workerByIdMap.set(r,l),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)}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(t){return new c(t)}exports.HangfirePoller=c;exports.default=k;
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 c = Object.defineProperty;
2
- var f = (t, e, r) => e in t ? c(t, e, { enumerable: !0, configurable: !0, writable: !0, value: r }) : t[e] = r;
3
- var i = (t, e, r) => (f(t, typeof e != "symbol" ? e + "" : e, r), r);
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
- i(this, "busyWorkers", /* @__PURE__ */ new Set());
11
+ o(this, "busyWorkers", /* @__PURE__ */ new Set());
12
12
  /** Groups with enabled workers */
13
- i(this, "enabledGroups", /* @__PURE__ */ new Set());
13
+ o(this, "enabledGroups", /* @__PURE__ */ new Set());
14
14
  /** Workers are grouped by entity ID (check ID or order ID) */
15
- i(this, "groups", /* @__PURE__ */ new Map());
15
+ o(this, "groups", /* @__PURE__ */ new Map());
16
16
  /** Workers mapped by worker ID */
17
- i(this, "workerByIdMap", /* @__PURE__ */ new Map());
18
- i(this, "timer", null);
19
- i(this, "config", {
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 r = this.config.getIsNetworkOnline();
59
- !this.busyWorkers.has(e.workerId) && r && (this.busyWorkers.add(e.workerId), p({
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((s) => {
67
- s && !m(s) ? (e.resolve(s), this.deleteWorker(e.groupId, e.workerId)) : this.busyWorkers.delete(e.workerId);
68
- }).catch((s) => {
69
- e.reject(s), this.deleteWorker(e.groupId, e.workerId);
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: g
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 o = this.groups.get(e);
93
- return new Promise((a, u) => {
94
- if (o.has(r))
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 (o.size >= this.config.maxWorkersSize) {
100
- const d = h(
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: o.size }
103
+ { groupId: e, count: t.size }
104
104
  );
105
- throw u(d), this.destroy(), d;
105
+ throw l(c), this.destroy(), c;
106
106
  }
107
- const l = {
107
+ const d = {
108
108
  workerId: r,
109
109
  groupId: e,
110
110
  url: s,
111
111
  resolve: a,
112
- reject: u,
113
- headers: g
112
+ reject: l,
113
+ headers: u
114
114
  };
115
- o.set(r, l), this.workerByIdMap.set(r, l), this.enabledGroups.has(e) || this.enabledGroups.add(e), this.timer == null && this.enabledGroups.size > 0 && this.startTimer();
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(t) {
135
- return new b(t);
135
+ function x(i) {
136
+ return new b(i);
136
137
  }
137
138
  export {
138
139
  b as HangfirePoller,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ancon/wildcat-utils",
3
- "version": "1.40.20",
3
+ "version": "1.40.21",
4
4
  "private": false,
5
5
  "main": "index.js",
6
6
  "module": "index.mjs",