badmfck-api-server 2.3.1 → 2.3.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -83,7 +83,7 @@ async function Initializer(services) {
83
83
  exports.Initializer = Initializer;
84
84
  class APIService extends BaseService_1.BaseService {
85
85
  static nextLogID = 0;
86
- version = "2.3.1";
86
+ version = "2.3.3";
87
87
  options;
88
88
  monitor;
89
89
  monitorIndexFile;
@@ -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 = 0;
55
+ static nextTransactionID = 1;
56
56
  transactions = [];
57
- maxTransactionWaitTime = 1000 * 60 * 2;
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
- this.transactions = this.transactions.filter(async (i) => {
77
- if (now - i.timestamp < this.maxTransactionWaitTime)
78
- return true;
79
- try {
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
- await i.conn.rollback();
84
+ i.conn.rollback();
83
85
  i.conn.release();
86
+ this.storeTransactionAsProblem(i, "Timeout");
87
+ removedIDs.push(i.id);
84
88
  }
85
- catch (e) {
86
- (0, LogService_1.logError)("Can't release transaction", e);
87
- }
88
- return false;
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?.write(JSON.stringify({ trx, date, message }) + "\n{'s':'&$5__1AzZa'}\n", err => {
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
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "badmfck-api-server",
3
- "version": "2.3.1",
3
+ "version": "2.3.3",
4
4
  "description": "Simple API http server based on express",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",