@resolveio/server-lib 20.0.26 → 20.0.28
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.
- package/client-server-app.js +1 -47
- package/client-server-app.js.map +1 -1
- package/collections/app-status.collection.js +1 -50
- package/collections/app-status.collection.js.map +1 -1
- package/collections/counter.collection.js +1 -53
- package/collections/counter.collection.js.map +1 -1
- package/collections/cron-job-history.collection.js +1 -88
- package/collections/cron-job-history.collection.js.map +1 -1
- package/collections/cron-job.collection.js +1 -84
- package/collections/cron-job.collection.js.map +1 -1
- package/collections/email-history.collection.js +1 -65
- package/collections/email-history.collection.js.map +1 -1
- package/collections/email-verified.collection.js +1 -60
- package/collections/email-verified.collection.js.map +1 -1
- package/collections/file.collection.js +1 -72
- package/collections/file.collection.js.map +1 -1
- package/collections/flag.collection.js +1 -55
- package/collections/flag.collection.js.map +1 -1
- package/collections/log-method-latency.collection.js +1 -63
- package/collections/log-method-latency.collection.js.map +1 -1
- package/collections/log-subscription.collection.js +1 -67
- package/collections/log-subscription.collection.js.map +1 -1
- package/collections/log-ts.collection.js +1 -77
- package/collections/log-ts.collection.js.map +1 -1
- package/collections/log.collection.js +1 -76
- package/collections/log.collection.js.map +1 -1
- package/collections/logged-in-users.collection.js +1 -64
- package/collections/logged-in-users.collection.js.map +1 -1
- package/collections/method-response.collection.js +1 -65
- package/collections/method-response.collection.js.map +1 -1
- package/collections/monitor-cpu.collection.js +1 -62
- package/collections/monitor-cpu.collection.js.map +1 -1
- package/collections/monitor-memory.collection.js +1 -74
- package/collections/monitor-memory.collection.js.map +1 -1
- package/collections/monitor-mongo.collection.js +1 -68
- package/collections/monitor-mongo.collection.js.map +1 -1
- package/collections/notification.collection.js +1 -55
- package/collections/notification.collection.js.map +1 -1
- package/collections/report-builder-dashboard-builder.collection.js +1 -108
- package/collections/report-builder-dashboard-builder.collection.js.map +1 -1
- package/collections/report-builder-library.collection.js +1 -86
- package/collections/report-builder-library.collection.js.map +1 -1
- package/collections/report-builder-report.collection.js +1 -148
- package/collections/report-builder-report.collection.js.map +1 -1
- package/collections/user-group.collection.js +1 -93
- package/collections/user-group.collection.js.map +1 -1
- package/collections/user-guide.collection.js +1 -56
- package/collections/user-guide.collection.js.map +1 -1
- package/collections/user.collection.js +1 -265
- package/collections/user.collection.js.map +1 -1
- package/cron/cron.js +1 -101
- package/cron/cron.js.map +1 -1
- package/fixtures/cron-jobs.js +1 -87
- package/fixtures/cron-jobs.js.map +1 -1
- package/fixtures/init.js +1 -58
- package/fixtures/init.js.map +1 -1
- package/http/auth.js +1 -763
- package/http/auth.js.map +1 -1
- package/http/health.js +1 -11
- package/http/health.js.map +1 -1
- package/http/home.js +1 -65
- package/http/home.js.map +1 -1
- package/index.js +1 -126
- package/index.js.map +1 -1
- package/managers/cron.manager.js +1 -322
- package/managers/cron.manager.js.map +1 -1
- package/managers/method.manager.js +1 -593
- package/managers/method.manager.js.map +1 -1
- package/managers/mongo.manager.js +1 -2259
- package/managers/mongo.manager.js.map +1 -1
- package/managers/monitor.manager.js +1 -350
- package/managers/monitor.manager.js.map +1 -1
- package/managers/subscription.manager.js +1 -1110
- package/managers/subscription.manager.js.map +1 -1
- package/managers/subscription.performance.js +1 -102
- package/managers/subscription.performance.js.map +1 -1
- package/methods/accounts.js +1 -291
- package/methods/accounts.js.map +1 -1
- package/methods/aws.js +1 -616
- package/methods/aws.js.map +1 -1
- package/methods/collections.js +1 -499
- package/methods/collections.js.map +1 -1
- package/methods/counters.js +1 -121
- package/methods/counters.js.map +1 -1
- package/methods/cron-jobs.js +1 -1337
- package/methods/cron-jobs.js.map +1 -1
- package/methods/flags.js +1 -8
- package/methods/flags.js.map +1 -1
- package/methods/logs.js +1 -370
- package/methods/logs.js.map +1 -1
- package/methods/monitor.js +1 -564
- package/methods/monitor.js.map +1 -1
- package/methods/pdf.js +1 -731
- package/methods/pdf.js.map +1 -1
- package/methods/report-builder.js +1 -859
- package/methods/report-builder.js.map +1 -1
- package/methods/support.js +1 -245
- package/methods/support.js.map +1 -1
- package/models/app-status.model.js +1 -3
- package/models/app-status.model.js.map +1 -1
- package/models/billing-logged-in-users.model.js +1 -3
- package/models/billing-logged-in-users.model.js.map +1 -1
- package/models/collection-document.model.js +1 -3
- package/models/collection-document.model.js.map +1 -1
- package/models/counter.model.js +1 -3
- package/models/counter.model.js.map +1 -1
- package/models/cron-job-history.model.js +1 -3
- package/models/cron-job-history.model.js.map +1 -1
- package/models/cron-job.model.js +1 -3
- package/models/cron-job.model.js.map +1 -1
- package/models/dialog.model.js +1 -3
- package/models/dialog.model.js.map +1 -1
- package/models/email-history.model.js +1 -3
- package/models/email-history.model.js.map +1 -1
- package/models/email-verified.model.js +1 -3
- package/models/email-verified.model.js.map +1 -1
- package/models/file.model.js +1 -3
- package/models/file.model.js.map +1 -1
- package/models/flag.model.js +1 -3
- package/models/flag.model.js.map +1 -1
- package/models/log-method-latency.model.js +1 -3
- package/models/log-method-latency.model.js.map +1 -1
- package/models/log-subscription.model.js +1 -3
- package/models/log-subscription.model.js.map +1 -1
- package/models/log.model.js +1 -3
- package/models/log.model.js.map +1 -1
- package/models/logged-in-users.model.js +1 -3
- package/models/logged-in-users.model.js.map +1 -1
- package/models/method-response.model.js +1 -3
- package/models/method-response.model.js.map +1 -1
- package/models/method.model.js +1 -3
- package/models/method.model.js.map +1 -1
- package/models/monitor-cpu.model.js +1 -3
- package/models/monitor-cpu.model.js.map +1 -1
- package/models/monitor-memory.model.js +1 -3
- package/models/monitor-memory.model.js.map +1 -1
- package/models/monitor-mongo.model.js +1 -3
- package/models/monitor-mongo.model.js.map +1 -1
- package/models/notification.model.js +1 -3
- package/models/notification.model.js.map +1 -1
- package/models/pagination.model.js +1 -23
- package/models/pagination.model.js.map +1 -1
- package/models/permission.model.js +1 -3
- package/models/permission.model.js.map +1 -1
- package/models/report-builder-dashboard-builder.model.js +1 -3
- package/models/report-builder-dashboard-builder.model.js.map +1 -1
- package/models/report-builder-library.model.js +1 -3
- package/models/report-builder-library.model.js.map +1 -1
- package/models/report-builder-report.model.js +1 -3
- package/models/report-builder-report.model.js.map +1 -1
- package/models/report-builder.model.js +1 -3
- package/models/report-builder.model.js.map +1 -1
- package/models/select-data-label.model.js +1 -3
- package/models/select-data-label.model.js.map +1 -1
- package/models/server-response.model.js +1 -3
- package/models/server-response.model.js.map +1 -1
- package/models/subscription.model.js +1 -3
- package/models/subscription.model.js.map +1 -1
- package/models/support-ticket.model.js +1 -3
- package/models/support-ticket.model.js.map +1 -1
- package/models/user-group.model.js +1 -3
- package/models/user-group.model.js.map +1 -1
- package/models/user-guide.model.js +1 -3
- package/models/user-guide.model.js.map +1 -1
- package/models/user.model.js +1 -3
- package/models/user.model.js.map +1 -1
- package/package.json +6 -4
- package/public_api.js +1 -74
- package/public_api.js.map +1 -1
- package/publications/app-status.js +1 -16
- package/publications/app-status.js.map +1 -1
- package/publications/cron-jobs.js +1 -32
- package/publications/cron-jobs.js.map +1 -1
- package/publications/files.js +1 -36
- package/publications/files.js.map +1 -1
- package/publications/flags.js +1 -22
- package/publications/flags.js.map +1 -1
- package/publications/logs.js +1 -164
- package/publications/logs.js.map +1 -1
- package/publications/method-responses.js +1 -16
- package/publications/method-responses.js.map +1 -1
- package/publications/notifications.js +1 -16
- package/publications/notifications.js.map +1 -1
- package/publications/report-builder-dashboard-builders.js +1 -42
- package/publications/report-builder-dashboard-builders.js.map +1 -1
- package/publications/report-builder-libraries.js +1 -89
- package/publications/report-builder-libraries.js.map +1 -1
- package/publications/report-builder-reports.js +1 -50
- package/publications/report-builder-reports.js.map +1 -1
- package/publications/super-admin.js +1 -16
- package/publications/super-admin.js.map +1 -1
- package/publications/user-groups.js +1 -16
- package/publications/user-groups.js.map +1 -1
- package/publications/user-guides.js +1 -16
- package/publications/user-guides.js.map +1 -1
- package/server-app.js +1 -770
- package/server-app.js.map +1 -1
- package/util/common.js +1 -546
- package/util/common.js.map +1 -1
- package/util/schema-report-builder.js +1 -452
- package/util/schema-report-builder.js.map +1 -1
|
@@ -1,1111 +1,2 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
-
function step(op) {
|
|
16
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
-
switch (op[0]) {
|
|
21
|
-
case 0: case 1: t = op; break;
|
|
22
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
39
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
40
|
-
if (ar || !(i in from)) {
|
|
41
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
42
|
-
ar[i] = from[i];
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
46
|
-
};
|
|
47
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
48
|
-
exports.SubscriptionManager = void 0;
|
|
49
|
-
var logs_1 = require("../publications/logs");
|
|
50
|
-
var app_status_1 = require("../publications/app-status");
|
|
51
|
-
var files_1 = require("../publications/files");
|
|
52
|
-
var super_admin_1 = require("../publications/super-admin");
|
|
53
|
-
var logged_in_users_collection_1 = require("../collections/logged-in-users.collection");
|
|
54
|
-
var cron_jobs_1 = require("../publications/cron-jobs");
|
|
55
|
-
var flags_1 = require("../publications/flags");
|
|
56
|
-
var method_responses_1 = require("../publications/method-responses");
|
|
57
|
-
var index_1 = require("../index");
|
|
58
|
-
var notifications_1 = require("../publications/notifications");
|
|
59
|
-
var report_builder_reports_1 = require("../publications/report-builder-reports");
|
|
60
|
-
var report_builder_libraries_1 = require("../publications/report-builder-libraries");
|
|
61
|
-
var user_groups_1 = require("../publications/user-groups");
|
|
62
|
-
var user_guides_1 = require("../publications/user-guides");
|
|
63
|
-
var report_builder_dashboard_builders_1 = require("../publications/report-builder-dashboard-builders");
|
|
64
|
-
var mongo_manager_1 = require("./mongo.manager");
|
|
65
|
-
var common_1 = require("../util/common");
|
|
66
|
-
var NodeCache = require("node-cache");
|
|
67
|
-
var flag_collection_1 = require("../collections/flag.collection");
|
|
68
|
-
var os_1 = require("os");
|
|
69
|
-
var numCPUs = (0, os_1.cpus)().length;
|
|
70
|
-
var v8 = require('v8');
|
|
71
|
-
// interface CurrentPerformanceMonitor {
|
|
72
|
-
// _id: number;
|
|
73
|
-
// function: string;
|
|
74
|
-
// publication: string;
|
|
75
|
-
// subscriptionData: any[];
|
|
76
|
-
// date_start: Date;
|
|
77
|
-
// date_end: Date;
|
|
78
|
-
// duration: number;
|
|
79
|
-
// result: string;
|
|
80
|
-
// }
|
|
81
|
-
var SubscriptionManager = /** @class */ (function () {
|
|
82
|
-
// private currentPerfomanceMonitor: CurrentPerformanceMonitor[] = [];
|
|
83
|
-
// private idPerformance: number = 0;
|
|
84
|
-
// private performanceThread;
|
|
85
|
-
function SubscriptionManager(mainServer, wss, serverConfig, monitorManagerFunction) {
|
|
86
|
-
var _this = this;
|
|
87
|
-
this._publications = {};
|
|
88
|
-
this._subscriptions = [];
|
|
89
|
-
this._loggedInUsers = [];
|
|
90
|
-
this._mongoQueue = [];
|
|
91
|
-
this._mongoQueueId = 0;
|
|
92
|
-
this._sendQueue = [];
|
|
93
|
-
this._runningQueue = false;
|
|
94
|
-
this._cacheId = 1;
|
|
95
|
-
this._heapStats = v8.getHeapStatistics();
|
|
96
|
-
this._heapLimit = this._heapStats.heap_size_limit / numCPUs / 8;
|
|
97
|
-
this._enableDebug = false;
|
|
98
|
-
this._debugOplogCollections = [];
|
|
99
|
-
this._debugOplogHits = 0;
|
|
100
|
-
this._debugSubCollections = [];
|
|
101
|
-
this._debugSubHits = 0;
|
|
102
|
-
this._debugUnSubHits = 0;
|
|
103
|
-
this._debugUnSubAllHits = 0;
|
|
104
|
-
this._debugMongoQueueHits = 0;
|
|
105
|
-
this._debugMongoQueueCollections = [];
|
|
106
|
-
this._debugSendQueueHits = 0;
|
|
107
|
-
this._debugRemoveCacheHits = 0;
|
|
108
|
-
this._oplogRetryCount = 0;
|
|
109
|
-
this._mainServer = mainServer;
|
|
110
|
-
this._monitorManagerFunction = monitorManagerFunction;
|
|
111
|
-
this._nodeCache = new NodeCache({ stdTTL: 0, checkperiod: 0 });
|
|
112
|
-
// setTimeout(() => {
|
|
113
|
-
// console.log('Setting up performance thread');
|
|
114
|
-
// this.performanceThread = new Worker(path.join(__dirname, './subscription.performance.js'));
|
|
115
|
-
// this.performanceThread.on('exit', code => {
|
|
116
|
-
// console.error(new Date(), 'THREAD EXITED!!!!!!!!!!!!!!!!!!', code);
|
|
117
|
-
// });
|
|
118
|
-
// this.performanceThread.on('error', code => {
|
|
119
|
-
// console.error(new Date(), 'THREAD RECV ERROR !!!!!!!!!!!!!!!!!!', code);
|
|
120
|
-
// });
|
|
121
|
-
// }, 5000);
|
|
122
|
-
// setInterval(() => {
|
|
123
|
-
// console.log('Post thread msg');
|
|
124
|
-
// this.performanceThread.postMessage(this.currentPerfomanceMonitor);
|
|
125
|
-
// this.currentPerfomanceMonitor = [];
|
|
126
|
-
// }, 10000);
|
|
127
|
-
this.serverConfig = serverConfig;
|
|
128
|
-
this._wss = wss;
|
|
129
|
-
// Publications
|
|
130
|
-
(0, super_admin_1.loadSuperAdminPublications)(this);
|
|
131
|
-
(0, app_status_1.loadAppStatusPublications)(this);
|
|
132
|
-
(0, logs_1.loadLogPublications)(this);
|
|
133
|
-
(0, files_1.loadFilePublications)(this);
|
|
134
|
-
(0, cron_jobs_1.loadCronJobPublications)(this);
|
|
135
|
-
(0, flags_1.loadFlagsPublications)(this);
|
|
136
|
-
(0, method_responses_1.loadMethodResponsePublications)(this);
|
|
137
|
-
(0, notifications_1.loadNotificationPublications)(this);
|
|
138
|
-
(0, report_builder_reports_1.loadReportBuilderReportPublications)(this);
|
|
139
|
-
(0, report_builder_libraries_1.loadReportBuilderLibraryPublications)(this);
|
|
140
|
-
(0, user_groups_1.loadUserGroupPublications)(this);
|
|
141
|
-
(0, user_guides_1.loadUserGuidePublications)(this);
|
|
142
|
-
(0, report_builder_dashboard_builders_1.loadReportBuilderDashboardBuilderPublications)(this);
|
|
143
|
-
this.tailOpLog();
|
|
144
|
-
setInterval(function () {
|
|
145
|
-
_this._oplogRetryCount = 0;
|
|
146
|
-
}, 15000);
|
|
147
|
-
setInterval(function () {
|
|
148
|
-
if (_this.getEnableDebug()) {
|
|
149
|
-
console.log(new Date(), 'Sub Manager', 'Subs', _this._subscriptions.length);
|
|
150
|
-
console.log(new Date(), 'Sub Manager', 'Logged In Users', _this._loggedInUsers.length);
|
|
151
|
-
console.log(new Date(), 'Sub Manager', 'Mongo Queue', _this._mongoQueue.length);
|
|
152
|
-
console.log(new Date(), 'Sub Manager', 'Mongo Queue Hits', _this._debugMongoQueueHits);
|
|
153
|
-
console.log(new Date(), 'Sub Manager', 'Mongo Queue Collections', JSON.stringify(_this._debugMongoQueueCollections.sort(function (a, b) { return a.collection.localeCompare(b.collection) || a.publication.localeCompare(b.publication); }), null, 2));
|
|
154
|
-
console.log(new Date(), 'Sub Manager', 'Oplog Hits', _this._debugOplogHits);
|
|
155
|
-
console.log(new Date(), 'Sub Manager', 'Oplog Collections', JSON.stringify(_this._debugOplogCollections.sort(function (a, b) { return a.collection.localeCompare(b.collection) || a.type.localeCompare(b.type); }), null, 2));
|
|
156
|
-
console.log(new Date(), 'Sub Manager', 'Send Queue Hits', _this._debugSendQueueHits);
|
|
157
|
-
console.log(new Date(), 'Sub Manager', 'Sub Hits', _this._debugSubHits);
|
|
158
|
-
console.log(new Date(), 'Sub Manager', 'Sub Collections', JSON.stringify(_this._debugSubCollections.sort(function (a, b) { return a.publication.localeCompare(b.publication); }), null, 2));
|
|
159
|
-
console.log(new Date(), 'Sub Manager', 'Unsub Hits', _this._debugUnSubHits);
|
|
160
|
-
console.log(new Date(), 'Sub Manager', 'Unsub All Hits', _this._debugUnSubAllHits);
|
|
161
|
-
console.log(new Date(), 'Sub Manager', 'Cache Cleanup Hits', _this._debugRemoveCacheHits);
|
|
162
|
-
}
|
|
163
|
-
_this._debugOplogHits = 0;
|
|
164
|
-
_this._debugOplogCollections = [];
|
|
165
|
-
_this._debugSubCollections = [];
|
|
166
|
-
_this._debugMongoQueueHits = 0;
|
|
167
|
-
_this._debugMongoQueueCollections = [];
|
|
168
|
-
_this._debugSendQueueHits = 0;
|
|
169
|
-
_this._debugSubHits = 0;
|
|
170
|
-
_this._debugUnSubHits = 0;
|
|
171
|
-
_this._debugUnSubAllHits = 0;
|
|
172
|
-
_this._debugRemoveCacheHits = 0;
|
|
173
|
-
}, 60000);
|
|
174
|
-
setInterval(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
175
|
-
var _loop_1, this_1, i, queueArr, _loop_2, this_2, i;
|
|
176
|
-
var _this = this;
|
|
177
|
-
return __generator(this, function (_a) {
|
|
178
|
-
if (!this._runningQueue) {
|
|
179
|
-
this._runningQueue = true;
|
|
180
|
-
if (this._sendQueue.length) {
|
|
181
|
-
_loop_1 = function (i) {
|
|
182
|
-
this_1._debugSendQueueHits += 1;
|
|
183
|
-
var ws = this_1._mainServer.getWS(this_1._sendQueue[i].id_ws);
|
|
184
|
-
if (ws && ws.readyState === ws.OPEN) {
|
|
185
|
-
if (ws.bufferedAmount < 20480) {
|
|
186
|
-
var sendItem = this_1._sendQueue.pop();
|
|
187
|
-
ws.send(JSON.stringify(sendItem.data), function (error) {
|
|
188
|
-
if (error) {
|
|
189
|
-
if (_this.getEnableDebug()) {
|
|
190
|
-
console.log(new Date(), 'Sub Manager', 'Unsub WS', ws['user'], ws['id_socket'], 1);
|
|
191
|
-
}
|
|
192
|
-
_this.unsubscribeAll(ws);
|
|
193
|
-
}
|
|
194
|
-
});
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
else {
|
|
198
|
-
this_1._sendQueue.splice(i, 1);
|
|
199
|
-
}
|
|
200
|
-
};
|
|
201
|
-
this_1 = this;
|
|
202
|
-
// let startDate = new Date();
|
|
203
|
-
for (i = this._sendQueue.length - 1; i >= 0; i--) {
|
|
204
|
-
_loop_1(i);
|
|
205
|
-
}
|
|
206
|
-
// let endDate = new Date();
|
|
207
|
-
// this.currentPerfomanceMonitor.push({
|
|
208
|
-
// _id: this.idPerformance++,
|
|
209
|
-
// function: 'sendInterval',
|
|
210
|
-
// publication: '',
|
|
211
|
-
// subscriptionData: [],
|
|
212
|
-
// date_start: startDate,
|
|
213
|
-
// date_end: endDate,
|
|
214
|
-
// duration: moment(endDate).diff(moment(startDate), 'milliseconds', false),
|
|
215
|
-
// result: 'Done'
|
|
216
|
-
// });
|
|
217
|
-
}
|
|
218
|
-
queueArr = this._mongoQueue.filter(function (a) { return !a.running; });
|
|
219
|
-
if (queueArr.length) {
|
|
220
|
-
queueArr.forEach(function (entry) {
|
|
221
|
-
entry.running = true;
|
|
222
|
-
});
|
|
223
|
-
_loop_2 = function (i) {
|
|
224
|
-
this_2._debugMongoQueueHits += 1;
|
|
225
|
-
var mongoQueue = queueArr[i];
|
|
226
|
-
if (!this_2._debugMongoQueueCollections.some(function (a) { return a.collection === mongoQueue.collection && a.publication === mongoQueue.subscription.publication; })) {
|
|
227
|
-
this_2._debugMongoQueueCollections.push({
|
|
228
|
-
collection: mongoQueue.collection,
|
|
229
|
-
publication: mongoQueue.subscription.publication,
|
|
230
|
-
hits: 1
|
|
231
|
-
});
|
|
232
|
-
}
|
|
233
|
-
else {
|
|
234
|
-
this_2._debugMongoQueueCollections.filter(function (a) { return a.collection === mongoQueue.collection && a.publication === mongoQueue.subscription.publication; })[0].hits += 1;
|
|
235
|
-
}
|
|
236
|
-
if (this_2._publications[mongoQueue.subscription.publication].user_specific) {
|
|
237
|
-
var promises = [];
|
|
238
|
-
var _loop_3 = function (j) {
|
|
239
|
-
var client = mongoQueue.subscription.clients[j];
|
|
240
|
-
var ws = this_2._mainServer.getWS(client.id_socket);
|
|
241
|
-
if (ws && ws['id_socket'] === client.id_socket) {
|
|
242
|
-
promises.push(this_2.sendDataToOne(ws, client.messageId, mongoQueue.subscription, mongoQueue.type, mongoQueue.collection).catch(function (err) {
|
|
243
|
-
_this._mainServer.getMethodManager().sendEmail('dev@resolveio.com', 'SERVER - Error Detected - ' + _this.serverConfig['CLIENT_NAME'], 'Error Detected During sendDataToOne - User Specific - Socket: ' + client.id_socket + ', User: ' + client.id_user + ', MessageId: ' + client.messageId + ', Pub: ' + mongoQueue.subscription.publication + ', Err: ' + JSON.stringify(err, null, 2));
|
|
244
|
-
return null;
|
|
245
|
-
}));
|
|
246
|
-
}
|
|
247
|
-
};
|
|
248
|
-
for (var j = 0; j < mongoQueue.subscription.clients.length; j++) {
|
|
249
|
-
_loop_3(j);
|
|
250
|
-
}
|
|
251
|
-
Promise.all(promises).then(function () {
|
|
252
|
-
if (mongoQueue.run_again) {
|
|
253
|
-
mongoQueue.running = false;
|
|
254
|
-
mongoQueue.run_again = false;
|
|
255
|
-
}
|
|
256
|
-
else {
|
|
257
|
-
if (_this._mongoQueue.map(function (a) { return a._id; }).indexOf(mongoQueue._id) >= 0) {
|
|
258
|
-
_this._mongoQueue.splice(_this._mongoQueue.map(function (a) { return a._id; }).indexOf(mongoQueue._id), 1);
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
});
|
|
262
|
-
}
|
|
263
|
-
else {
|
|
264
|
-
this_2.sendDataToAll(mongoQueue).then(function () {
|
|
265
|
-
if (mongoQueue.run_again) {
|
|
266
|
-
mongoQueue.running = false;
|
|
267
|
-
mongoQueue.run_again = false;
|
|
268
|
-
}
|
|
269
|
-
else {
|
|
270
|
-
if (_this._mongoQueue.map(function (a) { return a._id; }).indexOf(mongoQueue._id) >= 0) {
|
|
271
|
-
_this._mongoQueue.splice(_this._mongoQueue.map(function (a) { return a._id; }).indexOf(mongoQueue._id), 1);
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
}, function () {
|
|
275
|
-
if (_this._mongoQueue.map(function (a) { return a._id; }).indexOf(mongoQueue._id) >= 0) {
|
|
276
|
-
_this._mongoQueue.splice(_this._mongoQueue.map(function (a) { return a._id; }).indexOf(mongoQueue._id), 1);
|
|
277
|
-
}
|
|
278
|
-
});
|
|
279
|
-
}
|
|
280
|
-
};
|
|
281
|
-
this_2 = this;
|
|
282
|
-
for (i = queueArr.length - 1; i >= 0; i--) {
|
|
283
|
-
_loop_2(i);
|
|
284
|
-
}
|
|
285
|
-
// let endDate = new Date();
|
|
286
|
-
// this.currentPerfomanceMonitor.push({
|
|
287
|
-
// _id: this.idPerformance++,
|
|
288
|
-
// function: 'mongoQueue',
|
|
289
|
-
// publication: '',
|
|
290
|
-
// subscriptionData: [],
|
|
291
|
-
// date_start: startDate,
|
|
292
|
-
// date_end: endDate,
|
|
293
|
-
// duration: moment(endDate).diff(moment(startDate), 'milliseconds', false),
|
|
294
|
-
// result: 'Done'
|
|
295
|
-
// });
|
|
296
|
-
}
|
|
297
|
-
this._runningQueue = false;
|
|
298
|
-
}
|
|
299
|
-
return [2 /*return*/];
|
|
300
|
-
});
|
|
301
|
-
}); }, 25);
|
|
302
|
-
setInterval(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
303
|
-
var _a, userCopy, _loop_4, this_3, i, i, sub, _loop_5, this_4, j;
|
|
304
|
-
return __generator(this, function (_b) {
|
|
305
|
-
switch (_b.label) {
|
|
306
|
-
case 0:
|
|
307
|
-
_a = this;
|
|
308
|
-
return [4 /*yield*/, logged_in_users_collection_1.LoggedInUsers.find()];
|
|
309
|
-
case 1:
|
|
310
|
-
_a._loggedInUsers = _b.sent();
|
|
311
|
-
userCopy = (0, common_1.deepCopy)(this._loggedInUsers);
|
|
312
|
-
_loop_4 = function (i) {
|
|
313
|
-
var loggedInUser = userCopy[i];
|
|
314
|
-
if (!loggedInUser.date || Date.now() - loggedInUser.date.getTime() > 120000) {
|
|
315
|
-
if (this_3._mainServer.getWS(loggedInUser.id_ws)) {
|
|
316
|
-
if (this_3.getEnableDebug()) {
|
|
317
|
-
console.log(new Date(), 'Sub Manager', 'Unsub WS', this_3._mainServer.getWS(loggedInUser.id_ws)['user'], this_3._mainServer.getWS(loggedInUser.id_ws)['id_socket'], 2);
|
|
318
|
-
}
|
|
319
|
-
this_3.unsubscribeAll(this_3._mainServer.getWS(loggedInUser.id_ws));
|
|
320
|
-
}
|
|
321
|
-
else {
|
|
322
|
-
this_3._subscriptions.forEach(function (sub) {
|
|
323
|
-
for (var j = sub.clients.length - 1; j >= 0; j--) {
|
|
324
|
-
var client = sub.clients[j];
|
|
325
|
-
if (client.id_socket === loggedInUser.id_ws) {
|
|
326
|
-
sub.clients.splice(j, 1);
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
});
|
|
330
|
-
logged_in_users_collection_1.LoggedInUsers.deleteOne({ _id: loggedInUser._id });
|
|
331
|
-
if (this_3._loggedInUsers.findIndex(function (a) { return a._id === loggedInUser._id; }) >= 0) {
|
|
332
|
-
this_3._loggedInUsers.splice(this_3._loggedInUsers.findIndex(function (a) { return a._id === loggedInUser._id; }), 1);
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
}
|
|
336
|
-
};
|
|
337
|
-
this_3 = this;
|
|
338
|
-
for (i = this._loggedInUsers.length - 1; i >= 0; i--) {
|
|
339
|
-
_loop_4(i);
|
|
340
|
-
}
|
|
341
|
-
for (i = 0; i < this._subscriptions.length; i++) {
|
|
342
|
-
sub = this._subscriptions[i];
|
|
343
|
-
_loop_5 = function (j) {
|
|
344
|
-
var client = sub.clients[j];
|
|
345
|
-
if (!this_4._loggedInUsers.some(function (a) { return a.id_ws === client.id_socket; })) {
|
|
346
|
-
sub.clients.splice(j, 1);
|
|
347
|
-
}
|
|
348
|
-
};
|
|
349
|
-
this_4 = this;
|
|
350
|
-
for (j = sub.clients.length - 1; j >= 0; j--) {
|
|
351
|
-
_loop_5(j);
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
return [2 /*return*/];
|
|
355
|
-
}
|
|
356
|
-
});
|
|
357
|
-
}); }, 30000);
|
|
358
|
-
flag_collection_1.Flags.findOne({ type: 'Enable Debug' }).then(function (flag) {
|
|
359
|
-
if (flag && flag.value) {
|
|
360
|
-
_this._enableDebug = true;
|
|
361
|
-
}
|
|
362
|
-
else {
|
|
363
|
-
_this._enableDebug = false;
|
|
364
|
-
}
|
|
365
|
-
});
|
|
366
|
-
}
|
|
367
|
-
SubscriptionManager.prototype.addToQueue = function (mongoQueue) {
|
|
368
|
-
// let startDate = new Date();
|
|
369
|
-
var _mongoQueue = this._mongoQueue.filter(function (a) { return a.subscription.publication === mongoQueue.subscription.publication && JSON.stringify(a.subscription.subscriptionData) === JSON.stringify(mongoQueue.subscription.subscriptionData); })[0];
|
|
370
|
-
if (_mongoQueue) {
|
|
371
|
-
if (_mongoQueue.running) {
|
|
372
|
-
_mongoQueue.run_again = true;
|
|
373
|
-
// let endDate = new Date();
|
|
374
|
-
// this.currentPerfomanceMonitor.push({
|
|
375
|
-
// _id: this.idPerformance++,
|
|
376
|
-
// function: 'addToQueue',
|
|
377
|
-
// publication: mongoQueue.subscription.publication,
|
|
378
|
-
// // subscriptionData: mongoQueue.subscription.subscriptionData,
|
|
379
|
-
// subscriptionData: [],
|
|
380
|
-
// date_start: startDate,
|
|
381
|
-
// date_end: endDate,
|
|
382
|
-
// duration: moment(endDate).diff(moment(startDate), 'milliseconds', false),
|
|
383
|
-
// result: 'Run Again'
|
|
384
|
-
// });
|
|
385
|
-
}
|
|
386
|
-
// else {
|
|
387
|
-
// let endDate = new Date();
|
|
388
|
-
// this.currentPerfomanceMonitor.push({
|
|
389
|
-
// _id: this.idPerformance++,
|
|
390
|
-
// function: 'addToQueue',
|
|
391
|
-
// publication: mongoQueue.subscription.publication,
|
|
392
|
-
// // subscriptionData: mongoQueue.subscription.subscriptionData,
|
|
393
|
-
// subscriptionData: [],
|
|
394
|
-
// date_start: startDate,
|
|
395
|
-
// date_end: endDate,
|
|
396
|
-
// duration: moment(endDate).diff(moment(startDate), 'milliseconds', false),
|
|
397
|
-
// result: 'Not Running Yet'
|
|
398
|
-
// });
|
|
399
|
-
// }
|
|
400
|
-
}
|
|
401
|
-
else {
|
|
402
|
-
mongoQueue._id = this._mongoQueueId;
|
|
403
|
-
this._mongoQueueId += 1;
|
|
404
|
-
this._mongoQueue.splice(0, 0, mongoQueue);
|
|
405
|
-
// let endDate = new Date();
|
|
406
|
-
// this.currentPerfomanceMonitor.push({
|
|
407
|
-
// _id: this.idPerformance++,
|
|
408
|
-
// function: 'addToQueue',
|
|
409
|
-
// publication: mongoQueue.subscription.publication,
|
|
410
|
-
// // subscriptionData: mongoQueue.subscription.subscriptionData,
|
|
411
|
-
// subscriptionData: [],
|
|
412
|
-
// date_start: startDate,
|
|
413
|
-
// date_end: endDate,
|
|
414
|
-
// duration: moment(endDate).diff(moment(startDate), 'milliseconds', false),
|
|
415
|
-
// result: 'Add to Queue'
|
|
416
|
-
// });
|
|
417
|
-
}
|
|
418
|
-
};
|
|
419
|
-
SubscriptionManager.prototype.invalidatePubsCache = function (collection, type) {
|
|
420
|
-
// let startDate = new Date();
|
|
421
|
-
index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(collection);
|
|
422
|
-
var collSubs = this._subscriptions.filter(function (a) { return a.collections.includes(collection); });
|
|
423
|
-
for (var i = collSubs.length - 1; i >= 0; i--) {
|
|
424
|
-
var sub = collSubs[i];
|
|
425
|
-
this.addToQueue({
|
|
426
|
-
_id: 0,
|
|
427
|
-
type: type,
|
|
428
|
-
collection: collection,
|
|
429
|
-
subscription: sub,
|
|
430
|
-
running: false,
|
|
431
|
-
run_again: false
|
|
432
|
-
});
|
|
433
|
-
}
|
|
434
|
-
// let endDate = new Date();
|
|
435
|
-
// this.currentPerfomanceMonitor.push({
|
|
436
|
-
// _id: this.idPerformance++,
|
|
437
|
-
// function: 'invalidatePubsCache',
|
|
438
|
-
// publication: collection,
|
|
439
|
-
// subscriptionData: [],
|
|
440
|
-
// date_start: startDate,
|
|
441
|
-
// date_end: endDate,
|
|
442
|
-
// duration: moment(endDate).diff(moment(startDate), 'milliseconds', false),
|
|
443
|
-
// result: 'Done'
|
|
444
|
-
// });
|
|
445
|
-
};
|
|
446
|
-
// Add all files to publications private object
|
|
447
|
-
SubscriptionManager.prototype.publications = function (method) {
|
|
448
|
-
this._publications = Object.assign(this._publications, method);
|
|
449
|
-
};
|
|
450
|
-
SubscriptionManager.prototype.loggedInLatency = function (ws) {
|
|
451
|
-
var _this = this;
|
|
452
|
-
var loggedInUser = this._loggedInUsers.filter(function (a) { return a.id_ws === ws['id_socket']; })[0];
|
|
453
|
-
if (loggedInUser) {
|
|
454
|
-
loggedInUser.date = new Date();
|
|
455
|
-
logged_in_users_collection_1.LoggedInUsers.updateOne({ id_ws: ws['id_socket'] }, { $set: { latency: ws['latency'], date: loggedInUser.date } }).then(function (res) {
|
|
456
|
-
if (!res) {
|
|
457
|
-
_this.unsubscribeAll(ws);
|
|
458
|
-
if (_this.getEnableDebug()) {
|
|
459
|
-
console.log(new Date(), 'Sub Manager', 'Unsub WS', ws['user'], ws['id_socket'], 3);
|
|
460
|
-
}
|
|
461
|
-
}
|
|
462
|
-
}, function () {
|
|
463
|
-
_this.unsubscribeAll(ws);
|
|
464
|
-
if (_this.getEnableDebug()) {
|
|
465
|
-
console.log(new Date(), 'Sub Manager', 'Unsub WS', ws['user'], ws['id_socket'], 4);
|
|
466
|
-
}
|
|
467
|
-
});
|
|
468
|
-
}
|
|
469
|
-
};
|
|
470
|
-
// Subscribe to publication
|
|
471
|
-
SubscriptionManager.prototype.subscribe = function (messageRoute, messageDate, ws, messageId, publication, subscriptionData) {
|
|
472
|
-
var _this = this;
|
|
473
|
-
// let startDate = new Date();
|
|
474
|
-
this._debugSubHits += 1;
|
|
475
|
-
if (!this._debugSubCollections.some(function (a) { return a.publication === publication; })) {
|
|
476
|
-
this._debugSubCollections.push({
|
|
477
|
-
publication: publication,
|
|
478
|
-
hits: 1
|
|
479
|
-
});
|
|
480
|
-
}
|
|
481
|
-
else {
|
|
482
|
-
this._debugSubCollections.filter(function (a) { return a.publication === publication; })[0].hits += 1;
|
|
483
|
-
}
|
|
484
|
-
var pub = this._publications[publication];
|
|
485
|
-
if (!pub) {
|
|
486
|
-
console.error(new Date(), 'No Publication: ' + publication);
|
|
487
|
-
return;
|
|
488
|
-
}
|
|
489
|
-
else {
|
|
490
|
-
if (subscriptionData.length > 1 || subscriptionData[0]) {
|
|
491
|
-
if (!pub.check) {
|
|
492
|
-
console.error(new Date(), 'No Check Function For Pub ' + publication);
|
|
493
|
-
return;
|
|
494
|
-
}
|
|
495
|
-
else if (!pub.check._schema) {
|
|
496
|
-
console.error(new Date(), 'No Check Schema For Pub ' + publication);
|
|
497
|
-
return;
|
|
498
|
-
}
|
|
499
|
-
else {
|
|
500
|
-
var valObj = {};
|
|
501
|
-
var valKeys = Object.keys(pub.check._schema);
|
|
502
|
-
var rootKeys = valKeys.filter(function (a) { return !a.includes('.'); });
|
|
503
|
-
for (var i = 0; i < subscriptionData.length; i++) {
|
|
504
|
-
valObj[rootKeys[i]] = subscriptionData[i];
|
|
505
|
-
}
|
|
506
|
-
try {
|
|
507
|
-
pub.check.validate(valObj);
|
|
508
|
-
}
|
|
509
|
-
catch (errors) {
|
|
510
|
-
if (errors) {
|
|
511
|
-
console.error(new Date(), 'Error in Pub Check (' + publication + ')', errors);
|
|
512
|
-
return;
|
|
513
|
-
}
|
|
514
|
-
}
|
|
515
|
-
}
|
|
516
|
-
}
|
|
517
|
-
if (messageRoute !== 'Bypass') {
|
|
518
|
-
var urlData = messageRoute.split('/');
|
|
519
|
-
var urlModule_1 = '';
|
|
520
|
-
var urlNext = urlData[0];
|
|
521
|
-
if (urlData[0] === '') {
|
|
522
|
-
urlModule_1 = '/';
|
|
523
|
-
urlNext = urlData[1];
|
|
524
|
-
}
|
|
525
|
-
urlModule_1 += urlNext;
|
|
526
|
-
if (urlData.length > 1) {
|
|
527
|
-
urlModule_1 += '/';
|
|
528
|
-
}
|
|
529
|
-
var otherRouteSubs = this._subscriptions.filter(function (a) { return a.clients.some(function (b) { return b.id_socket === ws['id_socket'] && b.messageRoute !== 'Bypass' && b.messageRoute !== '/' && b.messageRoute !== messageRoute && !b.messageRoute.startsWith(urlModule_1); }); });
|
|
530
|
-
if (otherRouteSubs.length) {
|
|
531
|
-
this._mainServer.getMethodManager().sendEmail('dev@resolveio.com', 'SERVER - Detected Undestroyed Subscription - ' + this.serverConfig['CLIENT_NAME'], 'USER: ' + ws['user'] + ' (Socket: ' + ws['id_socket'] + ') ' + ' is on route: ' + messageRoute + ' but has the following subscriptions on other routes:' + JSON.stringify(otherRouteSubs, null, 2));
|
|
532
|
-
otherRouteSubs.forEach(function (otherSub) {
|
|
533
|
-
otherSub.clients.filter(function (a) { return a.id_socket === ws['id_socket']; }).forEach(function (client) {
|
|
534
|
-
_this.unsubscribe(client.messageRoute, new Date(), ws, client.messageId, otherSub.publication, otherSub.subscriptionData);
|
|
535
|
-
});
|
|
536
|
-
});
|
|
537
|
-
}
|
|
538
|
-
}
|
|
539
|
-
var sub_1 = this._subscriptions.filter(function (a) { return a.publication === publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscriptionData); })[0];
|
|
540
|
-
// If sub found (another user watching same data), add client to same sub
|
|
541
|
-
if (sub_1) {
|
|
542
|
-
if (!sub_1.clients.some(function (a) { return a.id_socket === ws['id_socket'] && a.messageId === messageId; })) {
|
|
543
|
-
sub_1.clients.push({
|
|
544
|
-
id_user: ws['id_user'],
|
|
545
|
-
messageId: messageId,
|
|
546
|
-
id_socket: ws['id_socket'],
|
|
547
|
-
messageRoute: messageRoute
|
|
548
|
-
});
|
|
549
|
-
}
|
|
550
|
-
}
|
|
551
|
-
// If sub not found, create new sub
|
|
552
|
-
else {
|
|
553
|
-
this._subscriptions.push({
|
|
554
|
-
publication: publication,
|
|
555
|
-
subscriptionData: subscriptionData,
|
|
556
|
-
collections: this.getPublicationCollections(publication),
|
|
557
|
-
clients: [{
|
|
558
|
-
id_user: ws['id_user'],
|
|
559
|
-
messageId: messageId,
|
|
560
|
-
id_socket: ws['id_socket'],
|
|
561
|
-
messageRoute: messageRoute
|
|
562
|
-
}],
|
|
563
|
-
cacheId: 0
|
|
564
|
-
});
|
|
565
|
-
}
|
|
566
|
-
if (!sub_1) {
|
|
567
|
-
sub_1 = this._subscriptions.filter(function (a) { return a.publication === publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscriptionData); })[0];
|
|
568
|
-
}
|
|
569
|
-
if (!sub_1.cacheId && !this._mongoQueue.some(function (a) { return a.subscription.publication === sub_1.publication && JSON.stringify(a.subscription.subscriptionData) === JSON.stringify(sub_1.subscriptionData); })) {
|
|
570
|
-
this.addToQueue({
|
|
571
|
-
_id: 0,
|
|
572
|
-
type: 'newSub',
|
|
573
|
-
collection: '',
|
|
574
|
-
subscription: sub_1,
|
|
575
|
-
running: false,
|
|
576
|
-
run_again: false
|
|
577
|
-
});
|
|
578
|
-
// let endDate = new Date();
|
|
579
|
-
// this.currentPerfomanceMonitor.push({
|
|
580
|
-
// _id: this.idPerformance++,
|
|
581
|
-
// function: 'subscribe',
|
|
582
|
-
// publication: publication,
|
|
583
|
-
// // subscriptionData: subscriptionData,
|
|
584
|
-
// subscriptionData: [],
|
|
585
|
-
// date_start: startDate,
|
|
586
|
-
// date_end: endDate,
|
|
587
|
-
// duration: moment(endDate).diff(moment(startDate), 'milliseconds', false),
|
|
588
|
-
// result: 'Add to Queue'
|
|
589
|
-
// });
|
|
590
|
-
}
|
|
591
|
-
else if (sub_1.cacheId) {
|
|
592
|
-
this.sendDataToOne(ws, messageId, sub_1, 'newSub', publication);
|
|
593
|
-
// let endDate = new Date();
|
|
594
|
-
// this.currentPerfomanceMonitor.push({
|
|
595
|
-
// _id: this.idPerformance++,
|
|
596
|
-
// function: 'subscribe',
|
|
597
|
-
// publication: publication,
|
|
598
|
-
// // subscriptionData: subscriptionData,
|
|
599
|
-
// subscriptionData: [],
|
|
600
|
-
// date_start: startDate,
|
|
601
|
-
// date_end: endDate,
|
|
602
|
-
// duration: moment(endDate).diff(moment(startDate), 'milliseconds', false),
|
|
603
|
-
// result: 'Send Pub Data Once'
|
|
604
|
-
// });
|
|
605
|
-
}
|
|
606
|
-
}
|
|
607
|
-
};
|
|
608
|
-
SubscriptionManager.prototype.createLoggedInUser = function (id_ws) {
|
|
609
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
610
|
-
var _this = this;
|
|
611
|
-
return __generator(this, function (_a) {
|
|
612
|
-
return [2 /*return*/, new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
|
|
613
|
-
var ws, user;
|
|
614
|
-
return __generator(this, function (_a) {
|
|
615
|
-
ws = this._mainServer.getWS(id_ws);
|
|
616
|
-
if (ws) {
|
|
617
|
-
user = {
|
|
618
|
-
_id: (0, mongo_manager_1.objectIdHexString)(),
|
|
619
|
-
__v: 0,
|
|
620
|
-
date: new Date(),
|
|
621
|
-
id_user: ws['id_user'],
|
|
622
|
-
user: ws['user'],
|
|
623
|
-
id_ws: ws['id_socket']
|
|
624
|
-
};
|
|
625
|
-
this._loggedInUsers.push(user);
|
|
626
|
-
logged_in_users_collection_1.LoggedInUsers.insertOne(user);
|
|
627
|
-
resolve(user);
|
|
628
|
-
}
|
|
629
|
-
else {
|
|
630
|
-
resolve(null);
|
|
631
|
-
}
|
|
632
|
-
return [2 /*return*/];
|
|
633
|
-
});
|
|
634
|
-
}); })];
|
|
635
|
-
});
|
|
636
|
-
});
|
|
637
|
-
};
|
|
638
|
-
// Unsubscribe from publication
|
|
639
|
-
SubscriptionManager.prototype.unsubscribe = function (messageRoute, messageDate, ws, messageId, publication, subscriptionData) {
|
|
640
|
-
this._debugUnSubHits += 1;
|
|
641
|
-
// let startDate = new Date();
|
|
642
|
-
// console.log('Before');
|
|
643
|
-
// console.dir(this._subscriptions.map(a => a.publication));
|
|
644
|
-
if (!this._publications[publication]) {
|
|
645
|
-
console.log('No Publication: ' + publication);
|
|
646
|
-
return;
|
|
647
|
-
}
|
|
648
|
-
else {
|
|
649
|
-
var sub = this._subscriptions.filter(function (a) { return a.publication === publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscriptionData); })[0];
|
|
650
|
-
if (sub) {
|
|
651
|
-
for (var i = sub.clients.length - 1; i >= 0; i--) {
|
|
652
|
-
if (sub.clients[i].id_user === ws['id_user'] && sub.clients[i].messageId === messageId && sub.clients[i].id_socket === ws['id_socket']) {
|
|
653
|
-
sub.clients.splice(i, 1);
|
|
654
|
-
}
|
|
655
|
-
}
|
|
656
|
-
}
|
|
657
|
-
}
|
|
658
|
-
// let endDate = new Date();
|
|
659
|
-
// this.currentPerfomanceMonitor.push({
|
|
660
|
-
// _id: this.idPerformance++,
|
|
661
|
-
// function: 'unsubscribe',
|
|
662
|
-
// publication: publication,
|
|
663
|
-
// // subscriptionData: subscriptionData,
|
|
664
|
-
// subscriptionData: [],
|
|
665
|
-
// date_start: startDate,
|
|
666
|
-
// date_end: endDate,
|
|
667
|
-
// duration: moment(endDate).diff(moment(startDate), 'milliseconds', false),
|
|
668
|
-
// result: 'Done'
|
|
669
|
-
// });
|
|
670
|
-
};
|
|
671
|
-
// Unsubscribe from publication
|
|
672
|
-
SubscriptionManager.prototype.unsubscribeAll = function (ws) {
|
|
673
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
674
|
-
var loggedInUser, userSubs, i, sub, j;
|
|
675
|
-
return __generator(this, function (_a) {
|
|
676
|
-
switch (_a.label) {
|
|
677
|
-
case 0:
|
|
678
|
-
this._debugUnSubAllHits += 1;
|
|
679
|
-
if (!ws) return [3 /*break*/, 2];
|
|
680
|
-
return [4 /*yield*/, logged_in_users_collection_1.LoggedInUsers.findOne({ id_ws: ws['id_socket'] })];
|
|
681
|
-
case 1:
|
|
682
|
-
loggedInUser = _a.sent();
|
|
683
|
-
if (loggedInUser) {
|
|
684
|
-
if (this._loggedInUsers.map(function (a) { return a._id; }).indexOf(loggedInUser._id) >= 0) {
|
|
685
|
-
this._loggedInUsers.splice(this._loggedInUsers.map(function (a) { return a._id; }).indexOf(loggedInUser._id), 1);
|
|
686
|
-
}
|
|
687
|
-
logged_in_users_collection_1.LoggedInUsers.deleteOne({ _id: loggedInUser._id });
|
|
688
|
-
}
|
|
689
|
-
userSubs = this._subscriptions.filter(function (a) { return a.clients.some(function (b) { return b.id_user === ws['id_user'] && b.id_socket === ws['id_socket']; }); });
|
|
690
|
-
for (i = userSubs.length - 1; i >= 0; i--) {
|
|
691
|
-
sub = userSubs[i];
|
|
692
|
-
for (j = sub.clients.length - 1; j >= 0; j--) {
|
|
693
|
-
if (sub.clients[j].id_socket === ws['id_socket']) {
|
|
694
|
-
sub.clients.splice(j, 1);
|
|
695
|
-
}
|
|
696
|
-
}
|
|
697
|
-
}
|
|
698
|
-
ws.close();
|
|
699
|
-
process.nextTick(function () {
|
|
700
|
-
if ([ws.OPEN, ws.CLOSING].includes(ws.readyState)) {
|
|
701
|
-
// Socket still hangs, hard close
|
|
702
|
-
ws.terminate();
|
|
703
|
-
}
|
|
704
|
-
});
|
|
705
|
-
_a.label = 2;
|
|
706
|
-
case 2: return [2 /*return*/];
|
|
707
|
-
}
|
|
708
|
-
});
|
|
709
|
-
});
|
|
710
|
-
};
|
|
711
|
-
SubscriptionManager.prototype.getActiveSubscriptions = function () {
|
|
712
|
-
return this._subscriptions;
|
|
713
|
-
};
|
|
714
|
-
// Get publication collection
|
|
715
|
-
SubscriptionManager.prototype.getPublicationCollections = function (publication) {
|
|
716
|
-
return this._publications[publication].collections;
|
|
717
|
-
};
|
|
718
|
-
// Watch (tail) Mongo's operation log on the entire database (all insert/modify/delete will trigger this function)
|
|
719
|
-
SubscriptionManager.prototype.tailOpLog = function (resumeToken) {
|
|
720
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
721
|
-
var lastResumeToken_1;
|
|
722
|
-
var _this = this;
|
|
723
|
-
return __generator(this, function (_a) {
|
|
724
|
-
switch (_a.label) {
|
|
725
|
-
case 0:
|
|
726
|
-
if (this._oplog$ && !this._oplog$.closed) {
|
|
727
|
-
this._oplog$.removeAllListeners();
|
|
728
|
-
this._oplog$.close();
|
|
729
|
-
this._oplog$ = null;
|
|
730
|
-
}
|
|
731
|
-
return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 1000); })];
|
|
732
|
-
case 1:
|
|
733
|
-
_a.sent();
|
|
734
|
-
if (!this._oplog$ || this._oplog$.closed) {
|
|
735
|
-
this._oplogRetryCount += 1;
|
|
736
|
-
if (this._oplogRetryCount > 5) {
|
|
737
|
-
console.error('****************** TAIL OPLOG ERROR, RETRYING A BUNCH OF TIMES, KILLING PROCESS **************************');
|
|
738
|
-
process.exit(1);
|
|
739
|
-
}
|
|
740
|
-
if (resumeToken) {
|
|
741
|
-
lastResumeToken_1 = resumeToken;
|
|
742
|
-
try {
|
|
743
|
-
this._oplog$ = index_1.ResolveIOServer.getMainDB().watch([], { resumeAfter: resumeToken });
|
|
744
|
-
}
|
|
745
|
-
catch (errOp) {
|
|
746
|
-
if (this._oplog$) {
|
|
747
|
-
this._oplog$.removeAllListeners();
|
|
748
|
-
this._oplog$.close();
|
|
749
|
-
this._oplog$ = null;
|
|
750
|
-
}
|
|
751
|
-
this.tailOpLog(resumeToken);
|
|
752
|
-
return [2 /*return*/];
|
|
753
|
-
}
|
|
754
|
-
}
|
|
755
|
-
else {
|
|
756
|
-
this._oplog$ = index_1.ResolveIOServer.getMainDB().watch();
|
|
757
|
-
}
|
|
758
|
-
console.log(new Date(), 'oplog started');
|
|
759
|
-
this._oplog$.on('change', function (doc) {
|
|
760
|
-
// let startDate = new Date();
|
|
761
|
-
if (doc.ns) {
|
|
762
|
-
var collection = doc.ns.coll;
|
|
763
|
-
if (!_this._debugOplogCollections.some(function (a) { return a.collection === doc.ns.coll && a.type === doc.operationType; })) {
|
|
764
|
-
_this._debugOplogCollections.push({
|
|
765
|
-
collection: doc.ns.coll,
|
|
766
|
-
type: doc.operationType,
|
|
767
|
-
hits: 1
|
|
768
|
-
});
|
|
769
|
-
}
|
|
770
|
-
else {
|
|
771
|
-
_this._debugOplogCollections.filter(function (a) { return a.collection === doc.ns.coll && a.type === doc.operationType; })[0].hits += 1;
|
|
772
|
-
}
|
|
773
|
-
// console.log(new Date(), 'Op log doc', doc.operationType, collection);
|
|
774
|
-
if (collection && !collection.endsWith('.versions') && !collection.startsWith('monitor-') && collection !== 'logs' && collection !== 'log-method-latencies' && collection !== 'log-subscriptions') {
|
|
775
|
-
_this._debugOplogHits += 1;
|
|
776
|
-
if (doc.operationType === 'insert') {
|
|
777
|
-
if (collection === 'support-tickets') {
|
|
778
|
-
if (_this.serverConfig['ROOT_URL'] === 'https://resolveio.com') {
|
|
779
|
-
_this._mainServer.getMethodManager().callMethodInternal.call(_this._mainServer.getMethodManager(), 'sendSupportTicketEmail', doc.documentKey['_id']);
|
|
780
|
-
_this.invalidatePubsCache(collection, 'insert');
|
|
781
|
-
}
|
|
782
|
-
}
|
|
783
|
-
if (collection !== 'method-responses') {
|
|
784
|
-
_this.invalidatePubsCache(collection, 'insert');
|
|
785
|
-
}
|
|
786
|
-
// let endDate = new Date();
|
|
787
|
-
// this.currentPerfomanceMonitor.push({
|
|
788
|
-
// _id: this.idPerformance++,
|
|
789
|
-
// function: 'oplog',
|
|
790
|
-
// publication: collection,
|
|
791
|
-
// subscriptionData: [],
|
|
792
|
-
// date_start: startDate,
|
|
793
|
-
// date_end: endDate,
|
|
794
|
-
// duration: moment(endDate).diff(moment(startDate), 'milliseconds', false),
|
|
795
|
-
// result: 'Insert'
|
|
796
|
-
// });
|
|
797
|
-
}
|
|
798
|
-
else if (doc.operationType === 'update' || doc.operationType === 'replace') {
|
|
799
|
-
if (collection !== 'method-responses') {
|
|
800
|
-
_this.invalidatePubsCache(collection, 'update');
|
|
801
|
-
}
|
|
802
|
-
// let endDate = new Date();
|
|
803
|
-
// this.currentPerfomanceMonitor.push({
|
|
804
|
-
// _id: this.idPerformance++,
|
|
805
|
-
// function: 'oplog',
|
|
806
|
-
// publication: collection,
|
|
807
|
-
// subscriptionData: [],
|
|
808
|
-
// date_start: startDate,
|
|
809
|
-
// date_end: endDate,
|
|
810
|
-
// duration: moment(endDate).diff(moment(startDate), 'milliseconds', false),
|
|
811
|
-
// result: 'Update'
|
|
812
|
-
// });
|
|
813
|
-
}
|
|
814
|
-
else if (doc.operationType === 'delete') {
|
|
815
|
-
if (collection !== 'method-responses') {
|
|
816
|
-
_this.invalidatePubsCache(collection, 'delete');
|
|
817
|
-
}
|
|
818
|
-
// let endDate = new Date();
|
|
819
|
-
// this.currentPerfomanceMonitor.push({
|
|
820
|
-
// _id: this.idPerformance++,
|
|
821
|
-
// function: 'oplog',
|
|
822
|
-
// publication: collection,
|
|
823
|
-
// subscriptionData: [],
|
|
824
|
-
// date_start: startDate,
|
|
825
|
-
// date_end: endDate,
|
|
826
|
-
// duration: moment(endDate).diff(moment(startDate), 'milliseconds', false),
|
|
827
|
-
// result: 'Delete'
|
|
828
|
-
// });
|
|
829
|
-
}
|
|
830
|
-
}
|
|
831
|
-
if (collection === 'flags') {
|
|
832
|
-
flag_collection_1.Flags.findOne({ type: 'Enable Debug' }).then(function (flag) {
|
|
833
|
-
if (flag && flag.value) {
|
|
834
|
-
_this._enableDebug = true;
|
|
835
|
-
}
|
|
836
|
-
else {
|
|
837
|
-
_this._enableDebug = false;
|
|
838
|
-
}
|
|
839
|
-
});
|
|
840
|
-
}
|
|
841
|
-
lastResumeToken_1 = doc._id;
|
|
842
|
-
}
|
|
843
|
-
});
|
|
844
|
-
this._oplog$.on('error', function (error) {
|
|
845
|
-
console.log(new Date(), 'oplog error', error);
|
|
846
|
-
_this._oplog$.removeAllListeners();
|
|
847
|
-
_this._oplog$.close();
|
|
848
|
-
_this._oplog$ = null;
|
|
849
|
-
_this.tailOpLog(lastResumeToken_1);
|
|
850
|
-
});
|
|
851
|
-
this._oplog$.on('end', function () {
|
|
852
|
-
console.log(new Date(), 'oplog end');
|
|
853
|
-
_this._oplog$.removeAllListeners();
|
|
854
|
-
_this._oplog$.close();
|
|
855
|
-
_this._oplog$ = null;
|
|
856
|
-
_this.tailOpLog(lastResumeToken_1);
|
|
857
|
-
});
|
|
858
|
-
this._oplog$.on('close', function () {
|
|
859
|
-
console.log(new Date(), 'oplog close');
|
|
860
|
-
_this._oplog$.removeAllListeners();
|
|
861
|
-
_this._oplog$ = null;
|
|
862
|
-
_this.tailOpLog(lastResumeToken_1);
|
|
863
|
-
});
|
|
864
|
-
}
|
|
865
|
-
return [2 /*return*/];
|
|
866
|
-
}
|
|
867
|
-
});
|
|
868
|
-
});
|
|
869
|
-
};
|
|
870
|
-
// Fetch pub once, send to all clients linked to this pub
|
|
871
|
-
SubscriptionManager.prototype.sendDataToOne = function (ws, messageId, subscription, type, collection) {
|
|
872
|
-
var _this = this;
|
|
873
|
-
return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
|
|
874
|
-
var cacheData, serverRes, monitor_1;
|
|
875
|
-
var _a;
|
|
876
|
-
var _this = this;
|
|
877
|
-
return __generator(this, function (_b) {
|
|
878
|
-
// let startDate = new Date();
|
|
879
|
-
if (!this._publications[subscription.publication].user_specific) { // Same pub for all users
|
|
880
|
-
if (subscription.cacheId) {
|
|
881
|
-
try {
|
|
882
|
-
cacheData = JSON.parse(this._nodeCache.get(subscription.cacheId), common_1.dateReviver);
|
|
883
|
-
serverRes = {
|
|
884
|
-
messageId: messageId,
|
|
885
|
-
hasError: false,
|
|
886
|
-
data: cacheData
|
|
887
|
-
};
|
|
888
|
-
this.sendWS(ws, serverRes);
|
|
889
|
-
}
|
|
890
|
-
catch (err) {
|
|
891
|
-
this._nodeCache.del(subscription.cacheId);
|
|
892
|
-
subscription.cacheId = 0;
|
|
893
|
-
this.addToQueue({
|
|
894
|
-
_id: 0,
|
|
895
|
-
type: 'newSub',
|
|
896
|
-
collection: '',
|
|
897
|
-
subscription: subscription,
|
|
898
|
-
running: false,
|
|
899
|
-
run_again: false
|
|
900
|
-
});
|
|
901
|
-
}
|
|
902
|
-
// let endDate = new Date();
|
|
903
|
-
// this.currentPerfomanceMonitor.push({
|
|
904
|
-
// _id: this.idPerformance++,
|
|
905
|
-
// function: 'sendDataToOne',
|
|
906
|
-
// publication: subscription.publication,
|
|
907
|
-
// // subscriptionData: subscription.subscriptionData,
|
|
908
|
-
// subscriptionData: [],
|
|
909
|
-
// date_start: startDate,
|
|
910
|
-
// date_end: endDate,
|
|
911
|
-
// duration: moment(endDate).diff(moment(startDate), 'milliseconds', false),
|
|
912
|
-
// result: 'Send cache'
|
|
913
|
-
// });
|
|
914
|
-
resolve(true);
|
|
915
|
-
}
|
|
916
|
-
else {
|
|
917
|
-
this._mainServer.getMethodManager().sendEmail('dev@resolveio.com', 'SERVER - Error Detected - ' + this.serverConfig['CLIENT_NAME'], 'Error Detected During sendDataToOne - No Cache! - ' + subscription.publication + '\n\nData \n' + JSON.stringify(subscription.subscriptionData, null, 2));
|
|
918
|
-
resolve(false);
|
|
919
|
-
}
|
|
920
|
-
}
|
|
921
|
-
else { // Pub is different for each user (Look at users publication) [MUCH SLOWER, RE-RUNS FOR EACH USER]
|
|
922
|
-
monitor_1 = this._monitorManagerFunction.startMonitorFunction('User Specific Publication', subscription.publication, '', '', subscription.subscriptionData);
|
|
923
|
-
(_a = this._publications[subscription.publication].function).call.apply(_a, __spreadArray([Object.assign({}, this, SubscriptionManager.prototype), ws['id_user']], subscription.subscriptionData, false)).then(function (res) {
|
|
924
|
-
_this._monitorManagerFunction.finishMonitorFunction(monitor_1);
|
|
925
|
-
var serverRes = {
|
|
926
|
-
messageId: messageId,
|
|
927
|
-
hasError: false,
|
|
928
|
-
data: res
|
|
929
|
-
};
|
|
930
|
-
// console.log('Send Pub Data Once 2', new Date(), 'Success', subscription);
|
|
931
|
-
_this.sendWS(ws, serverRes);
|
|
932
|
-
// let endDate = new Date();
|
|
933
|
-
// this.currentPerfomanceMonitor.push({
|
|
934
|
-
// _id: this.idPerformance++,
|
|
935
|
-
// function: 'sendDataToOne',
|
|
936
|
-
// publication: subscription.publication,
|
|
937
|
-
// // subscriptionData: subscription.subscriptionData,
|
|
938
|
-
// subscriptionData: [],
|
|
939
|
-
// date_start: startDate,
|
|
940
|
-
// date_end: endDate,
|
|
941
|
-
// duration: moment(endDate).diff(moment(startDate), 'milliseconds', false),
|
|
942
|
-
// result: 'Send ws spec data'
|
|
943
|
-
// });
|
|
944
|
-
resolve(true);
|
|
945
|
-
}, function (err) {
|
|
946
|
-
_this._monitorManagerFunction.finishMonitorFunction(monitor_1);
|
|
947
|
-
var serverRes = {
|
|
948
|
-
messageId: messageId,
|
|
949
|
-
hasError: true,
|
|
950
|
-
data: err
|
|
951
|
-
};
|
|
952
|
-
// console.log('Send Pub Data Once 2', new Date(), 'Error', subscription);
|
|
953
|
-
_this.sendWS(ws, serverRes);
|
|
954
|
-
_this._mainServer.getMethodManager().sendEmail('dev@resolveio.com', 'SERVER - Error Detected - ' + _this.serverConfig['CLIENT_NAME'], 'Error Detected During Subscription ' + subscription.publication + ' - (sendDataToOne - WS)\n\nData \n' + JSON.stringify(subscription.subscriptionData, null, 2) + '\n\nErrors\n' + JSON.stringify(err, null, 2));
|
|
955
|
-
reject(err);
|
|
956
|
-
});
|
|
957
|
-
}
|
|
958
|
-
return [2 /*return*/];
|
|
959
|
-
});
|
|
960
|
-
}); });
|
|
961
|
-
};
|
|
962
|
-
// Fetch pub once, send to all clients linked to this pub
|
|
963
|
-
SubscriptionManager.prototype.sendDataToAll = function (mongoQueue) {
|
|
964
|
-
var _this = this;
|
|
965
|
-
return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
|
|
966
|
-
var monitor_2;
|
|
967
|
-
var _a;
|
|
968
|
-
var _this = this;
|
|
969
|
-
return __generator(this, function (_b) {
|
|
970
|
-
// let startDate = new Date();
|
|
971
|
-
if (!mongoQueue.subscription.clients.length) {
|
|
972
|
-
if (mongoQueue.subscription.cacheId) {
|
|
973
|
-
this._nodeCache.del(mongoQueue.subscription.cacheId);
|
|
974
|
-
}
|
|
975
|
-
if (this._subscriptions.findIndex(function (a) { return a.publication === mongoQueue.subscription.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(mongoQueue.subscription.subscriptionData); }) >= 0) {
|
|
976
|
-
this._subscriptions.splice(this._subscriptions.findIndex(function (a) { return a.publication === mongoQueue.subscription.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(mongoQueue.subscription.subscriptionData); }), 1);
|
|
977
|
-
}
|
|
978
|
-
resolve(false);
|
|
979
|
-
}
|
|
980
|
-
else {
|
|
981
|
-
if (mongoQueue.subscription.publication !== 'superadminAPM' && mongoQueue.subscription.publication !== 'loggedInUsers') {
|
|
982
|
-
this._mainServer.getMethodManager().callMethodInternal.call(this._mainServer.getMethodManager(), 'insertSubscriptionLog', mongoQueue.type, mongoQueue.subscription.publication, mongoQueue.collection, JSON.stringify(mongoQueue.subscription.subscriptionData));
|
|
983
|
-
}
|
|
984
|
-
monitor_2 = this._monitorManagerFunction.startMonitorFunction('Publication', mongoQueue.subscription.publication, '', '', mongoQueue.subscription.subscriptionData);
|
|
985
|
-
(_a = this._publications[mongoQueue.subscription.publication].function).call.apply(_a, __spreadArray([Object.assign({}, this, SubscriptionManager.prototype)], mongoQueue.subscription.subscriptionData, false)).then(function (res) {
|
|
986
|
-
_this._monitorManagerFunction.finishMonitorFunction(monitor_2);
|
|
987
|
-
if (!mongoQueue.subscription.clients.length) {
|
|
988
|
-
var nodeCacheSize = _this._nodeCache.getStats().vsize;
|
|
989
|
-
if (nodeCacheSize > _this._heapLimit) {
|
|
990
|
-
var deleteCount = 0;
|
|
991
|
-
var subArr = _this._subscriptions.filter(function (a) { return a.cacheId && !a.clients.length; });
|
|
992
|
-
for (var zz = 0; zz < subArr.length; zz++) {
|
|
993
|
-
_this._debugRemoveCacheHits += 1;
|
|
994
|
-
_this._nodeCache.del(subArr[zz].cacheId);
|
|
995
|
-
subArr[zz].cacheId = 0;
|
|
996
|
-
deleteCount += 1;
|
|
997
|
-
nodeCacheSize = _this._nodeCache.getStats().vsize;
|
|
998
|
-
if (nodeCacheSize < _this._heapLimit * 0.75) {
|
|
999
|
-
break;
|
|
1000
|
-
}
|
|
1001
|
-
}
|
|
1002
|
-
console.log('Sub Cache: ' + 'Too Big - ' + mongoQueue.subscription.publication + ' - Deleted: ' + deleteCount + ' - ' + nodeCacheSize);
|
|
1003
|
-
}
|
|
1004
|
-
if (mongoQueue.subscription.cacheId) {
|
|
1005
|
-
_this._nodeCache.del(mongoQueue.subscription.cacheId);
|
|
1006
|
-
}
|
|
1007
|
-
var cacheId = _this._cacheId;
|
|
1008
|
-
_this._cacheId += 1;
|
|
1009
|
-
_this._nodeCache.set(cacheId, JSON.stringify(res));
|
|
1010
|
-
mongoQueue.subscription.cacheId = cacheId;
|
|
1011
|
-
// let endDate = new Date();
|
|
1012
|
-
// this.currentPerfomanceMonitor.push({
|
|
1013
|
-
// _id: this.idPerformance++,
|
|
1014
|
-
// function: 'sendPubData',
|
|
1015
|
-
// publication: subscription.publication,
|
|
1016
|
-
// // subscriptionData: subscription.subscriptionData,
|
|
1017
|
-
// subscriptionData: [],
|
|
1018
|
-
// date_start: startDate,
|
|
1019
|
-
// date_end: endDate,
|
|
1020
|
-
// duration: moment(endDate).diff(moment(startDate), 'milliseconds', false),
|
|
1021
|
-
// result: 'No clients'
|
|
1022
|
-
// });
|
|
1023
|
-
resolve(false);
|
|
1024
|
-
}
|
|
1025
|
-
else {
|
|
1026
|
-
if (!mongoQueue.subscription.cacheId || _this._nodeCache.get(mongoQueue.subscription.cacheId) !== JSON.stringify(res)) {
|
|
1027
|
-
mongoQueue.subscription.clients.forEach(function (client) {
|
|
1028
|
-
var ws = _this._mainServer.getWS(client.id_socket);
|
|
1029
|
-
if (ws) {
|
|
1030
|
-
var serverRes = {
|
|
1031
|
-
messageId: client.messageId,
|
|
1032
|
-
hasError: false,
|
|
1033
|
-
data: res
|
|
1034
|
-
};
|
|
1035
|
-
_this.sendWS(ws, serverRes);
|
|
1036
|
-
}
|
|
1037
|
-
});
|
|
1038
|
-
var nodeCacheSize = _this._nodeCache.getStats().vsize;
|
|
1039
|
-
if (nodeCacheSize > _this._heapLimit) {
|
|
1040
|
-
var deleteCount = 0;
|
|
1041
|
-
var subArr = _this._subscriptions.filter(function (a) { return a.cacheId && !a.clients.length; });
|
|
1042
|
-
for (var zz = 0; zz < subArr.length; zz++) {
|
|
1043
|
-
_this._nodeCache.del(subArr[zz].cacheId);
|
|
1044
|
-
subArr[zz].cacheId = 0;
|
|
1045
|
-
deleteCount += 1;
|
|
1046
|
-
nodeCacheSize = _this._nodeCache.getStats().vsize;
|
|
1047
|
-
if (nodeCacheSize < _this._heapLimit * 0.75) {
|
|
1048
|
-
break;
|
|
1049
|
-
}
|
|
1050
|
-
}
|
|
1051
|
-
console.log('Sub Cache: ' + 'Too Big - ' + mongoQueue.subscription.publication + ' - Deleted: ' + deleteCount + ' - ' + nodeCacheSize);
|
|
1052
|
-
}
|
|
1053
|
-
if (mongoQueue.subscription.cacheId) {
|
|
1054
|
-
_this._nodeCache.del(mongoQueue.subscription.cacheId);
|
|
1055
|
-
}
|
|
1056
|
-
var cacheId = _this._cacheId;
|
|
1057
|
-
_this._cacheId += 1;
|
|
1058
|
-
_this._nodeCache.set(cacheId, JSON.stringify(res));
|
|
1059
|
-
mongoQueue.subscription.cacheId = cacheId;
|
|
1060
|
-
// let endDate = new Date();
|
|
1061
|
-
// this.currentPerfomanceMonitor.push({
|
|
1062
|
-
// _id: this.idPerformance++,
|
|
1063
|
-
// function: 'sendPubData',
|
|
1064
|
-
// publication: subscription.publication,
|
|
1065
|
-
// // subscriptionData: subscription.subscriptionData,
|
|
1066
|
-
// subscriptionData: [],
|
|
1067
|
-
// date_start: startDate,
|
|
1068
|
-
// date_end: endDate,
|
|
1069
|
-
// duration: moment(endDate).diff(moment(startDate), 'milliseconds', false),
|
|
1070
|
-
// result: 'Same data'
|
|
1071
|
-
// });
|
|
1072
|
-
}
|
|
1073
|
-
resolve(true);
|
|
1074
|
-
}
|
|
1075
|
-
}, function (err) {
|
|
1076
|
-
_this._monitorManagerFunction.finishMonitorFunction(monitor_2);
|
|
1077
|
-
_this._mainServer.getMethodManager().sendEmail('dev@resolveio.com', 'SERVER - Error Detected - ' + _this.serverConfig['CLIENT_NAME'], 'Error Detected During Subscription ' + mongoQueue.subscription.publication + ' - (sendPubData)\n\nData \n' + JSON.stringify(mongoQueue.subscription.subscriptionData, null, 2) + '\n\nErrors\n' + JSON.stringify(err, null, 2));
|
|
1078
|
-
mongoQueue.subscription.clients.forEach(function (client) {
|
|
1079
|
-
var ws = _this._mainServer.getWS(client.id_socket);
|
|
1080
|
-
if (ws) {
|
|
1081
|
-
if (ws['id_socket'] === client.id_socket) {
|
|
1082
|
-
var serverRes = {
|
|
1083
|
-
messageId: client.messageId,
|
|
1084
|
-
hasError: true,
|
|
1085
|
-
data: err
|
|
1086
|
-
};
|
|
1087
|
-
_this.sendWS(ws, serverRes);
|
|
1088
|
-
}
|
|
1089
|
-
}
|
|
1090
|
-
});
|
|
1091
|
-
reject(err);
|
|
1092
|
-
});
|
|
1093
|
-
}
|
|
1094
|
-
return [2 /*return*/];
|
|
1095
|
-
});
|
|
1096
|
-
}); });
|
|
1097
|
-
};
|
|
1098
|
-
SubscriptionManager.prototype.sendWS = function (ws, data) {
|
|
1099
|
-
this._sendQueue.splice(0, 0, {
|
|
1100
|
-
id_ws: ws['id_socket'],
|
|
1101
|
-
data: data
|
|
1102
|
-
});
|
|
1103
|
-
};
|
|
1104
|
-
SubscriptionManager.prototype.getEnableDebug = function () {
|
|
1105
|
-
return this._enableDebug;
|
|
1106
|
-
};
|
|
1107
|
-
return SubscriptionManager;
|
|
1108
|
-
}());
|
|
1109
|
-
exports.SubscriptionManager = SubscriptionManager;
|
|
1110
|
-
|
|
1
|
+
"use strict";var __awaiter=this&&this.__awaiter||function(e,r,u,c){return new(u=u||Promise)(function(t,i){function n(e){try{s(c.next(e))}catch(e){i(e)}}function o(e){try{s(c.throw(e))}catch(e){i(e)}}function s(e){var i;e.done?t(e.value):((i=e.value)instanceof u?i:new u(function(e){e(i)})).then(n,o)}s((c=c.apply(e,r||[])).next())})},__generator=this&&this.__generator||function(n,o){var s,r,u,c={label:0,sent:function(){if(1&u[0])throw u[1];return u[1]},trys:[],ops:[]},a={next:e(0),throw:e(1),return:e(2)};return"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a;function e(t){return function(e){var i=[t,e];if(s)throw new TypeError("Generator is already executing.");for(;c=a&&i[a=0]?0:c;)try{if(s=1,r&&(u=2&i[0]?r.return:i[0]?r.throw||((u=r.return)&&u.call(r),0):r.next)&&!(u=u.call(r,i[1])).done)return u;switch(r=0,(i=u?[2&i[0],u.value]:i)[0]){case 0:case 1:u=i;break;case 4:return c.label++,{value:i[1],done:!1};case 5:c.label++,r=i[1],i=[0];continue;case 7:i=c.ops.pop(),c.trys.pop();continue;default:if(!(u=0<(u=c.trys).length&&u[u.length-1])&&(6===i[0]||2===i[0])){c=0;continue}if(3===i[0]&&(!u||i[1]>u[0]&&i[1]<u[3]))c.label=i[1];else if(6===i[0]&&c.label<u[1])c.label=u[1],u=i;else{if(!(u&&c.label<u[2])){u[2]&&c.ops.pop(),c.trys.pop();continue}c.label=u[2],c.ops.push(i)}}i=o.call(n,c)}catch(e){i=[6,e],r=0}finally{s=u=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}}},__spreadArray=this&&this.__spreadArray||function(e,i,t){if(t||2===arguments.length)for(var n,o=0,s=i.length;o<s;o++)!n&&o in i||((n=n||Array.prototype.slice.call(i,0,o))[o]=i[o]);return e.concat(n||Array.prototype.slice.call(i))},logs_1=(Object.defineProperty(exports,"__esModule",{value:!0}),exports.SubscriptionManager=void 0,require("../publications/logs")),app_status_1=require("../publications/app-status"),files_1=require("../publications/files"),super_admin_1=require("../publications/super-admin"),logged_in_users_collection_1=require("../collections/logged-in-users.collection"),cron_jobs_1=require("../publications/cron-jobs"),flags_1=require("../publications/flags"),method_responses_1=require("../publications/method-responses"),index_1=require("../index"),notifications_1=require("../publications/notifications"),report_builder_reports_1=require("../publications/report-builder-reports"),report_builder_libraries_1=require("../publications/report-builder-libraries"),user_groups_1=require("../publications/user-groups"),user_guides_1=require("../publications/user-guides"),report_builder_dashboard_builders_1=require("../publications/report-builder-dashboard-builders"),mongo_manager_1=require("./mongo.manager"),common_1=require("../util/common"),NodeCache=require("node-cache"),flag_collection_1=require("../collections/flag.collection"),os_1=require("os"),numCPUs=(0,os_1.cpus)().length,v8=require("v8"),SubscriptionManager=function(){function g(e,i,t,n){var o=this;this._publications={},this._subscriptions=[],this._loggedInUsers=[],this._mongoQueue=[],this._mongoQueueId=0,this._sendQueue=[],this._runningQueue=!1,this._cacheId=1,this._heapStats=v8.getHeapStatistics(),this._heapLimit=this._heapStats.heap_size_limit/numCPUs/8,this._enableDebug=!1,this._debugOplogCollections=[],this._debugOplogHits=0,this._debugSubCollections=[],this._debugSubHits=0,this._debugUnSubHits=0,this._debugUnSubAllHits=0,this._debugMongoQueueHits=0,this._debugMongoQueueCollections=[],this._debugSendQueueHits=0,this._debugRemoveCacheHits=0,this._oplogRetryCount=0,this._mainServer=e,this._monitorManagerFunction=n,this._nodeCache=new NodeCache({stdTTL:0,checkperiod:0}),this.serverConfig=t,this._wss=i,(0,super_admin_1.loadSuperAdminPublications)(this),(0,app_status_1.loadAppStatusPublications)(this),(0,logs_1.loadLogPublications)(this),(0,files_1.loadFilePublications)(this),(0,cron_jobs_1.loadCronJobPublications)(this),(0,flags_1.loadFlagsPublications)(this),(0,method_responses_1.loadMethodResponsePublications)(this),(0,notifications_1.loadNotificationPublications)(this),(0,report_builder_reports_1.loadReportBuilderReportPublications)(this),(0,report_builder_libraries_1.loadReportBuilderLibraryPublications)(this),(0,user_groups_1.loadUserGroupPublications)(this),(0,user_guides_1.loadUserGuidePublications)(this),(0,report_builder_dashboard_builders_1.loadReportBuilderDashboardBuilderPublications)(this),this.tailOpLog(),setInterval(function(){o._oplogRetryCount=0},15e3),setInterval(function(){o.getEnableDebug()&&(console.log(new Date,"Sub Manager","Subs",o._subscriptions.length),console.log(new Date,"Sub Manager","Logged In Users",o._loggedInUsers.length),console.log(new Date,"Sub Manager","Mongo Queue",o._mongoQueue.length),console.log(new Date,"Sub Manager","Mongo Queue Hits",o._debugMongoQueueHits),console.log(new Date,"Sub Manager","Mongo Queue Collections",JSON.stringify(o._debugMongoQueueCollections.sort(function(e,i){return e.collection.localeCompare(i.collection)||e.publication.localeCompare(i.publication)}),null,2)),console.log(new Date,"Sub Manager","Oplog Hits",o._debugOplogHits),console.log(new Date,"Sub Manager","Oplog Collections",JSON.stringify(o._debugOplogCollections.sort(function(e,i){return e.collection.localeCompare(i.collection)||e.type.localeCompare(i.type)}),null,2)),console.log(new Date,"Sub Manager","Send Queue Hits",o._debugSendQueueHits),console.log(new Date,"Sub Manager","Sub Hits",o._debugSubHits),console.log(new Date,"Sub Manager","Sub Collections",JSON.stringify(o._debugSubCollections.sort(function(e,i){return e.publication.localeCompare(i.publication)}),null,2)),console.log(new Date,"Sub Manager","Unsub Hits",o._debugUnSubHits),console.log(new Date,"Sub Manager","Unsub All Hits",o._debugUnSubAllHits),console.log(new Date,"Sub Manager","Cache Cleanup Hits",o._debugRemoveCacheHits)),o._debugOplogHits=0,o._debugOplogCollections=[],o._debugSubCollections=[],o._debugMongoQueueHits=0,o._debugMongoQueueCollections=[],o._debugSendQueueHits=0,o._debugSubHits=0,o._debugUnSubHits=0,o._debugUnSubAllHits=0,o._debugRemoveCacheHits=0},6e4),setInterval(function(){return __awaiter(o,void 0,void 0,function(){var i,n,o,t,s,r,u=this;return __generator(this,function(e){if(!this._runningQueue){if(this._runningQueue=!0,this._sendQueue.length)for(i=function(e){n._debugSendQueueHits+=1;var i,t=n._mainServer.getWS(n._sendQueue[e].id_ws);t&&t.readyState===t.OPEN?t.bufferedAmount<20480&&(i=n._sendQueue.pop(),t.send(JSON.stringify(i.data),function(e){e&&(u.getEnableDebug()&&console.log(new Date,"Sub Manager","Unsub WS",t.user,t.id_socket,1),u.unsubscribeAll(t))})):n._sendQueue.splice(e,1)},r=(n=this)._sendQueue.length-1;0<=r;r--)i(r);if((o=this._mongoQueue.filter(function(e){return!e.running})).length)for(o.forEach(function(e){e.running=!0}),t=function(e){s._debugMongoQueueHits+=1;var t=o[e];if(s._debugMongoQueueCollections.some(function(e){return e.collection===t.collection&&e.publication===t.subscription.publication})?s._debugMongoQueueCollections.filter(function(e){return e.collection===t.collection&&e.publication===t.subscription.publication})[0].hits+=1:s._debugMongoQueueCollections.push({collection:t.collection,publication:t.subscription.publication,hits:1}),s._publications[t.subscription.publication].user_specific){for(var n=[],i=0;i<t.subscription.clients.length;i++)!function(e){var i=t.subscription.clients[e],e=s._mainServer.getWS(i.id_socket);e&&e.id_socket===i.id_socket&&n.push(s.sendDataToOne(e,i.messageId,t.subscription,t.type,t.collection).catch(function(e){return u._mainServer.getMethodManager().sendEmail("dev@resolveio.com","SERVER - Error Detected - "+u.serverConfig.CLIENT_NAME,"Error Detected During sendDataToOne - User Specific - Socket: "+i.id_socket+", User: "+i.id_user+", MessageId: "+i.messageId+", Pub: "+t.subscription.publication+", Err: "+JSON.stringify(e,null,2)),null}))}(i);Promise.all(n).then(function(){t.run_again?(t.running=!1,t.run_again=!1):0<=u._mongoQueue.map(function(e){return e._id}).indexOf(t._id)&&u._mongoQueue.splice(u._mongoQueue.map(function(e){return e._id}).indexOf(t._id),1)})}else s.sendDataToAll(t).then(function(){t.run_again?(t.running=!1,t.run_again=!1):0<=u._mongoQueue.map(function(e){return e._id}).indexOf(t._id)&&u._mongoQueue.splice(u._mongoQueue.map(function(e){return e._id}).indexOf(t._id),1)},function(){0<=u._mongoQueue.map(function(e){return e._id}).indexOf(t._id)&&u._mongoQueue.splice(u._mongoQueue.map(function(e){return e._id}).indexOf(t._id),1)})},s=this,r=o.length-1;0<=r;r--)t(r);this._runningQueue=!1}return[2]})})},25),setInterval(function(){return __awaiter(o,void 0,void 0,function(){var i,n,t,o,s,r,u,c,a;return __generator(this,function(e){switch(e.label){case 0:return i=this,[4,logged_in_users_collection_1.LoggedInUsers.find()];case 1:for(i._loggedInUsers=e.sent(),n=(0,common_1.deepCopy)(this._loggedInUsers),t=function(e){var t=n[e];(!t.date||12e4<Date.now()-t.date.getTime())&&(o._mainServer.getWS(t.id_ws)?(o.getEnableDebug()&&console.log(new Date,"Sub Manager","Unsub WS",o._mainServer.getWS(t.id_ws).user,o._mainServer.getWS(t.id_ws).id_socket,2),o.unsubscribeAll(o._mainServer.getWS(t.id_ws))):(o._subscriptions.forEach(function(e){for(var i=e.clients.length-1;0<=i;i--)e.clients[i].id_socket===t.id_ws&&e.clients.splice(i,1)}),logged_in_users_collection_1.LoggedInUsers.deleteOne({_id:t._id}),0<=o._loggedInUsers.findIndex(function(e){return e._id===t._id})&&o._loggedInUsers.splice(o._loggedInUsers.findIndex(function(e){return e._id===t._id}),1)))},s=(o=this)._loggedInUsers.length-1;0<=s;s--)t(s);for(s=0;s<this._subscriptions.length;s++)for(r=this._subscriptions[s],u=function(e){var i=r.clients[e];c._loggedInUsers.some(function(e){return e.id_ws===i.id_socket})||r.clients.splice(e,1)},c=this,a=r.clients.length-1;0<=a;a--)u(a);return[2]}})})},3e4),flag_collection_1.Flags.findOne({type:"Enable Debug"}).then(function(e){e&&e.value?o._enableDebug=!0:o._enableDebug=!1})}return g.prototype.addToQueue=function(i){var e=this._mongoQueue.filter(function(e){return e.subscription.publication===i.subscription.publication&&JSON.stringify(e.subscription.subscriptionData)===JSON.stringify(i.subscription.subscriptionData)})[0];e?e.running&&(e.run_again=!0):(i._id=this._mongoQueueId,this._mongoQueueId+=1,this._mongoQueue.splice(0,0,i))},g.prototype.invalidatePubsCache=function(i,e){index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(i);for(var t=this._subscriptions.filter(function(e){return e.collections.includes(i)}),n=t.length-1;0<=n;n--){var o=t[n];this.addToQueue({_id:0,type:e,collection:i,subscription:o,running:!1,run_again:!1})}},g.prototype.publications=function(e){this._publications=Object.assign(this._publications,e)},g.prototype.loggedInLatency=function(i){var t=this,e=this._loggedInUsers.filter(function(e){return e.id_ws===i.id_socket})[0];e&&(e.date=new Date,logged_in_users_collection_1.LoggedInUsers.updateOne({id_ws:i.id_socket},{$set:{latency:i.latency,date:e.date}}).then(function(e){e||(t.unsubscribeAll(i),t.getEnableDebug()&&console.log(new Date,"Sub Manager","Unsub WS",i.user,i.id_socket,3))},function(){t.unsubscribeAll(i),t.getEnableDebug()&&console.log(new Date,"Sub Manager","Unsub WS",i.user,i.id_socket,4)}))},g.prototype.subscribe=function(i,e,t,n,o,s){var r=this,u=(this._debugSubHits+=1,this._debugSubCollections.some(function(e){return e.publication===o})?this._debugSubCollections.filter(function(e){return e.publication===o})[0].hits+=1:this._debugSubCollections.push({publication:o,hits:1}),this._publications[o]);if(u){if(1<s.length||s[0]){if(!u.check)return void console.error(new Date,"No Check Function For Pub "+o);if(!u.check._schema)return void console.error(new Date,"No Check Schema For Pub "+o);for(var c={},a=Object.keys(u.check._schema).filter(function(e){return!e.includes(".")}),l=0;l<s.length;l++)c[a[l]]=s[l];try{u.check.validate(c)}catch(e){if(e)return void console.error(new Date,"Error in Pub Check ("+o+")",e)}}"Bypass"!==i&&(u=i.split("/"),g="",_=u[0],""===u[0]&&(g="/",_=u[1]),g+=_,1<u.length&&(g+="/"),(_=this._subscriptions.filter(function(e){return e.clients.some(function(e){return e.id_socket===t.id_socket&&"Bypass"!==e.messageRoute&&"/"!==e.messageRoute&&e.messageRoute!==i&&!e.messageRoute.startsWith(g)})})).length)&&(this._mainServer.getMethodManager().sendEmail("dev@resolveio.com","SERVER - Detected Undestroyed Subscription - "+this.serverConfig.CLIENT_NAME,"USER: "+t.user+" (Socket: "+t.id_socket+") is on route: "+i+" but has the following subscriptions on other routes:"+JSON.stringify(_,null,2)),_.forEach(function(i){i.clients.filter(function(e){return e.id_socket===t.id_socket}).forEach(function(e){r.unsubscribe(e.messageRoute,new Date,t,e.messageId,i.publication,i.subscriptionData)})}));var g,_,d=this._subscriptions.filter(function(e){return e.publication===o&&JSON.stringify(e.subscriptionData)===JSON.stringify(s)})[0];d?d.clients.some(function(e){return e.id_socket===t.id_socket&&e.messageId===n})||d.clients.push({id_user:t.id_user,messageId:n,id_socket:t.id_socket,messageRoute:i}):this._subscriptions.push({publication:o,subscriptionData:s,collections:this.getPublicationCollections(o),clients:[{id_user:t.id_user,messageId:n,id_socket:t.id_socket,messageRoute:i}],cacheId:0}),(d=d||this._subscriptions.filter(function(e){return e.publication===o&&JSON.stringify(e.subscriptionData)===JSON.stringify(s)})[0]).cacheId||this._mongoQueue.some(function(e){return e.subscription.publication===d.publication&&JSON.stringify(e.subscription.subscriptionData)===JSON.stringify(d.subscriptionData)})?d.cacheId&&this.sendDataToOne(t,n,d,"newSub",o):this.addToQueue({_id:0,type:"newSub",collection:"",subscription:d,running:!1,run_again:!1})}else console.error(new Date,"No Publication: "+o)},g.prototype.createLoggedInUser=function(n){return __awaiter(this,void 0,void 0,function(){var i=this;return __generator(this,function(e){return[2,new Promise(function(t,e){return __awaiter(i,void 0,void 0,function(){var i;return __generator(this,function(e){return(i=this._mainServer.getWS(n))?(i={_id:(0,mongo_manager_1.objectIdHexString)(),__v:0,date:new Date,id_user:i.id_user,user:i.user,id_ws:i.id_socket},this._loggedInUsers.push(i),logged_in_users_collection_1.LoggedInUsers.insertOne(i),t(i)):t(null),[2]})})})]})})},g.prototype.unsubscribe=function(e,i,t,n,o,s){if(this._debugUnSubHits+=1,this._publications[o]){var r=this._subscriptions.filter(function(e){return e.publication===o&&JSON.stringify(e.subscriptionData)===JSON.stringify(s)})[0];if(r)for(var u=r.clients.length-1;0<=u;u--)r.clients[u].id_user===t.id_user&&r.clients[u].messageId===n&&r.clients[u].id_socket===t.id_socket&&r.clients.splice(u,1)}else console.log("No Publication: "+o)},g.prototype.unsubscribeAll=function(r){return __awaiter(this,void 0,void 0,function(){var i,t,n,o,s;return __generator(this,function(e){switch(e.label){case 0:return(this._debugUnSubAllHits+=1,r)?[4,logged_in_users_collection_1.LoggedInUsers.findOne({id_ws:r.id_socket})]:[3,2];case 1:for((i=e.sent())&&(0<=this._loggedInUsers.map(function(e){return e._id}).indexOf(i._id)&&this._loggedInUsers.splice(this._loggedInUsers.map(function(e){return e._id}).indexOf(i._id),1),logged_in_users_collection_1.LoggedInUsers.deleteOne({_id:i._id})),t=this._subscriptions.filter(function(e){return e.clients.some(function(e){return e.id_user===r.id_user&&e.id_socket===r.id_socket})}),n=t.length-1;0<=n;n--)for(o=t[n],s=o.clients.length-1;0<=s;s--)o.clients[s].id_socket===r.id_socket&&o.clients.splice(s,1);r.close(),process.nextTick(function(){[r.OPEN,r.CLOSING].includes(r.readyState)&&r.terminate()}),e.label=2;case 2:return[2]}})})},g.prototype.getActiveSubscriptions=function(){return this._subscriptions},g.prototype.getPublicationCollections=function(e){return this._publications[e].collections},g.prototype.tailOpLog=function(i){return __awaiter(this,void 0,void 0,function(){var t,n=this;return __generator(this,function(e){switch(e.label){case 0:return this._oplog$&&!this._oplog$.closed&&(this._oplog$.removeAllListeners(),this._oplog$.close(),this._oplog$=null),[4,new Promise(function(e){return setTimeout(e,1e3)})];case 1:if(e.sent(),!this._oplog$||this._oplog$.closed){if(this._oplogRetryCount+=1,5<this._oplogRetryCount&&(console.error("****************** TAIL OPLOG ERROR, RETRYING A BUNCH OF TIMES, KILLING PROCESS **************************"),process.exit(1)),i){t=i;try{this._oplog$=index_1.ResolveIOServer.getMainDB().watch([],{resumeAfter:i})}catch(e){return this._oplog$&&(this._oplog$.removeAllListeners(),this._oplog$.close(),this._oplog$=null),this.tailOpLog(i),[2]}}else this._oplog$=index_1.ResolveIOServer.getMainDB().watch();console.log(new Date,"oplog started"),this._oplog$.on("change",function(i){var e;i.ns&&(e=i.ns.coll,n._debugOplogCollections.some(function(e){return e.collection===i.ns.coll&&e.type===i.operationType})?n._debugOplogCollections.filter(function(e){return e.collection===i.ns.coll&&e.type===i.operationType})[0].hits+=1:n._debugOplogCollections.push({collection:i.ns.coll,type:i.operationType,hits:1}),!e||e.endsWith(".versions")||e.startsWith("monitor-")||"logs"===e||"log-method-latencies"===e||"log-subscriptions"===e||(n._debugOplogHits+=1,"insert"===i.operationType?("support-tickets"===e&&"https://resolveio.com"===n.serverConfig.ROOT_URL&&(n._mainServer.getMethodManager().callMethodInternal.call(n._mainServer.getMethodManager(),"sendSupportTicketEmail",i.documentKey._id),n.invalidatePubsCache(e,"insert")),"method-responses"!==e&&n.invalidatePubsCache(e,"insert")):"update"===i.operationType||"replace"===i.operationType?"method-responses"!==e&&n.invalidatePubsCache(e,"update"):"delete"===i.operationType&&"method-responses"!==e&&n.invalidatePubsCache(e,"delete")),"flags"===e&&flag_collection_1.Flags.findOne({type:"Enable Debug"}).then(function(e){e&&e.value?n._enableDebug=!0:n._enableDebug=!1}),t=i._id)}),this._oplog$.on("error",function(e){console.log(new Date,"oplog error",e),n._oplog$.removeAllListeners(),n._oplog$.close(),n._oplog$=null,n.tailOpLog(t)}),this._oplog$.on("end",function(){console.log(new Date,"oplog end"),n._oplog$.removeAllListeners(),n._oplog$.close(),n._oplog$=null,n.tailOpLog(t)}),this._oplog$.on("close",function(){console.log(new Date,"oplog close"),n._oplog$.removeAllListeners(),n._oplog$=null,n.tailOpLog(t)})}return[2]}})})},g.prototype.sendDataToOne=function(c,a,l,e,i){var t=this;return new Promise(function(r,u){return __awaiter(t,void 0,void 0,function(){var i,t,n,o,s=this;return __generator(this,function(e){if(this._publications[l.publication].user_specific)n=this._monitorManagerFunction.startMonitorFunction("User Specific Publication",l.publication,"","",l.subscriptionData),(o=this._publications[l.publication].function).call.apply(o,__spreadArray([Object.assign({},this,g.prototype),c.id_user],l.subscriptionData,!1)).then(function(e){s._monitorManagerFunction.finishMonitorFunction(n),s.sendWS(c,{messageId:a,hasError:!1,data:e}),r(!0)},function(e){s._monitorManagerFunction.finishMonitorFunction(n),s.sendWS(c,{messageId:a,hasError:!0,data:e}),s._mainServer.getMethodManager().sendEmail("dev@resolveio.com","SERVER - Error Detected - "+s.serverConfig.CLIENT_NAME,"Error Detected During Subscription "+l.publication+" - (sendDataToOne - WS)\n\nData \n"+JSON.stringify(l.subscriptionData,null,2)+"\n\nErrors\n"+JSON.stringify(e,null,2)),u(e)});else if(l.cacheId){try{i=JSON.parse(this._nodeCache.get(l.cacheId),common_1.dateReviver),t={messageId:a,hasError:!1,data:i},this.sendWS(c,t)}catch(e){this._nodeCache.del(l.cacheId),l.cacheId=0,this.addToQueue({_id:0,type:"newSub",collection:"",subscription:l,running:!1,run_again:!1})}r(!0)}else this._mainServer.getMethodManager().sendEmail("dev@resolveio.com","SERVER - Error Detected - "+this.serverConfig.CLIENT_NAME,"Error Detected During sendDataToOne - No Cache! - "+l.publication+"\n\nData \n"+JSON.stringify(l.subscriptionData,null,2)),r(!1);return[2]})})})},g.prototype.sendDataToAll=function(a){var e=this;return new Promise(function(c,n){return __awaiter(e,void 0,void 0,function(){var r,i,u=this;return __generator(this,function(e){return a.subscription.clients.length?("superadminAPM"!==a.subscription.publication&&"loggedInUsers"!==a.subscription.publication&&this._mainServer.getMethodManager().callMethodInternal.call(this._mainServer.getMethodManager(),"insertSubscriptionLog",a.type,a.subscription.publication,a.collection,JSON.stringify(a.subscription.subscriptionData)),r=this._monitorManagerFunction.startMonitorFunction("Publication",a.subscription.publication,"","",a.subscription.subscriptionData),(i=this._publications[a.subscription.publication].function).call.apply(i,__spreadArray([Object.assign({},this,g.prototype)],a.subscription.subscriptionData,!1)).then(function(t){if(u._monitorManagerFunction.finishMonitorFunction(r),a.subscription.clients.length){if(!a.subscription.cacheId||u._nodeCache.get(a.subscription.cacheId)!==JSON.stringify(t)){if(a.subscription.clients.forEach(function(e){var i=u._mainServer.getWS(e.id_socket);i&&(e={messageId:e.messageId,hasError:!1,data:t},u.sendWS(i,e))}),(e=u._nodeCache.getStats().vsize)>u._heapLimit){for(i=0,n=u._subscriptions.filter(function(e){return e.cacheId&&!e.clients.length}),o=0;o<n.length&&(u._nodeCache.del(n[o].cacheId),n[o].cacheId=0,i+=1,!((e=u._nodeCache.getStats().vsize)<.75*u._heapLimit));o++);console.log("Sub Cache: Too Big - "+a.subscription.publication+" - Deleted: "+i+" - "+e)}a.subscription.cacheId&&u._nodeCache.del(a.subscription.cacheId);s=u._cacheId;u._cacheId+=1,u._nodeCache.set(s,JSON.stringify(t)),a.subscription.cacheId=s}c(!0)}else{var e;if((e=u._nodeCache.getStats().vsize)>u._heapLimit){for(var i=0,n=u._subscriptions.filter(function(e){return e.cacheId&&!e.clients.length}),o=0;o<n.length&&(u._debugRemoveCacheHits+=1,u._nodeCache.del(n[o].cacheId),n[o].cacheId=0,i+=1,!((e=u._nodeCache.getStats().vsize)<.75*u._heapLimit));o++);console.log("Sub Cache: Too Big - "+a.subscription.publication+" - Deleted: "+i+" - "+e)}a.subscription.cacheId&&u._nodeCache.del(a.subscription.cacheId);var s=u._cacheId;u._cacheId+=1,u._nodeCache.set(s,JSON.stringify(t)),a.subscription.cacheId=s,c(!1)}},function(t){u._monitorManagerFunction.finishMonitorFunction(r),u._mainServer.getMethodManager().sendEmail("dev@resolveio.com","SERVER - Error Detected - "+u.serverConfig.CLIENT_NAME,"Error Detected During Subscription "+a.subscription.publication+" - (sendPubData)\n\nData \n"+JSON.stringify(a.subscription.subscriptionData,null,2)+"\n\nErrors\n"+JSON.stringify(t,null,2)),a.subscription.clients.forEach(function(e){var i=u._mainServer.getWS(e.id_socket);i&&i.id_socket===e.id_socket&&(e={messageId:e.messageId,hasError:!0,data:t},u.sendWS(i,e))}),n(t)})):(a.subscription.cacheId&&this._nodeCache.del(a.subscription.cacheId),0<=this._subscriptions.findIndex(function(e){return e.publication===a.subscription.publication&&JSON.stringify(e.subscriptionData)===JSON.stringify(a.subscription.subscriptionData)})&&this._subscriptions.splice(this._subscriptions.findIndex(function(e){return e.publication===a.subscription.publication&&JSON.stringify(e.subscriptionData)===JSON.stringify(a.subscription.subscriptionData)}),1),c(!1)),[2]})})})},g.prototype.sendWS=function(e,i){this._sendQueue.splice(0,0,{id_ws:e.id_socket,data:i})},g.prototype.getEnableDebug=function(){return this._enableDebug},g}();exports.SubscriptionManager=SubscriptionManager;
|
|
1111
2
|
//# sourceMappingURL=subscription.manager.js.map
|