badmfck-api-server 2.3.1 → 2.3.3
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.
@@ -25,6 +25,7 @@ export interface MysqlServiceOptions {
|
|
25
25
|
transactionFailReport?: (trx: ITransaction, message: string) => void;
|
26
26
|
transactionFailReportDir?: string;
|
27
27
|
debug?: boolean;
|
28
|
+
naxTransactionWaitTime?: number;
|
28
29
|
migrations?: {
|
29
30
|
dir: string;
|
30
31
|
callback: () => void;
|
@@ -52,9 +52,9 @@ class MysqlService extends BaseService_1.BaseService {
|
|
52
52
|
timeoutID;
|
53
53
|
debug = false;
|
54
54
|
queries = [];
|
55
|
-
static nextTransactionID =
|
55
|
+
static nextTransactionID = 1;
|
56
56
|
transactions = [];
|
57
|
-
maxTransactionWaitTime = 1000 * 60 *
|
57
|
+
maxTransactionWaitTime = 1000 * 60 * 1;
|
58
58
|
failReportFileStream = null;
|
59
59
|
failReportFileStreamName = null;
|
60
60
|
failReportLastAccessTime = 0;
|
@@ -71,22 +71,25 @@ class MysqlService extends BaseService_1.BaseService {
|
|
71
71
|
this.options.password = decrypt(this.options.password.substring(1)) ?? this.options.password;
|
72
72
|
if (this.options.debug)
|
73
73
|
this.debug = true;
|
74
|
+
if (this.options.naxTransactionWaitTime)
|
75
|
+
this.maxTransactionWaitTime = this.options.naxTransactionWaitTime;
|
74
76
|
setInterval(() => {
|
75
77
|
const now = Date.now();
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
78
|
+
const count = this.transactions.length;
|
79
|
+
const removedIDs = [];
|
80
|
+
for (let i of this.transactions) {
|
81
|
+
if (now - i.timestamp > this.maxTransactionWaitTime) {
|
80
82
|
(0, LogService_1.logError)("Release transaction connection due to timeout");
|
81
83
|
i.conn.removeAllListeners();
|
82
|
-
|
84
|
+
i.conn.rollback();
|
83
85
|
i.conn.release();
|
86
|
+
this.storeTransactionAsProblem(i, "Timeout");
|
87
|
+
removedIDs.push(i.id);
|
84
88
|
}
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
});
|
89
|
+
}
|
90
|
+
this.transactions = this.transactions.filter(i => !removedIDs.includes(i.id));
|
91
|
+
if (this.debug)
|
92
|
+
console.log("Removed: ", count - this.transactions.length);
|
90
93
|
}, 1000 * 30);
|
91
94
|
setInterval(() => {
|
92
95
|
if (!this.failReportFileStream)
|
@@ -145,6 +148,12 @@ class MysqlService extends BaseService_1.BaseService {
|
|
145
148
|
const conn = await this.pool?.getConnection();
|
146
149
|
if (!conn)
|
147
150
|
return { code: "NO_POOL", errno: 100000, fatal: true, sql: "", name: "NO_POOL", message: "Mysql pool not created" };
|
151
|
+
for (let i of this.transactions) {
|
152
|
+
if (i.conn === conn) {
|
153
|
+
(0, LogService_1.logInfo)("Your connection is in transaction");
|
154
|
+
return { code: "CONN_IN_TRX", errno: 100005, fatal: true, sql: "", name: "CONN_IN_TRX", message: "Connection is in transaction" };
|
155
|
+
}
|
156
|
+
}
|
148
157
|
const tid = MysqlService.nextTransactionID++;
|
149
158
|
if (this.debug)
|
150
159
|
console.log("Begin transaction with id ", tid);
|
@@ -397,7 +406,9 @@ class MysqlService extends BaseService_1.BaseService {
|
|
397
406
|
}
|
398
407
|
if (this.debug)
|
399
408
|
console.log("Store transaction fail report: ", file, trx, message);
|
400
|
-
this.failReportFileStream
|
409
|
+
if (!this.failReportFileStream)
|
410
|
+
this.failReportFileStream = fs_1.default.createWriteStream(file, { flags: 'a' });
|
411
|
+
this.failReportFileStream.write(JSON.stringify({ trx, date, message }) + "\n{'s':'&$5__1AzZa'}\n", err => {
|
401
412
|
if (err)
|
402
413
|
(0, LogService_1.logCrit)("${MysqlService.js}", "Can't write to transaction fail report file");
|
403
414
|
else
|