badmfck-api-server 2.3.3 → 2.3.6

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.6";
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) => {
@@ -240,22 +218,7 @@ class MysqlService extends BaseService_1.BaseService {
240
218
  message: "Transaction not found"
241
219
  };
242
220
  }
243
- if (this.debug)
244
- console.log("Rollback transaction:", trx);
245
- this.transactions = this.transactions.filter(i => i.id !== tid);
246
- if (this.debug)
247
- 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
- }
221
+ this.rollbackTransaction(trx);
259
222
  return null;
260
223
  };
261
224
  exports.REQ_MYSQL_TCOMMIT.listener = async (tid) => {
@@ -283,17 +246,7 @@ class MysqlService extends BaseService_1.BaseService {
283
246
  console.log("Commit completed:");
284
247
  }
285
248
  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
- }
249
+ this.rollbackTransaction(trx);
297
250
  return this.createMysqlQueryError(e);
298
251
  }
299
252
  return null;
@@ -344,35 +297,33 @@ class MysqlService extends BaseService_1.BaseService {
344
297
  await conn.commit();
345
298
  }
346
299
  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
- }
300
+ await this.rollbackTransaction(trx);
355
301
  const err = this.createMysqlQueryError(e);
356
- this.storeTransactionAsProblem(trx, err.message);
357
- return { error: err, data: null, rollbackError: rollbackError };
302
+ return { error: err, data: null };
358
303
  }
359
304
  return { error: null, data: income };
360
305
  };
361
306
  }
307
+ async rollbackTransaction(trx, donNotRemove) {
308
+ try {
309
+ console.log("Rollback started", trx.conn);
310
+ await trx.conn.rollback();
311
+ trx.conn.removeAllListeners();
312
+ trx.conn.release();
313
+ if (!donNotRemove)
314
+ this.transactions = this.transactions.filter(i => i.id !== trx.id);
315
+ if (this.debug)
316
+ console.log("Rollback complete", trx.conn);
317
+ }
318
+ catch (e) {
319
+ (0, LogService_1.logCrit)("${MysqlService.js}", "Can't rollback transaction", trx);
320
+ }
321
+ this.storeTransactionAsProblem(trx, "rollback");
322
+ }
362
323
  async finishApp() {
363
324
  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
- }
325
+ for (let i of this.transactions)
326
+ this.rollbackTransaction(i);
376
327
  if (this.pool) {
377
328
  try {
378
329
  await this.pool.end();
@@ -408,7 +359,7 @@ class MysqlService extends BaseService_1.BaseService {
408
359
  console.log("Store transaction fail report: ", file, trx, message);
409
360
  if (!this.failReportFileStream)
410
361
  this.failReportFileStream = fs_1.default.createWriteStream(file, { flags: 'a' });
411
- this.failReportFileStream.write(JSON.stringify({ trx, date, message }) + "\n{'s':'&$5__1AzZa'}\n", err => {
362
+ this.failReportFileStream.write(JSON.stringify({ queries: trx.queries, time: trx.timestamp, date, message }) + "\n}EOB{\n", err => {
412
363
  if (err)
413
364
  (0, LogService_1.logCrit)("${MysqlService.js}", "Can't write to transaction fail report file");
414
365
  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.6",
4
4
  "description": "Simple API http server based on express",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",