badmfck-api-server 2.2.0 → 2.2.1

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.2.0";
86
+ version = "2.2.1";
87
87
  options;
88
88
  monitor;
89
89
  monitorIndexFile;
@@ -20,6 +20,7 @@ export interface MysqlServiceOptions {
20
20
  database: string;
21
21
  queueLimit?: number;
22
22
  transactionFailReport?: (trx: ITransaction, message: string) => void;
23
+ transactionFailReportDir?: string;
23
24
  migrations?: {
24
25
  dir: string;
25
26
  callback: () => void;
@@ -84,6 +85,7 @@ export declare class MysqlService extends BaseService {
84
85
  constructor(options: MysqlServiceOptions);
85
86
  static executeQuery(query: MySqlQuery | MySqlQuery[]): Promise<MysqlResult[]>;
86
87
  init(): Promise<void>;
88
+ finishApp(): Promise<void>;
87
89
  storeTransactionAsProblem(trx: ITransaction, message: string): Promise<void>;
88
90
  recreatePool(): Promise<boolean>;
89
91
  onApplicationReady(): Promise<void>;
@@ -76,35 +76,30 @@ class MysqlService extends BaseService_1.BaseService {
76
76
  return false;
77
77
  });
78
78
  }, 1000 * 30);
79
+ if (options.transactionFailReportDir) {
80
+ const fs = require('fs');
81
+ try {
82
+ if (!fs.existsSync(options.transactionFailReportDir)) {
83
+ fs.mkdirSync(options.transactionFailReportDir);
84
+ }
85
+ }
86
+ catch (e) {
87
+ options.transactionFailReportDir = undefined;
88
+ (0, LogService_1.logCrit)("${MysqlService.js}", "Can't create transaction fail report dir");
89
+ }
90
+ }
79
91
  }
80
92
  static async executeQuery(query) { return await exports.REQ_MYSQL_QUERY.request(query); }
81
93
  async init() {
82
94
  super.init();
83
95
  process.on('SIGINT', async () => {
84
96
  console.log('1. Received SIGINT. Performing cleanup...');
85
- for (let i of this.transactions) {
86
- try {
87
- this.storeTransactionAsProblem(i, "SIGINT");
88
- i.conn.removeAllListeners();
89
- await i.conn.rollback();
90
- i.conn.release();
91
- }
92
- catch (e) {
93
- (0, LogService_1.logError)("Can't release transaction connection", e);
94
- }
95
- }
96
- if (this.pool) {
97
- try {
98
- await this.pool.end();
99
- }
100
- catch (e) {
101
- (0, LogService_1.logCrit)("${MysqlService.js}", "Can't close MYSQL pool!");
102
- }
103
- }
97
+ await this.finishApp();
104
98
  process.exit(0);
105
99
  });
106
- process.on('SIGTERM', () => {
100
+ process.on('SIGTERM', async () => {
107
101
  console.log('2. Received SIGTERM. Performing cleanup...');
102
+ await this.finishApp();
108
103
  process.exit(0);
109
104
  });
110
105
  this.serviceStarted = false;
@@ -220,38 +215,74 @@ class MysqlService extends BaseService_1.BaseService {
220
215
  const conn = await this.pool.getConnection();
221
216
  if (!conn)
222
217
  return { error: { code: "NO_CONN", errno: 100001, fatal: true, sql: "", name: "NO_CONN", message: "Mysql pool cant get connection" }, data: null };
223
- let result = null;
218
+ const trx = {
219
+ id: MysqlService.nextTransactionID++,
220
+ timestamp: Date.now(),
221
+ conn: conn,
222
+ queries: []
223
+ };
224
224
  let income = [];
225
225
  try {
226
226
  await conn.beginTransaction();
227
227
  await conn.query("SET autocommit=0");
228
228
  for (let i of data) {
229
- const d = await conn.query(MysqlService.prepareQuery(i.query, i.fields));
230
- income.push(d[0]);
229
+ const query = MysqlService.prepareQuery(i.query, i.fields);
230
+ let err = null;
231
+ try {
232
+ const d = await conn.query(query);
233
+ income.push(d[0]);
234
+ }
235
+ catch (e) {
236
+ err = this.createMysqlQueryError(e);
237
+ }
238
+ trx.queries.push({
239
+ sql: query,
240
+ status: err ? err.message : "completed"
241
+ });
242
+ if (err)
243
+ return { error: err, data: null };
231
244
  }
232
- await conn.commit();
233
- }
234
- catch (e) {
235
- let rollbackError = null;
236
245
  try {
237
- await conn.rollback();
246
+ await conn.commit();
238
247
  }
239
- catch (e2) {
240
- (0, LogService_1.logCrit)("${MysqlService.js}", "Can't rollback transaction!");
241
- rollbackError = this.createMysqlQueryError(e2);
248
+ catch (e) {
249
+ let rollbackError = null;
250
+ try {
251
+ await conn.rollback();
252
+ }
253
+ catch (e2) {
254
+ (0, LogService_1.logCrit)("${MysqlService.js}", "Can't rollback transaction!");
255
+ rollbackError = this.createMysqlQueryError(e2);
256
+ }
257
+ return { error: this.createMysqlQueryError(e), data: null, rollbackError: rollbackError };
242
258
  }
243
- result = { error: this.createMysqlQueryError(e), data: null, rollbackError: rollbackError };
244
259
  }
260
+ catch (e) {
261
+ return { error: this.createMysqlQueryError(e), data: null };
262
+ }
263
+ return { error: null, data: income };
264
+ };
265
+ }
266
+ async finishApp() {
267
+ for (let i of this.transactions) {
268
+ try {
269
+ this.storeTransactionAsProblem(i, "SIGINT");
270
+ i.conn.removeAllListeners();
271
+ await i.conn.rollback();
272
+ i.conn.release();
273
+ }
274
+ catch (e) {
275
+ (0, LogService_1.logError)("Can't release transaction connection", e);
276
+ }
277
+ }
278
+ if (this.pool) {
245
279
  try {
246
- conn.release();
280
+ await this.pool.end();
247
281
  }
248
282
  catch (e) {
249
- (0, LogService_1.logCrit)("${MysqlService.js}", "Can't release connection!");
283
+ (0, LogService_1.logCrit)("${MysqlService.js}", "Can't close MYSQL pool!");
250
284
  }
251
- if (!result)
252
- result = { error: null, data: income };
253
- return result;
254
- };
285
+ }
255
286
  }
256
287
  async storeTransactionAsProblem(trx, message) {
257
288
  if (!this.options.transactionFailReport) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "badmfck-api-server",
3
- "version": "2.2.0",
3
+ "version": "2.2.1",
4
4
  "description": "Simple API http server based on express",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",