dt-common-device 7.6.11 → 7.6.13

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.
@@ -12,5 +12,32 @@ export declare class WebhookQueueService implements IWebhookQueue {
12
12
  * Creates queue if it doesn't exist
13
13
  */
14
14
  addWebhookToQueue(propertyId: string, pmsType: string, webhookData: any, options?: IWebhookQueueOptions): Promise<string>;
15
+ /**
16
+ * Mark a webhook job as completed (processed successfully)
17
+ */
18
+ markWebhookCompleted(propertyId: string, pmsType: string, jobId: string): Promise<void>;
19
+ /**
20
+ * Mark a webhook job as failed
21
+ */
22
+ markWebhookFailed(propertyId: string, pmsType: string, jobId: string, error?: string): Promise<void>;
23
+ /**
24
+ * Poll available webhook from ANY available webhook queue in Redis
25
+ * This method will discover all queues in Redis and filter only webhook queues
26
+ */
27
+ pollWebhookFromQueues(): Promise<{
28
+ jobId: string;
29
+ data: any;
30
+ propertyId: string;
31
+ pmsType: string;
32
+ queueName: string;
33
+ } | null>;
34
+ /**
35
+ * Get all available webhook queue names from Redis
36
+ */
37
+ getAllQueueNames(): Promise<string[]>;
38
+ /**
39
+ * Get total waiting count across all webhook queues
40
+ */
41
+ getTotalWaitingCount(): Promise<number>;
15
42
  static getOrCreateQueue(queueKey: string, queues: Map<string, any>): any;
16
43
  }
@@ -78,6 +78,151 @@ let WebhookQueueService = (() => {
78
78
  });
79
79
  return job.id;
80
80
  }
81
+ /**
82
+ * Mark a webhook job as completed (processed successfully)
83
+ */
84
+ async markWebhookCompleted(propertyId, pmsType, jobId) {
85
+ const queueName = this.generateQueueName(propertyId, pmsType);
86
+ if (!this.webhookQueues.has(queueName)) {
87
+ throw new Error(`Queue ${queueName} does not exist`);
88
+ }
89
+ const queue = this.webhookQueues.get(queueName);
90
+ const job = await queue.getJob(jobId);
91
+ if (job) {
92
+ // Mark job as completed - it will be removed after TTL
93
+ await job.complete();
94
+ }
95
+ }
96
+ /**
97
+ * Mark a webhook job as failed
98
+ */
99
+ async markWebhookFailed(propertyId, pmsType, jobId, error) {
100
+ const queueName = this.generateQueueName(propertyId, pmsType);
101
+ if (!this.webhookQueues.has(queueName)) {
102
+ throw new Error(`Queue ${queueName} does not exist`);
103
+ }
104
+ const queue = this.webhookQueues.get(queueName);
105
+ const job = await queue.getJob(jobId);
106
+ if (job) {
107
+ // Mark job as failed - it will be removed after TTL
108
+ await job.fail(new Error(error || "Job processing failed"));
109
+ }
110
+ }
111
+ /**
112
+ * Poll available webhook from ANY available webhook queue in Redis
113
+ * This method will discover all queues in Redis and filter only webhook queues
114
+ */
115
+ async pollWebhookFromQueues() {
116
+ try {
117
+ // Get all queue names from Redis (not just local Map)
118
+ const redisClient = (0, redis_1.getRedisClient)();
119
+ const queueKeys = await redisClient.keys("bull:*:id");
120
+ // Filter only webhook queues (PropertyId_pmsType_webhook pattern)
121
+ const webhookQueueKeys = queueKeys
122
+ .map((key) => key.replace("bull:", "").replace(":id", ""))
123
+ .filter((queueName) => {
124
+ const parts = queueName.split("_");
125
+ return parts.length === 3 && parts[2] === "webhook";
126
+ });
127
+ // Check each webhook queue for waiting jobs
128
+ for (const queueName of webhookQueueKeys) {
129
+ try {
130
+ // Create queue instance if not exists locally
131
+ if (!this.webhookQueues.has(queueName)) {
132
+ const { Queue } = require("bullmq");
133
+ const queue = new Queue(queueName, {
134
+ connection: (0, redis_1.getRedisClient)(),
135
+ });
136
+ this.webhookQueues.set(queueName, queue);
137
+ }
138
+ const queue = this.webhookQueues.get(queueName);
139
+ // Use takeNextJob instead of getWaiting + moveToActive
140
+ const job = await queue.takeNextJob();
141
+ if (job) {
142
+ // Extract propertyId and pmsType from queue name
143
+ const parts = queueName.split("_");
144
+ const propertyId = parts[0];
145
+ const pmsType = parts[1];
146
+ return {
147
+ jobId: job.id,
148
+ data: job.data,
149
+ propertyId,
150
+ pmsType,
151
+ queueName,
152
+ };
153
+ }
154
+ }
155
+ catch (error) {
156
+ console.error(`Error checking queue ${queueName}:`, error);
157
+ continue; // Try next queue
158
+ }
159
+ }
160
+ return null; // No jobs available in any webhook queue
161
+ }
162
+ catch (error) {
163
+ console.error("Error discovering queues from Redis:", error);
164
+ return null;
165
+ }
166
+ }
167
+ /**
168
+ * Get all available webhook queue names from Redis
169
+ */
170
+ async getAllQueueNames() {
171
+ try {
172
+ const redisClient = (0, redis_1.getRedisClient)();
173
+ const queueKeys = await redisClient.keys("bull:*:id");
174
+ // Filter only webhook queues (PropertyId_pmsType_webhook pattern)
175
+ return queueKeys
176
+ .map((key) => key.replace("bull:", "").replace(":id", ""))
177
+ .filter((queueName) => {
178
+ const parts = queueName.split("_");
179
+ return parts.length === 3 && parts[2] === "webhook";
180
+ });
181
+ }
182
+ catch (error) {
183
+ console.error("Error getting queue names from Redis:", error);
184
+ return [];
185
+ }
186
+ }
187
+ /**
188
+ * Get total waiting count across all webhook queues
189
+ */
190
+ async getTotalWaitingCount() {
191
+ try {
192
+ const redisClient = (0, redis_1.getRedisClient)();
193
+ const queueKeys = await redisClient.keys("bull:*:id");
194
+ let totalCount = 0;
195
+ // Filter only webhook queues and count waiting jobs
196
+ for (const key of queueKeys) {
197
+ const queueName = key.replace("bull:", "").replace(":id", "");
198
+ const parts = queueName.split("_");
199
+ // Only count webhook queues
200
+ if (parts.length === 3 && parts[2] === "webhook") {
201
+ try {
202
+ // Create queue instance if not exists locally
203
+ if (!this.webhookQueues.has(queueName)) {
204
+ const { Queue } = require("bullmq");
205
+ const queue = new Queue(queueName, {
206
+ connection: (0, redis_1.getRedisClient)(),
207
+ });
208
+ this.webhookQueues.set(queueName, queue);
209
+ }
210
+ const queue = this.webhookQueues.get(queueName);
211
+ const waitingJobs = await queue.getWaiting();
212
+ totalCount += waitingJobs.length;
213
+ }
214
+ catch (error) {
215
+ console.error(`Error getting count for queue ${queueName}:`, error);
216
+ }
217
+ }
218
+ }
219
+ return totalCount;
220
+ }
221
+ catch (error) {
222
+ console.error("Error getting total waiting count:", error);
223
+ return 0;
224
+ }
225
+ }
81
226
  static getOrCreateQueue(queueKey, queues) {
82
227
  return (queues.get(queueKey) ??
83
228
  queues
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dt-common-device",
3
- "version": "7.6.11",
3
+ "version": "7.6.13",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "files": [