badmfck-api-server 1.2.1 → 1.2.3

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.
@@ -42,7 +42,7 @@ async function Initializer(services) {
42
42
  exports.Initializer = Initializer;
43
43
  class APIService extends BaseService_1.BaseService {
44
44
  static nextLogID = 0;
45
- version = "1.2.2";
45
+ version = "1.2.3";
46
46
  options;
47
47
  netLog = [];
48
48
  constructor(options) {
@@ -37,10 +37,12 @@ export declare class MysqlService extends BaseService {
37
37
  pool: Pool | null;
38
38
  options: MysqlServiceOptions;
39
39
  serviceStarted: boolean;
40
+ timeoutID: any;
40
41
  queries: never[];
41
42
  constructor(options: MysqlServiceOptions);
42
43
  static executeQuery(query: MySqlQuery | MySqlQuery[]): Promise<MysqlResult[]>;
43
44
  init(): Promise<void>;
45
+ recreatePool(): Promise<void>;
44
46
  onApplicationReady(): Promise<void>;
45
47
  static prepareQuery(query: string, fields: MysqlQueryField[]): string;
46
48
  static prepareQueryFieldValue(value: string | number | boolean | null | undefined, system?: boolean): string | number | boolean | null | undefined;
@@ -16,6 +16,7 @@ class MysqlService extends BaseService_1.BaseService {
16
16
  pool = null;
17
17
  options;
18
18
  serviceStarted = false;
19
+ timeoutID;
19
20
  queries = [];
20
21
  constructor(options) {
21
22
  super("mysql");
@@ -24,17 +25,7 @@ class MysqlService extends BaseService_1.BaseService {
24
25
  static async executeQuery(query) { return await exports.REQ_MYSQL_QUERY.request(query); }
25
26
  async init() {
26
27
  this.serviceStarted = false;
27
- const ok = await this.createPool();
28
- if (!ok) {
29
- if (this.options.onLog)
30
- this.options.onLog(2, "Mysql server not connected, retrying in 3 sec");
31
- setTimeout(() => { this.init(); }, 3000);
32
- }
33
- else {
34
- this.serviceStarted = true;
35
- if (this.options.onLog)
36
- this.options.onLog(1, "Mysql Service started!");
37
- }
28
+ await this.recreatePool();
38
29
  exports.REQ_MYSQL_QUERY.listener = async (data) => {
39
30
  if (!Array.isArray(data))
40
31
  data = [data];
@@ -46,6 +37,24 @@ class MysqlService extends BaseService_1.BaseService {
46
37
  return await Promise.all(promises);
47
38
  };
48
39
  }
40
+ async recreatePool() {
41
+ if (this.options.onLog)
42
+ this.options.onLog(2, "Mysql server trying to create pool");
43
+ this.serviceStarted = false;
44
+ const ok = await this.createPool();
45
+ if (!ok) {
46
+ if (this.options.onLog)
47
+ this.options.onLog(2, "Mysql server not connected, retrying in 3 sec");
48
+ if (this.timeoutID)
49
+ clearTimeout(this.timeoutID);
50
+ this.timeoutID = setTimeout(() => { this.recreatePool(); }, 3000);
51
+ }
52
+ else {
53
+ this.serviceStarted = true;
54
+ if (this.options.onLog)
55
+ this.options.onLog(1, "Mysql Service started!");
56
+ }
57
+ }
49
58
  async onApplicationReady() { }
50
59
  static prepareQuery(query, fields) {
51
60
  for (let i of fields) {
@@ -130,14 +139,13 @@ class MysqlService extends BaseService_1.BaseService {
130
139
  });
131
140
  return;
132
141
  }
133
- this.pool.on("error", (evt) => {
134
- if (this.options.onLog)
135
- this.options.onLog(2, `${evt}`);
136
- });
137
142
  this.pool.getConnection((err, conn) => {
138
143
  if (err) {
139
144
  if (this.options.onLog)
140
145
  this.options.onLog(2, `${err}`);
146
+ if (`${err}`.indexOf('ECONNREFUSED') !== -1) {
147
+ this.recreatePool();
148
+ }
141
149
  resolve({
142
150
  error: err,
143
151
  data: null,
@@ -241,6 +249,19 @@ class MysqlService extends BaseService_1.BaseService {
241
249
  if (this.options.onLog)
242
250
  this.options.onLog(1, "Connecting to mysql: \n HOST: " + this.options.host + '\n USER:' + this.options.user + '\n PORT:' + this.options.port);
243
251
  let err = false;
252
+ if (this.pool) {
253
+ try {
254
+ this.pool.removeAllListeners();
255
+ this.pool.end(err => {
256
+ if (err && this.options.onLog)
257
+ this.options.onLog(2, err);
258
+ });
259
+ }
260
+ catch (e) {
261
+ if (this.options.onLog)
262
+ this.options.onLog(2, e);
263
+ }
264
+ }
244
265
  try {
245
266
  this.pool = mysql_1.default.createPool({
246
267
  connectionLimit: this.options.connectionLimit,
@@ -257,7 +278,12 @@ class MysqlService extends BaseService_1.BaseService {
257
278
  this.options.onLog(2, "Can't connect to MYSQL!");
258
279
  err = true;
259
280
  }
260
- 2;
281
+ if (!err && this.pool) {
282
+ this.pool.on("error", (evt) => {
283
+ if (this.options.onLog)
284
+ this.options.onLog(2, `${evt}`);
285
+ });
286
+ }
261
287
  return new Promise((res, rej) => {
262
288
  if (err) {
263
289
  res(false);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "badmfck-api-server",
3
- "version": "1.2.1",
3
+ "version": "1.2.3",
4
4
  "description": "Simple API http server based on express",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",