badmfck-api-server 2.3.3 → 2.3.5

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.
@@ -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.3";
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>;
@@ -80,10 +80,7 @@ class MysqlService extends BaseService_1.BaseService {
80
80
  for (let i of this.transactions) {
81
81
  if (now - i.timestamp > this.maxTransactionWaitTime) {
82
82
  (0, LogService_1.logError)("Release transaction connection due to timeout");
83
- i.conn.removeAllListeners();
84
- i.conn.rollback();
85
- i.conn.release();
86
- this.storeTransactionAsProblem(i, "Timeout");
83
+ this.rollbackTransaction(i, true);
87
84
  removedIDs.push(i.id);
88
85
  }
89
86
  }
@@ -164,14 +161,7 @@ class MysqlService extends BaseService_1.BaseService {
164
161
  console.log("Transaction started, SET autocommit=0 ", tid, res);
165
162
  }
166
163
  catch (e) {
167
- try {
168
- if (this.debug)
169
- console.log("Transaction failed to start ", tid, e);
170
- conn.removeAllListeners();
171
- await conn.rollback();
172
- conn.release();
173
- }
174
- catch (e) { }
164
+ this.rollbackTransaction({ id: tid, conn: conn, timestamp: Date.now(), queries: [{ sql: "SET autocommit=0", status: "error" }] });
175
165
  return this.createMysqlQueryError(e);
176
166
  }
177
167
  this.transactions.push({
@@ -197,7 +187,6 @@ class MysqlService extends BaseService_1.BaseService {
197
187
  }, data: null, rollbackError: null };
198
188
  const query = MysqlService.prepareQuery(data.query.query, data.query.fields);
199
189
  let err = null;
200
- let rollbackError = null;
201
190
  let sqlData = null;
202
191
  if (this.debug)
203
192
  console.log("Execute query on transaction: ", trx);
@@ -207,25 +196,14 @@ class MysqlService extends BaseService_1.BaseService {
207
196
  catch (e) {
208
197
  err = this.createMysqlQueryError(e);
209
198
  }
210
- if (err) {
211
- try {
212
- trx.conn.removeAllListeners();
213
- await trx.conn.rollback();
214
- trx.conn.release();
215
- this.transactions = this.transactions.filter(i => i.id !== data.tid);
216
- this.storeTransactionAsProblem(trx, err.message);
217
- }
218
- catch (e) {
219
- rollbackError = this.createMysqlQueryError(e);
220
- }
221
- }
199
+ if (err)
200
+ this.rollbackTransaction(trx);
222
201
  if (this.debug)
223
202
  console.log("Query execution status:", err ? err.message : "completed");
224
203
  trx.queries.push({ sql: query, status: err ? err.message : "completed" });
225
204
  return {
226
205
  data: sqlData,
227
206
  error: err,
228
- rollbackError: rollbackError
229
207
  };
230
208
  };
231
209
  exports.REQ_MYSQL_TROLLBACK.listener = async (tid) => {
@@ -245,17 +223,7 @@ class MysqlService extends BaseService_1.BaseService {
245
223
  this.transactions = this.transactions.filter(i => i.id !== tid);
246
224
  if (this.debug)
247
225
  console.log("Transaction pool:", this.transactions);
248
- try {
249
- await trx.conn.rollback();
250
- trx.conn.removeAllListeners();
251
- trx.conn.release();
252
- if (this.debug)
253
- console.log("Rollback complete");
254
- }
255
- catch (e) {
256
- (0, LogService_1.logError)("Can't rollback transaction");
257
- return this.createMysqlQueryError(e);
258
- }
226
+ this.rollbackTransaction(trx);
259
227
  return null;
260
228
  };
261
229
  exports.REQ_MYSQL_TCOMMIT.listener = async (tid) => {
@@ -283,17 +251,7 @@ class MysqlService extends BaseService_1.BaseService {
283
251
  console.log("Commit completed:");
284
252
  }
285
253
  catch (e) {
286
- this.storeTransactionAsProblem(trx, "Can't commit transaction");
287
- try {
288
- if (this.debug)
289
- console.log("Commit error, try rollback");
290
- trx.conn.removeAllListeners();
291
- await trx.conn.rollback();
292
- trx.conn.release();
293
- }
294
- catch (e) {
295
- (0, LogService_1.logError)("Can't rollback transaction");
296
- }
254
+ this.rollbackTransaction(trx);
297
255
  return this.createMysqlQueryError(e);
298
256
  }
299
257
  return null;
@@ -344,35 +302,33 @@ class MysqlService extends BaseService_1.BaseService {
344
302
  await conn.commit();
345
303
  }
346
304
  catch (e) {
347
- let rollbackError = null;
348
- try {
349
- await conn.rollback();
350
- }
351
- catch (e2) {
352
- (0, LogService_1.logCrit)("${MysqlService.js}", "Can't rollback transaction!");
353
- rollbackError = this.createMysqlQueryError(e2);
354
- }
305
+ await this.rollbackTransaction(trx);
355
306
  const err = this.createMysqlQueryError(e);
356
- this.storeTransactionAsProblem(trx, err.message);
357
- return { error: err, data: null, rollbackError: rollbackError };
307
+ return { error: err, data: null };
358
308
  }
359
309
  return { error: null, data: income };
360
310
  };
361
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
+ }
362
328
  async finishApp() {
363
329
  console.log("Finishing mysql service");
364
- for (let i of this.transactions) {
365
- try {
366
- this.storeTransactionAsProblem(i, "SIGINT");
367
- i.conn.removeAllListeners();
368
- await i.conn.rollback();
369
- i.conn.release();
370
- console.log("Transaction rollbacked: ", i.id, i.queries);
371
- }
372
- catch (e) {
373
- (0, LogService_1.logError)("Can't release transaction connection", e);
374
- }
375
- }
330
+ for (let i of this.transactions)
331
+ this.rollbackTransaction(i);
376
332
  if (this.pool) {
377
333
  try {
378
334
  await this.pool.end();
@@ -408,7 +364,7 @@ class MysqlService extends BaseService_1.BaseService {
408
364
  console.log("Store transaction fail report: ", file, trx, message);
409
365
  if (!this.failReportFileStream)
410
366
  this.failReportFileStream = fs_1.default.createWriteStream(file, { flags: 'a' });
411
- this.failReportFileStream.write(JSON.stringify({ trx, date, message }) + "\n{'s':'&$5__1AzZa'}\n", err => {
367
+ this.failReportFileStream.write(JSON.stringify({ queries: trx.queries, time: trx.timestamp, date, message }) + "\n{'s':'&$5__1AzZa'}\n", err => {
412
368
  if (err)
413
369
  (0, LogService_1.logCrit)("${MysqlService.js}", "Can't write to transaction fail report file");
414
370
  else
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "badmfck-api-server",
3
- "version": "2.3.3",
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",