n8n-nodes-chat2crm 0.1.19 → 0.1.20
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.
|
@@ -261,24 +261,36 @@ class Chat2CrmTrigger {
|
|
|
261
261
|
let pollingPromise = null;
|
|
262
262
|
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
|
|
263
263
|
const runPollingLoop = async () => {
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
264
|
+
try {
|
|
265
|
+
while (!isClosing) {
|
|
266
|
+
await readMessages();
|
|
267
|
+
// Если block=0, добавляем небольшой интервал чтобы не прожигать CPU
|
|
268
|
+
if (!isClosing) {
|
|
269
|
+
await sleep(pollInterval);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
catch (error) {
|
|
274
|
+
// Важно: не даём polling loop'у "повесить" деактивацию воркфлоу
|
|
267
275
|
if (!isClosing) {
|
|
268
|
-
|
|
276
|
+
console.error('[Chat2Crm Trigger] Polling loop crashed:', error);
|
|
269
277
|
}
|
|
270
278
|
}
|
|
271
279
|
};
|
|
272
280
|
pollingPromise = runPollingLoop();
|
|
281
|
+
const withTimeout = async (promise, timeoutMs) => {
|
|
282
|
+
return await Promise.race([
|
|
283
|
+
promise,
|
|
284
|
+
new Promise((_, reject) => setTimeout(() => reject(new Error('close timeout')), timeoutMs)),
|
|
285
|
+
]);
|
|
286
|
+
};
|
|
273
287
|
// Возвращаем объект с функцией closeFunction для cleanup
|
|
274
288
|
return {
|
|
275
289
|
closeFunction: async () => {
|
|
276
290
|
console.log(`[Chat2Crm Trigger] Closing trigger, cleaning up resources...`);
|
|
277
291
|
isClosing = true; // Устанавливаем флаг закрытия
|
|
278
292
|
try {
|
|
279
|
-
//
|
|
280
|
-
await pollingPromise?.catch(() => undefined);
|
|
281
|
-
// Используем disconnect() вместо quit() для более быстрого закрытия
|
|
293
|
+
// ВАЖНО: СНАЧАЛА рвём соединения, чтобы прервать возможный зависший XREAD (особенно через SSH)
|
|
282
294
|
for (const [db, redis] of redisConnections.entries()) {
|
|
283
295
|
try {
|
|
284
296
|
redis.disconnect();
|
|
@@ -288,6 +300,15 @@ class Chat2CrmTrigger {
|
|
|
288
300
|
}
|
|
289
301
|
}
|
|
290
302
|
redisConnections.clear();
|
|
303
|
+
// Не даём unpublish/deactivate висеть бесконечно: ждём loop ограниченное время
|
|
304
|
+
if (pollingPromise) {
|
|
305
|
+
try {
|
|
306
|
+
await withTimeout(pollingPromise.catch(() => undefined), 1000);
|
|
307
|
+
}
|
|
308
|
+
catch {
|
|
309
|
+
// Игнорируем таймаут, главное — быстро закрыть триггер
|
|
310
|
+
}
|
|
311
|
+
}
|
|
291
312
|
console.log(`[Chat2Crm Trigger] Cleanup completed`);
|
|
292
313
|
}
|
|
293
314
|
catch (error) {
|