cgserver 13.2.2 → 13.2.4
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/README.md
CHANGED
|
@@ -23,11 +23,15 @@ class MongoBaseService {
|
|
|
23
23
|
if (this._model) {
|
|
24
24
|
return this._model;
|
|
25
25
|
}
|
|
26
|
-
if (
|
|
26
|
+
if (!this.mongo) {
|
|
27
|
+
Log_1.gLog.error("MongoDB connection is not established, please check the connection settings.");
|
|
28
|
+
throw new Error("MongoDB connection is not established");
|
|
29
|
+
}
|
|
30
|
+
if (!this.mongo.connected) {
|
|
27
31
|
Log_1.gLog.error("MongoDB connection is not ready, please check the connection settings. Current state: " + mongoose_1.default.connection.readyState);
|
|
28
32
|
throw new Error("MongoDB connection is not ready");
|
|
29
33
|
}
|
|
30
|
-
let db =
|
|
34
|
+
let db = this.mongo.connection.db;
|
|
31
35
|
if (!db || db.databaseName == "test") {
|
|
32
36
|
Log_1.gLog.error("MongoDB connection is not valid, please check the connection settings.");
|
|
33
37
|
throw new Error("MongoDB connection is not valid");
|
|
@@ -122,6 +122,9 @@ class MongoExt {
|
|
|
122
122
|
get connection() {
|
|
123
123
|
return this._connection;
|
|
124
124
|
}
|
|
125
|
+
get connected() {
|
|
126
|
+
return this._connection && this._connection.readyState === 1;
|
|
127
|
+
}
|
|
125
128
|
constructor() {
|
|
126
129
|
}
|
|
127
130
|
async init(cfg) {
|
|
@@ -137,11 +140,18 @@ class MongoExt {
|
|
|
137
140
|
this._mongocfg.options.dbName = this._mongocfg.database;
|
|
138
141
|
this._connection = mongoose_2.default.createConnection("mongodb://" + this._mongocfg.host + ":" + this._mongocfg.port, this._mongocfg.options);
|
|
139
142
|
this._connection = this._connection.useDb(this._mongocfg.database);
|
|
140
|
-
this._connection.
|
|
143
|
+
this._connection.once("open", this.onOpen.bind(this));
|
|
141
144
|
this._connection.on("close", this.onClose.bind(this));
|
|
142
145
|
this._connection.on("connectionCreated", this.onConnect.bind(this));
|
|
143
146
|
this._connection.on("connectionClosed", this.onDisconnect.bind(this));
|
|
144
147
|
await this._connection.asPromise();
|
|
148
|
+
while (this._connection.readyState != mongoose_1.ConnectionStates.connected) {
|
|
149
|
+
if (this._connection.readyState != mongoose_1.ConnectionStates.connecting) {
|
|
150
|
+
Log_1.gLog.error("MongoDB connection is not ready, please check the connection settings. Current state: " + this._connection.readyState);
|
|
151
|
+
return false;
|
|
152
|
+
}
|
|
153
|
+
await Core_1.core.sleep(100);
|
|
154
|
+
}
|
|
145
155
|
console.log("mongo connect success! db=" + this._connection.name);
|
|
146
156
|
return true;
|
|
147
157
|
}
|
|
@@ -152,7 +162,10 @@ class MongoExt {
|
|
|
152
162
|
this._cur_connecting_count--;
|
|
153
163
|
}
|
|
154
164
|
close(force = false) {
|
|
155
|
-
|
|
165
|
+
if (!this._connection) {
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
this._connection.close(force);
|
|
156
169
|
}
|
|
157
170
|
registerInitCb(cb) {
|
|
158
171
|
this._init_cbs.push(cb);
|