badmfck-api-server 2.2.9 → 2.3.1
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.
|
@@ -24,6 +24,7 @@ export interface MysqlServiceOptions {
|
|
|
24
24
|
queueLimit?: number;
|
|
25
25
|
transactionFailReport?: (trx: ITransaction, message: string) => void;
|
|
26
26
|
transactionFailReportDir?: string;
|
|
27
|
+
debug?: boolean;
|
|
27
28
|
migrations?: {
|
|
28
29
|
dir: string;
|
|
29
30
|
callback: () => void;
|
|
@@ -82,6 +83,7 @@ export declare class MysqlService extends BaseService {
|
|
|
82
83
|
options: MysqlServiceOptions;
|
|
83
84
|
serviceStarted: boolean;
|
|
84
85
|
timeoutID: any;
|
|
86
|
+
debug: boolean;
|
|
85
87
|
queries: never[];
|
|
86
88
|
static nextTransactionID: number;
|
|
87
89
|
transactions: ITransaction[];
|
|
@@ -50,6 +50,7 @@ class MysqlService extends BaseService_1.BaseService {
|
|
|
50
50
|
options;
|
|
51
51
|
serviceStarted = false;
|
|
52
52
|
timeoutID;
|
|
53
|
+
debug = false;
|
|
53
54
|
queries = [];
|
|
54
55
|
static nextTransactionID = 0;
|
|
55
56
|
transactions = [];
|
|
@@ -68,6 +69,8 @@ class MysqlService extends BaseService_1.BaseService {
|
|
|
68
69
|
this.options.database = decrypt(this.options.database.substring(1)) ?? this.options.database;
|
|
69
70
|
if (this.options.password.startsWith("_"))
|
|
70
71
|
this.options.password = decrypt(this.options.password.substring(1)) ?? this.options.password;
|
|
72
|
+
if (this.options.debug)
|
|
73
|
+
this.debug = true;
|
|
71
74
|
setInterval(() => {
|
|
72
75
|
const now = Date.now();
|
|
73
76
|
this.transactions = this.transactions.filter(async (i) => {
|
|
@@ -109,12 +112,10 @@ class MysqlService extends BaseService_1.BaseService {
|
|
|
109
112
|
async init() {
|
|
110
113
|
super.init();
|
|
111
114
|
process.on('SIGINT', async () => {
|
|
112
|
-
console.log('1. Received SIGINT. Performing cleanup...');
|
|
113
115
|
await this.finishApp();
|
|
114
116
|
process.exit(0);
|
|
115
117
|
});
|
|
116
118
|
process.on('SIGTERM', async () => {
|
|
117
|
-
console.log('2. Received SIGTERM. Performing cleanup...');
|
|
118
119
|
await this.finishApp();
|
|
119
120
|
process.exit(0);
|
|
120
121
|
});
|
|
@@ -136,6 +137,8 @@ class MysqlService extends BaseService_1.BaseService {
|
|
|
136
137
|
const query = MysqlService.prepareQuery(i.query, i.fields);
|
|
137
138
|
promises.push(this.execute(query, i.rollbackQuery ?? null, i.transactionID ?? 0));
|
|
138
139
|
}
|
|
140
|
+
if (this.debug)
|
|
141
|
+
console.log("Execute queries: ", data);
|
|
139
142
|
return await Promise.all(promises);
|
|
140
143
|
};
|
|
141
144
|
exports.REQ_MYSQL_TBEGIN.listener = async () => {
|
|
@@ -143,12 +146,18 @@ class MysqlService extends BaseService_1.BaseService {
|
|
|
143
146
|
if (!conn)
|
|
144
147
|
return { code: "NO_POOL", errno: 100000, fatal: true, sql: "", name: "NO_POOL", message: "Mysql pool not created" };
|
|
145
148
|
const tid = MysqlService.nextTransactionID++;
|
|
149
|
+
if (this.debug)
|
|
150
|
+
console.log("Begin transaction with id ", tid);
|
|
146
151
|
try {
|
|
147
152
|
await conn.beginTransaction();
|
|
148
|
-
await conn.query("SET autocommit=0");
|
|
153
|
+
const res = await conn.query("SET autocommit=0");
|
|
154
|
+
if (this.debug)
|
|
155
|
+
console.log("Transaction started, SET autocommit=0 ", tid, res);
|
|
149
156
|
}
|
|
150
157
|
catch (e) {
|
|
151
158
|
try {
|
|
159
|
+
if (this.debug)
|
|
160
|
+
console.log("Transaction failed to start ", tid, e);
|
|
152
161
|
conn.removeAllListeners();
|
|
153
162
|
await conn.rollback();
|
|
154
163
|
conn.release();
|
|
@@ -162,6 +171,8 @@ class MysqlService extends BaseService_1.BaseService {
|
|
|
162
171
|
conn: conn,
|
|
163
172
|
queries: []
|
|
164
173
|
});
|
|
174
|
+
if (this.debug)
|
|
175
|
+
console.log("Transactions pool: ", this.transactions);
|
|
165
176
|
return tid;
|
|
166
177
|
};
|
|
167
178
|
exports.REQ_MYSQL_TQUERY.listener = async (data) => {
|
|
@@ -179,6 +190,8 @@ class MysqlService extends BaseService_1.BaseService {
|
|
|
179
190
|
let err = null;
|
|
180
191
|
let rollbackError = null;
|
|
181
192
|
let sqlData = null;
|
|
193
|
+
if (this.debug)
|
|
194
|
+
console.log("Execute query on transaction: ", trx);
|
|
182
195
|
try {
|
|
183
196
|
sqlData = await trx.conn.query(query);
|
|
184
197
|
}
|
|
@@ -197,6 +210,8 @@ class MysqlService extends BaseService_1.BaseService {
|
|
|
197
210
|
rollbackError = this.createMysqlQueryError(e);
|
|
198
211
|
}
|
|
199
212
|
}
|
|
213
|
+
if (this.debug)
|
|
214
|
+
console.log("Query execution status:", err ? err.message : "completed");
|
|
200
215
|
trx.queries.push({ sql: query, status: err ? err.message : "completed" });
|
|
201
216
|
return {
|
|
202
217
|
data: sqlData,
|
|
@@ -216,12 +231,17 @@ class MysqlService extends BaseService_1.BaseService {
|
|
|
216
231
|
message: "Transaction not found"
|
|
217
232
|
};
|
|
218
233
|
}
|
|
234
|
+
if (this.debug)
|
|
235
|
+
console.log("Rollback transaction:", trx);
|
|
219
236
|
this.transactions = this.transactions.filter(i => i.id !== tid);
|
|
237
|
+
if (this.debug)
|
|
238
|
+
console.log("Transaction pool:", this.transactions);
|
|
220
239
|
try {
|
|
221
240
|
await trx.conn.rollback();
|
|
222
241
|
trx.conn.removeAllListeners();
|
|
223
242
|
trx.conn.release();
|
|
224
|
-
|
|
243
|
+
if (this.debug)
|
|
244
|
+
console.log("Rollback complete");
|
|
225
245
|
}
|
|
226
246
|
catch (e) {
|
|
227
247
|
(0, LogService_1.logError)("Can't rollback transaction");
|
|
@@ -231,7 +251,7 @@ class MysqlService extends BaseService_1.BaseService {
|
|
|
231
251
|
};
|
|
232
252
|
exports.REQ_MYSQL_TCOMMIT.listener = async (tid) => {
|
|
233
253
|
const trx = this.transactions.find(i => i.id === tid);
|
|
234
|
-
if (!trx)
|
|
254
|
+
if (!trx) {
|
|
235
255
|
return {
|
|
236
256
|
code: "NO_TRX",
|
|
237
257
|
errno: 100004,
|
|
@@ -240,15 +260,24 @@ class MysqlService extends BaseService_1.BaseService {
|
|
|
240
260
|
name: "NO_TRX",
|
|
241
261
|
message: "Transaction not found"
|
|
242
262
|
};
|
|
263
|
+
}
|
|
264
|
+
if (this.debug)
|
|
265
|
+
console.log("Commit transaction:", trx);
|
|
243
266
|
this.transactions = this.transactions.filter(i => i.id !== tid);
|
|
267
|
+
if (this.debug)
|
|
268
|
+
console.log("Transaction pool:", this.transactions);
|
|
244
269
|
try {
|
|
245
270
|
await trx.conn.commit();
|
|
246
271
|
trx.conn.removeAllListeners();
|
|
247
272
|
trx.conn.release();
|
|
273
|
+
if (this.debug)
|
|
274
|
+
console.log("Commit completed:");
|
|
248
275
|
}
|
|
249
276
|
catch (e) {
|
|
250
277
|
this.storeTransactionAsProblem(trx, "Can't commit transaction");
|
|
251
278
|
try {
|
|
279
|
+
if (this.debug)
|
|
280
|
+
console.log("Commit error, try rollback");
|
|
252
281
|
trx.conn.removeAllListeners();
|
|
253
282
|
await trx.conn.rollback();
|
|
254
283
|
trx.conn.release();
|
|
@@ -264,6 +293,7 @@ class MysqlService extends BaseService_1.BaseService {
|
|
|
264
293
|
if (!this.pool)
|
|
265
294
|
return { error: { code: "NO_POOL", errno: 100000, fatal: true, sql: "", name: "NO_POOL", message: "Mysql pool not created" }, data: null };
|
|
266
295
|
const conn = await this.pool.getConnection();
|
|
296
|
+
(0, LogService_1.logCrit)("${MysqlService.js}", "REQ_MYSQL_TRANSACTION deprecated, use REQ_MYSQL_TBEGIN, REQ_MYSQL_TQUERY, REQ_MYSQL_TCOMMIT, REQ_MYSQL_TROLLBACK");
|
|
267
297
|
if (!conn)
|
|
268
298
|
return { error: { code: "NO_CONN", errno: 100001, fatal: true, sql: "", name: "NO_CONN", message: "Mysql pool cant get connection" }, data: null };
|
|
269
299
|
const trx = {
|
|
@@ -321,6 +351,7 @@ class MysqlService extends BaseService_1.BaseService {
|
|
|
321
351
|
};
|
|
322
352
|
}
|
|
323
353
|
async finishApp() {
|
|
354
|
+
console.log("Finishing mysql service");
|
|
324
355
|
for (let i of this.transactions) {
|
|
325
356
|
try {
|
|
326
357
|
this.storeTransactionAsProblem(i, "SIGINT");
|
|
@@ -364,9 +395,13 @@ class MysqlService extends BaseService_1.BaseService {
|
|
|
364
395
|
this.failReportFileStream = fs_1.default.createWriteStream(file, { flags: 'a' });
|
|
365
396
|
this.failReportFileStreamName = file;
|
|
366
397
|
}
|
|
398
|
+
if (this.debug)
|
|
399
|
+
console.log("Store transaction fail report: ", file, trx, message);
|
|
367
400
|
this.failReportFileStream?.write(JSON.stringify({ trx, date, message }) + "\n{'s':'&$5__1AzZa'}\n", err => {
|
|
368
401
|
if (err)
|
|
369
402
|
(0, LogService_1.logCrit)("${MysqlService.js}", "Can't write to transaction fail report file");
|
|
403
|
+
else
|
|
404
|
+
(0, LogService_1.logInfo)("${MysqlService.js}", "Transaction fail report stored");
|
|
370
405
|
});
|
|
371
406
|
this.failReportLastAccessTime = Date.now();
|
|
372
407
|
}
|
|
@@ -413,11 +448,14 @@ class MysqlService extends BaseService_1.BaseService {
|
|
|
413
448
|
this.pool?.on('enqueue', () => {
|
|
414
449
|
(0, LogService_1.logInfo)("${MysqlService.js}", "MYSQL CONNECTION ENQUEUED");
|
|
415
450
|
});
|
|
451
|
+
if (this.debug)
|
|
452
|
+
console.log("Check mysql connection");
|
|
416
453
|
return new Promise(async (resolve, reject) => {
|
|
417
454
|
try {
|
|
418
455
|
const conn = await this.pool?.getConnection();
|
|
419
456
|
if (conn) {
|
|
420
457
|
conn?.release();
|
|
458
|
+
(0, LogService_1.logInfo)("${MysqlService.js}", "Connected to MYSQL!");
|
|
421
459
|
resolve(true);
|
|
422
460
|
}
|
|
423
461
|
else
|
|
@@ -569,10 +607,14 @@ class MysqlService extends BaseService_1.BaseService {
|
|
|
569
607
|
let conn = null;
|
|
570
608
|
if (transactionID && transactionID > 0) {
|
|
571
609
|
conn = this.transactions.find(i => i.id === transactionID)?.conn ?? null;
|
|
610
|
+
if (this.debug)
|
|
611
|
+
console.log("Execute query on transaction: ", conn);
|
|
572
612
|
}
|
|
573
613
|
else {
|
|
574
614
|
conn = await this.pool.getConnection();
|
|
575
615
|
}
|
|
616
|
+
if (this.debug)
|
|
617
|
+
console.log("Execute query", conn);
|
|
576
618
|
if (!conn) {
|
|
577
619
|
(0, LogService_1.logCrit)("${MysqlService.js}", `No connection created!`);
|
|
578
620
|
resolve({
|