diff --git a/core/db_connector.js b/core/db_connector.js index b3d39f7..9f808c8 100644 --- a/core/db_connector.js +++ b/core/db_connector.js @@ -90,7 +90,7 @@ class DBConnector { throw new ServerError(glConst.ERR_DB_DISCONNECT, e.message); } } - //Исполнить запрос + //Получить список сервисов async getServices() { try { let res = await this.connector.getServices(this.connection); @@ -99,6 +99,15 @@ class DBConnector { throw new ServerError(glConst.ERR_DB_EXECUTE, e.message); } } + //Запись в журнал работы + async putLog(msg, queueID) { + try { + let res = await this.connector.log(this.connection, msg, queueID); + return res; + } catch (e) { + throw new ServerError(glConst.ERR_DB_EXECUTE, e.message); + } + } } //----------------- diff --git a/index.js b/index.js index 5828bed..013d9aa 100644 --- a/index.js +++ b/index.js @@ -26,27 +26,37 @@ a.connect() a.getServices() .then(res => { console.log(res); - setTimeout(() => { - a.disconnect() - .then(res => { - console.log("DISCONNECTED"); - }) - .catch(e => { - console.log(e.code + ": " + e.message); - }); - }, 10000); + a.putLog("Сервер приложений подключен") + .then(res => { + console.log(res); + a.disconnect() + .then(res => { + console.log("DISCONNECTED"); + }) + .catch(e => { + console.log(e.code + ": " + e.message); + }); + }) + .catch(e => { + console.log(e.code + ": " + e.message); + a.disconnect() + .then(res => { + console.log("DISCONNECTED"); + }) + .catch(e => { + console.log(e.code + ": " + e.message); + }); + }); }) .catch(e => { console.log(e.code + ": " + e.message); - setTimeout(() => { - a.disconnect() - .then(res => { - console.log("DISCONNECTED"); - }) - .catch(e => { - console.log(e.code + ": " + e.message); - }); - }, 10000); + a.disconnect() + .then(res => { + console.log("DISCONNECTED"); + }) + .catch(e => { + console.log(e.code + ": " + e.message); + }); }); }) .catch(e => { diff --git a/modules/parus_db.js b/modules/parus_db.js index 3cda739..8f07cb8 100644 --- a/modules/parus_db.js +++ b/modules/parus_db.js @@ -22,9 +22,9 @@ const connect = prms => { password: prms.password, connectString: prms.connectString }, - function(err, connection) { + (err, connection) => { if (err) { - reject(err); + reject(new Error(err.message)); } else { resolve(connection); } @@ -37,9 +37,9 @@ const connect = prms => { const disconnect = connection => { return new Promise((resolve, reject) => { if (connection) { - connection.close(function(err) { + connection.close(err => { if (err) { - reject(err); + reject(new Error(err.message)); } else { resolve(); } @@ -54,12 +54,28 @@ const disconnect = connection => { const getServices = connection => { return new Promise((resolve, reject) => { if (connection) { - connection.execute("select * from EXSSERVICE", [], { outFormat: oracledb.OBJECT }, (err, result) => { - if (err) { - reject(err); + connection.execute( + "BEGIN PKG_EXS.SERVICE_GET(RCSERVICES => :RCSERVICES); END;", + { RCSERVICES: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT } }, + { outFormat: oracledb.OBJECT }, + (err, result) => { + if (err) { + reject(new Error(err.message)); + } + let cursor = result.outBinds.RCSERVICES; + let queryStream = cursor.toQueryStream(); + let rows = []; + queryStream.on("data", row => { + rows.push(row); + }); + queryStream.on("error", err => { + reject(new Error(err.message)); + }); + queryStream.on("close", () => { + resolve(rows); + }); } - resolve(result.rows); - }); + ); } else { reject(new Error("Не указано подключение")); } @@ -67,7 +83,41 @@ const getServices = connection => { }; //Запись в протокол работы -const log = prms => {}; +const log = (connection, msg, queueID) => { + return new Promise((resolve, reject) => { + if (connection) { + connection.execute( + "BEGIN PKG_EXS.LOG_PUT(NLOG_STATE => :NLOG_STATE, SMSG => :SMSG, NEXSQUEUE => :NEXSQUEUE, RCLOG => :RCLOG); END;", + { + NLOG_STATE: 0, + SMSG: msg, + NEXSQUEUE: queueID, + RCLOG: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT } + }, + { outFormat: oracledb.OBJECT, autoCommit: true }, + (err, result) => { + if (err) { + reject(new Error(err.message)); + } + let cursor = result.outBinds.RCLOG; + let queryStream = cursor.toQueryStream(); + let rows = []; + queryStream.on("data", row => { + rows.push(row); + }); + queryStream.on("error", err => { + reject(new Error(err.message)); + }); + queryStream.on("close", () => { + resolve(rows[0]); + }); + } + ); + } else { + reject(new Error("Не указано подключение")); + } + }); +}; //Считывание очередной порции исходящих сообщений из очереди const getQueueOutgoing = prms => {};