badmfck-api-server 2.3.2 → 2.3.5

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.2";
86
+ version = "2.3.5";
87
87
  options;
88
88
  monitor;
89
89
  monitorIndexFile;
@@ -95,6 +95,7 @@ export declare class MysqlService extends BaseService {
95
95
  constructor(options: MysqlServiceOptions);
96
96
  static executeQuery(query: MySqlQuery | MySqlQuery[]): Promise<MysqlResult[]>;
97
97
  init(): Promise<void>;
98
+ rollbackTransaction(trx: ITransaction, donNotRemove?: boolean): Promise<void>;
98
99
  finishApp(): Promise<void>;
99
100
  storeTransactionAsProblem(trx: ITransaction, message: string): Promise<void>;
100
101
  recreatePool(): Promise<boolean>;
@@ -76,22 +76,15 @@ class MysqlService extends BaseService_1.BaseService {
76
76
  setInterval(() => {
77
77
  const now = Date.now();
78
78
  const count = this.transactions.length;
79
- this.transactions = this.transactions.filter(async (i) => {
80
- if (now - i.timestamp < this.maxTransactionWaitTime)
81
- return true;
82
- try {
79
+ const removedIDs = [];
80
+ for (let i of this.transactions) {
81
+ if (now - i.timestamp > this.maxTransactionWaitTime) {
83
82
  (0, LogService_1.logError)("Release transaction connection due to timeout");
84
- i.conn.removeAllListeners();
85
- await i.conn.rollback();
86
- i.conn.release();
87
- this.storeTransactionAsProblem(i, "Timeout");
88
- }
89
- catch (e) {
90
- (0, LogService_1.logError)("Can't release transaction", e);
91
- this.storeTransactionAsProblem(i, "Timeout, cant,release");
83
+ this.rollbackTransaction(i, true);
84
+ removedIDs.push(i.id);
92
85
  }
93
- return false;
94
- });
86
+ }
87
+ this.transactions = this.transactions.filter(i => !removedIDs.includes(i.id));
95
88
  if (this.debug)
96
89
  console.log("Removed: ", count - this.transactions.length);
97
90
  }, 1000 * 30);
@@ -168,14 +161,7 @@ class MysqlService extends BaseService_1.BaseService {
168
161
  console.log("Transaction started, SET autocommit=0 ", tid, res);
169
162
  }
170
163
  catch (e) {
171
- try {
172
- if (this.debug)
173
- console.log("Transaction failed to start ", tid, e);
174
- conn.removeAllListeners();
175
- await conn.rollback();
176
- conn.release();
177
- }
178
- catch (e) { }
164
+ this.rollbackTransaction({ id: tid, conn: conn, timestamp: Date.now(), queries: [{ sql: "SET autocommit=0", status: "error" }] });
179
165
  return this.createMysqlQueryError(e);
180
166
  }
181
167
  this.transactions.push({
@@ -201,7 +187,6 @@ class MysqlService extends BaseService_1.BaseService {
201
187
  }, data: null, rollbackError: null };
202
188
  const query = MysqlService.prepareQuery(data.query.query, data.query.fields);
203
189
  let err = null;
204
- let rollbackError = null;
205
190
  let sqlData = null;
206
191
  if (this.debug)
207
192
  console.log("Execute query on transaction: ", trx);
@@ -211,25 +196,14 @@ class MysqlService extends BaseService_1.BaseService {
211
196
  catch (e) {
212
197
  err = this.createMysqlQueryError(e);
213
198
  }
214
- if (err) {
215
- try {
216
- trx.conn.removeAllListeners();
217
- await trx.conn.rollback();
218
- trx.conn.release();
219
- this.transactions = this.transactions.filter(i => i.id !== data.tid);
220
- this.storeTransactionAsProblem(trx, err.message);
221
- }
222
- catch (e) {
223
- rollbackError = this.createMysqlQueryError(e);
224
- }
225
- }
199
+ if (err)
200
+ this.rollbackTransaction(trx);
226
201
  if (this.debug)
227
202
  console.log("Query execution status:", err ? err.message : "completed");
228
203
  trx.queries.push({ sql: query, status: err ? err.message : "completed" });
229
204
  return {
230
205
  data: sqlData,
231
206
  error: err,
232
- rollbackError: rollbackError
233
207
  };
234
208
  };
235
209
  exports.REQ_MYSQL_TROLLBACK.listener = async (tid) => {
@@ -249,17 +223,7 @@ class MysqlService extends BaseService_1.BaseService {
249
223
  this.transactions = this.transactions.filter(i => i.id !== tid);
250
224
  if (this.debug)
251
225
  console.log("Transaction pool:", this.transactions);
252
- try {
253
- await trx.conn.rollback();
254
- trx.conn.removeAllListeners();
255
- trx.conn.release();
256
- if (this.debug)
257
- console.log("Rollback complete");
258
- }
259
- catch (e) {
260
- (0, LogService_1.logError)("Can't rollback transaction");
261
- return this.createMysqlQueryError(e);
262
- }
226
+ this.rollbackTransaction(trx);
263
227
  return null;
264
228
  };
265
229
  exports.REQ_MYSQL_TCOMMIT.listener = async (tid) => {
@@ -287,17 +251,7 @@ class MysqlService extends BaseService_1.BaseService {
287
251
  console.log("Commit completed:");
288
252
  }
289
253
  catch (e) {
290
- this.storeTransactionAsProblem(trx, "Can't commit transaction");
291
- try {
292
- if (this.debug)
293
- console.log("Commit error, try rollback");
294
- trx.conn.removeAllListeners();
295
- await trx.conn.rollback();
296
- trx.conn.release();
297
- }
298
- catch (e) {
299
- (0, LogService_1.logError)("Can't rollback transaction");
300
- }
254
+ this.rollbackTransaction(trx);
301
255
  return this.createMysqlQueryError(e);
302
256
  }
303
257
  return null;
@@ -348,35 +302,33 @@ class MysqlService extends BaseService_1.BaseService {
348
302
  await conn.commit();
349
303
  }
350
304
  catch (e) {
351
- let rollbackError = null;
352
- try {
353
- await conn.rollback();
354
- }
355
- catch (e2) {
356
- (0, LogService_1.logCrit)("${MysqlService.js}", "Can't rollback transaction!");
357
- rollbackError = this.createMysqlQueryError(e2);
358
- }
305
+ await this.rollbackTransaction(trx);
359
306
  const err = this.createMysqlQueryError(e);
360
- this.storeTransactionAsProblem(trx, err.message);
361
- return { error: err, data: null, rollbackError: rollbackError };
307
+ return { error: err, data: null };
362
308
  }
363
309
  return { error: null, data: income };
364
310
  };
365
311
  }
312
+ async rollbackTransaction(trx, donNotRemove) {
313
+ try {
314
+ console.log("Rollback started", trx.conn);
315
+ await trx.conn.rollback();
316
+ trx.conn.removeAllListeners();
317
+ trx.conn.release();
318
+ if (!donNotRemove)
319
+ this.transactions = this.transactions.filter(i => i.id !== trx.id);
320
+ if (this.debug)
321
+ console.log("Rollback complete", trx.conn);
322
+ }
323
+ catch (e) {
324
+ (0, LogService_1.logCrit)("${MysqlService.js}", "Can't rollback transaction", trx);
325
+ }
326
+ this.storeTransactionAsProblem(trx, "rollback");
327
+ }
366
328
  async finishApp() {
367
329
  console.log("Finishing mysql service");
368
- for (let i of this.transactions) {
369
- try {
370
- this.storeTransactionAsProblem(i, "SIGINT");
371
- i.conn.removeAllListeners();
372
- await i.conn.rollback();
373
- i.conn.release();
374
- console.log("Transaction rollbacked: ", i.id, i.queries);
375
- }
376
- catch (e) {
377
- (0, LogService_1.logError)("Can't release transaction connection", e);
378
- }
379
- }
330
+ for (let i of this.transactions)
331
+ this.rollbackTransaction(i);
380
332
  if (this.pool) {
381
333
  try {
382
334
  await this.pool.end();
@@ -410,7 +362,9 @@ class MysqlService extends BaseService_1.BaseService {
410
362
  }
411
363
  if (this.debug)
412
364
  console.log("Store transaction fail report: ", file, trx, message);
413
- this.failReportFileStream?.write(JSON.stringify({ trx, date, message }) + "\n{'s':'&$5__1AzZa'}\n", err => {
365
+ if (!this.failReportFileStream)
366
+ this.failReportFileStream = fs_1.default.createWriteStream(file, { flags: 'a' });
367
+ this.failReportFileStream.write(JSON.stringify({ queries: trx.queries, time: trx.timestamp, date, message }) + "\n{'s':'&$5__1AzZa'}\n", err => {
414
368
  if (err)
415
369
  (0, LogService_1.logCrit)("${MysqlService.js}", "Can't write to transaction fail report file");
416
370
  else
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "badmfck-api-server",
3
- "version": "2.3.2",
3
+ "version": "2.3.5",
4
4
  "description": "Simple API http server based on express",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",