@ancon/wildcat-utils 1.40.50 → 1.40.51

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.
@@ -40,6 +40,8 @@ declare class HangfirePoller {
40
40
  }): Promise<unknown>;
41
41
  /** Trigger a worker to execute immediately, ignoring busy state */
42
42
  triggerWorker(workerId: string): void;
43
+ /** Trigger all workers to execute immediately */
44
+ triggerAllWorkers(): void;
43
45
  /** May be called either before or after `createWorker()` */
44
46
  enableWorkers(groupId: string): void;
45
47
  disableWorkers(groupId: string): void;
@@ -1 +1 @@
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("../error/isErrorWithStatusCode.js"),m=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",{pollerInterval:5e3,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()},this.config.pollerInterval)}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",timeout:this.config.pollerInterval,headers:{...await this.config.getRequestHeaders(),...e.headers}}).then(i=>{i&&!m(i)?(e.resolve(i),this.deleteWorker(e.groupId,e.workerId)):this.busyWorkers.delete(e.workerId)}).catch(i=>{k(i)?(e.reject(i),this.deleteWorker(e.groupId,e.workerId)):(this.config.debug("*executeWorkerRequest* not an api error"),this.busyWorkers.delete(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 d=a(new Error("Max number of Hangfire workers in group exceeded"),u.ErrorCode.GenericUnexpectedHangfireError,{groupId:e,count:n.size});throw l(d),this.destroy(),d}const c={workerId:r,groupId:e,url:t,resolve:h,reject:l,headers:i};n.set(r,c),this.workerByIdMap.set(r,c),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 W(s){return new g(s)}exports.HangfirePoller=g;exports.default=W;
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("../error/isErrorWithStatusCode.js"),w=require("../api/isNoContentResponse.js"),m=require("../shared/wait.js");class d{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",{pollerInterval:5e3,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()},this.config.pollerInterval)}async waitForNetworkOnline(e=5){let r=0;for(;!this.config.getIsNetworkOnline()&&r<e+1;)r+=1,await m(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",timeout:this.config.pollerInterval,headers:{...await this.config.getRequestHeaders(),...e.headers}}).then(i=>{i&&!w(i)?(e.resolve(i),this.deleteWorker(e.groupId,e.workerId)):this.busyWorkers.delete(e.workerId)}).catch(i=>{k(i)?(e.reject(i),this.deleteWorker(e.groupId,e.workerId)):(this.config.debug("*executeWorkerRequest* not an api error"),this.busyWorkers.delete(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 g={workerId:r,groupId:e,url:t,resolve:h,reject:l,headers:i};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()})}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)}triggerAllWorkers(){this.config.debug("*triggerAllWorkers*");for(const e of this.workerByIdMap.values())this.executeWorkerRequest(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 W(s){return new d(s)}exports.HangfirePoller=d;exports.default=W;
@@ -1,9 +1,9 @@
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;
1
+ var d = Object.defineProperty;
2
+ var f = (s, e, r) => e in s ? d(s, e, { enumerable: !0, configurable: !0, writable: !0, value: r }) : s[e] = r;
3
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 a } from "@ancon/wildcat-types";
6
- import u from "../error/createCodedError.mjs";
5
+ import { ErrorCode as u } from "@ancon/wildcat-types";
6
+ import a from "../error/createCodedError.mjs";
7
7
  import b from "../error/isErrorWithStatusCode.mjs";
8
8
  import m from "../api/isNoContentResponse.mjs";
9
9
  import k from "../shared/wait.mjs";
@@ -42,9 +42,9 @@ class w {
42
42
  }
43
43
  startTimer() {
44
44
  if (this.timer != null)
45
- throw u(
45
+ throw a(
46
46
  new Error("Timer already started"),
47
- a.GenericUnexpectedHangfireError
47
+ u.GenericUnexpectedHangfireError
48
48
  );
49
49
  this.config.debug("*start timer*"), this.timer = setInterval(() => {
50
50
  this.config.debug("*tick*", {
@@ -98,9 +98,9 @@ class w {
98
98
  }) {
99
99
  if (!this.groups.has(e)) {
100
100
  if (this.groups.size >= this.config.maxGroupsSize) {
101
- const h = u(
101
+ const h = a(
102
102
  new Error("Max number of Hangfire groups exceeded"),
103
- a.GenericUnexpectedHangfireError,
103
+ u.GenericUnexpectedHangfireError,
104
104
  { count: this.groups.size }
105
105
  );
106
106
  throw this.destroy(), h;
@@ -110,17 +110,17 @@ class w {
110
110
  const n = this.groups.get(e);
111
111
  return new Promise((h, l) => {
112
112
  if (n.has(r))
113
- throw u(
113
+ throw a(
114
114
  new Error("Duplicate Hangfire worker ID"),
115
- a.GenericUnexpectedHangfireError
115
+ u.GenericUnexpectedHangfireError
116
116
  );
117
117
  if (n.size >= this.config.maxWorkersSize) {
118
- const d = u(
118
+ const c = a(
119
119
  new Error("Max number of Hangfire workers in group exceeded"),
120
- a.GenericUnexpectedHangfireError,
120
+ u.GenericUnexpectedHangfireError,
121
121
  { groupId: e, count: n.size }
122
122
  );
123
- throw l(d), this.destroy(), d;
123
+ throw l(c), this.destroy(), c;
124
124
  }
125
125
  const g = {
126
126
  workerId: r,
@@ -139,6 +139,12 @@ class w {
139
139
  const r = this.workerByIdMap.get(e);
140
140
  r ? this.executeWorkerRequest(r, !0) : this.config.debug("*triggerWorker* worker not found", e);
141
141
  }
142
+ /** Trigger all workers to execute immediately */
143
+ triggerAllWorkers() {
144
+ this.config.debug("*triggerAllWorkers*");
145
+ for (const e of this.workerByIdMap.values())
146
+ this.executeWorkerRequest(e);
147
+ }
142
148
  /** May be called either before or after `createWorker()` */
143
149
  enableWorkers(e) {
144
150
  this.config.debug("*enable*", e), this.enabledGroups.add(e), this.groups.has(e) && this.timer == null && this.startTimer();
@@ -150,10 +156,10 @@ class w {
150
156
  this.config.debug("*destroy*"), this.groups.clear(), this.stopTimer();
151
157
  }
152
158
  }
153
- function q(s) {
159
+ function H(s) {
154
160
  return new w(s);
155
161
  }
156
162
  export {
157
163
  w as HangfirePoller,
158
- q as default
164
+ H as default
159
165
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ancon/wildcat-utils",
3
- "version": "1.40.50",
3
+ "version": "1.40.51",
4
4
  "private": false,
5
5
  "main": "index.js",
6
6
  "module": "index.mjs",