Commit 402ba9dec54f2259e1d1d0121c67e9d13615723b
1 parent
93303acb84
Exists in
master
resync db-mysql dari komodo
Showing 2 changed files with 111 additions and 18 deletions Side-by-side Diff
lib/db-mysql.js
1 | -'use strict'; | |
1 | +const HEALTHY_CHECK_INTERVAL_MS = 10000; | |
2 | + | |
3 | +const MODULE_NAME = require('path').basename(__filename); | |
2 | 4 | |
3 | 5 | const mysql = require('mysql'); |
4 | 6 | |
5 | 7 | const config = require('komodo-sdk/config'); |
8 | +const logger = require('komodo-sdk/logger'); | |
9 | + | |
10 | +const connectionLimit = config.mysql && config.mysql.pool_connection_limit | |
11 | + ? config.mysql.pool_connection_limit | |
12 | + : 0; | |
6 | 13 | |
7 | -const connectionLimit = config.mysql && config.mysql.pool_connection_limit ? config.mysql.pool_connection_limit : 0; | |
14 | +const ERROR_POOL_NOT_READY = new Error(`${MODULE_NAME}: pool is not ready`); | |
15 | +exports.ERROR_POOL_NOT_READY = ERROR_POOL_NOT_READY; | |
8 | 16 | |
9 | 17 | const pool = config.mysql ? mysql.createPool({ |
10 | - connectionLimit: connectionLimit, | |
18 | + connectionLimit, | |
11 | 19 | host: config.mysql.host || 'localhost', |
12 | 20 | database: config.mysql.database || 'komodo', |
13 | 21 | user: config.mysql.user || 'komodo', |
14 | - password: config.mysql.password | |
22 | + password: config.mysql.password, | |
23 | + timezone: config.mysql.timezone, | |
15 | 24 | }) : null; |
16 | 25 | |
17 | 26 | exports.pool = pool; |
18 | 27 | |
19 | -exports.format = (sql, values) => { | |
20 | - return new Promise((resolve, reject) => { | |
21 | - if (!pool) { | |
22 | - reject('Missing DB config'); | |
28 | +exports.query = (query, values, cb) => { | |
29 | + // pool.query.apply(null, arguments); | |
30 | + if (!pool || !pool.query) { | |
31 | + logger.warn(`${MODULE_NAME}: ${ERROR_POOL_NOT_READY.toString()}`); | |
32 | + if (typeof cb === 'function') { | |
33 | + cb(ERROR_POOL_NOT_READY); | |
34 | + } | |
35 | + | |
36 | + return; | |
37 | + } | |
38 | + | |
39 | + pool.query(query, values, cb); | |
40 | +}; | |
41 | + | |
42 | +exports.format = (sql, values, cb) => new Promise((resolve, reject) => { | |
43 | + if (!pool) { | |
44 | + reject(ERROR_POOL_NOT_READY); | |
45 | + if (typeof cb === 'function') cb(ERROR_POOL_NOT_READY); | |
46 | + return; | |
47 | + } | |
48 | + | |
49 | + pool.getConnection((err, connection) => { | |
50 | + if (err) { | |
51 | + reject(err); | |
52 | + if (typeof cb === 'function') cb(err); | |
53 | + return; | |
54 | + } | |
55 | + | |
56 | + const formatted = connection.format(sql, values); | |
57 | + connection.release(); | |
58 | + | |
59 | + resolve(formatted); | |
60 | + if (typeof cb === 'function') cb(null, formatted); | |
61 | + }); | |
62 | +}); | |
63 | + | |
64 | +exports.beginConnection = (cb) => new Promise((resolve) => { | |
65 | + pool.getConnection((errGetConnection, connection) => { | |
66 | + if (errGetConnection) { | |
67 | + resolve([errGetConnection]); | |
68 | + if (typeof cb === 'function') cb(errGetConnection); | |
23 | 69 | return; |
24 | 70 | } |
25 | 71 | |
26 | - pool.getConnection((err, connection) => { | |
27 | - if (err) { | |
28 | - reject(err); | |
72 | + connection.beginTransaction((errBeginTransaction) => { | |
73 | + if (errBeginTransaction) { | |
74 | + resolve([errBeginTransaction]); | |
75 | + if (typeof cb === 'function') cb(errBeginTransaction); | |
29 | 76 | return; |
30 | 77 | } |
31 | 78 | |
32 | - resolve(connection.format(sql, values)); | |
33 | - connection.release(); | |
79 | + resolve([null, connection]); | |
80 | + if (typeof cb === 'function') cb(null, connection); | |
34 | 81 | }); |
35 | - }) | |
36 | -} | |
37 | 82 | \ No newline at end of file |
83 | + }); | |
84 | +}); | |
85 | + | |
86 | +exports.getBy = (tableName, fieldName, value, cb) => new Promise((resolve) => { | |
87 | + const query = 'SELECT * FROM ?? WHERE ?? = ? LIMIT 1'; | |
88 | + const values = [tableName, fieldName, value]; | |
89 | + pool.query(query, values, (err, results) => { | |
90 | + const result = results && results[0]; | |
91 | + resolve([err, result || null]); | |
92 | + if (typeof cb === 'function') cb(err, result || null); | |
93 | + }); | |
94 | +}); | |
95 | + | |
96 | +function healthyCheck() { | |
97 | + const query = 'SELECT 1'; | |
98 | + const values = []; | |
99 | + | |
100 | + if (!pool) { | |
101 | + logger.warn(`${MODULE_NAME}: Skip healthy check on undefined pool (ERR-EB9E5C08)`); | |
102 | + return; | |
103 | + } | |
104 | + | |
105 | + if (!pool.query) { | |
106 | + logger.warn(`${MODULE_NAME}: Skip healthy check on undefined pool.query (ERR-D10F70F3)`); | |
107 | + return; | |
108 | + } | |
109 | + | |
110 | + pool.query(query, values, (err) => { | |
111 | + if (err) { | |
112 | + logger.warn(`${MODULE_NAME}: Error on healthy check (ERR-38EC9B78)`, { err }); | |
113 | + } | |
114 | + }); | |
115 | +} | |
116 | + | |
117 | +setInterval(() => { | |
118 | + const randomMs = Math.floor(Math.random() * HEALTHY_CHECK_INTERVAL_MS * 0.3); | |
119 | + setTimeout(() => { | |
120 | + try { | |
121 | + healthyCheck(); | |
122 | + } catch (err) { | |
123 | + logger.warn(`${MODULE_NAME}: Exception on periodic healthy check (ERR-2D137502)`, { err }); | |
124 | + } | |
125 | + }, randomMs); | |
126 | +}, HEALTHY_CHECK_INTERVAL_MS); |
lib/messages-archive.js
... | ... | @@ -52,7 +52,10 @@ function insert(params, direction) { |
52 | 52 | direction |
53 | 53 | ); |
54 | 54 | |
55 | - if (!db.pool) return; | |
55 | + if (!db.pool) { | |
56 | + logger.warn('MESSAGE-ARCHIVE: DB POOL is not ready to insert message history'); | |
57 | + return; | |
58 | + } | |
56 | 59 | |
57 | 60 | const query = `INSERT INTO messages SET ?`; |
58 | 61 | const values = [{ |
... | ... | @@ -63,9 +66,10 @@ function insert(params, direction) { |
63 | 66 | message: (params.msg || params.message).trim(), |
64 | 67 | }]; |
65 | 68 | |
66 | - db.pool.query(query, values, (err) => { | |
69 | + db.pool.query(query, values, async (err) => { | |
67 | 70 | if (err) { |
68 | - logger.warn(`MESSAGES-ARCHIVE: DB ERROR on inserting message. ${err.toString()}`); | |
71 | + const fullQuery = await db.format(query, values); | |
72 | + logger.warn(`MESSAGES-ARCHIVE: DB ERROR on inserting message. ${err.toString()}`, { query: fullQuery }); | |
69 | 73 | } |
70 | 74 | }); |
71 | 75 | } |