exodus-framework 2.0.955 → 2.0.958
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.
- package/lib/app/classes/broadcast.js +79 -0
- package/lib/app/classes/cache.js +94 -0
- package/lib/app/classes/communication.js +99 -0
- package/lib/app/classes/communicationcontext.js +1 -0
- package/lib/app/classes/event.js +94 -0
- package/lib/app/classes/index.js +105 -0
- package/lib/app/classes/managed.js +145 -0
- package/lib/app/classes/service.js +232 -0
- package/lib/app/classes/servicemodel.js +22 -0
- package/lib/app/classes/singleton.js +26 -0
- package/lib/app/classes/socket/clienthandler.js +54 -0
- package/lib/app/classes/socket/eventhandler.js +51 -0
- package/lib/app/classes/socket/index.js +20 -0
- package/lib/app/classes/system.js +49 -0
- package/lib/app/controller.js +97 -0
- package/lib/app/core.js +47 -0
- package/lib/app/error.js +66 -0
- package/lib/app/exodus.js +97 -0
- package/lib/app/index.js +68 -0
- package/lib/app/settings.js +161 -0
- package/lib/certificates/private_key.pem +27 -0
- package/lib/certificates/public_key.pem +9 -0
- package/lib/contracts/communication/communication.js +39 -0
- package/lib/contracts/communication/index.js +27 -0
- package/lib/contracts/communication/operation.js +5 -0
- package/lib/contracts/console.js +5 -0
- package/lib/contracts/contansts.js +13 -0
- package/lib/contracts/core.js +12 -0
- package/lib/contracts/entity.js +5 -0
- package/lib/contracts/http.js +52 -0
- package/lib/contracts/index.js +104 -0
- package/lib/contracts/messaging.js +50 -0
- package/lib/contracts/security.js +5 -0
- package/lib/contracts/service.js +11 -0
- package/lib/contracts/session.js +5 -0
- package/lib/contracts/settings.js +5 -0
- package/lib/contracts/singleton.js +5 -0
- package/lib/contracts/socket.js +11 -0
- package/lib/contracts/tasks.js +1 -0
- package/lib/controllers/api/file.js +24 -0
- package/lib/controllers/api/index.js +13 -0
- package/lib/controllers/api/log.js +55 -0
- package/lib/controllers/api/report.js +55 -0
- package/lib/controllers/index.js +16 -0
- package/lib/controllers/messaging/application.js +75 -0
- package/lib/controllers/messaging/database.js +64 -0
- package/lib/controllers/messaging/environment.js +111 -0
- package/lib/express.d.js +5 -0
- package/lib/index.js +104 -0
- package/lib/middlewares/access.js +114 -0
- package/lib/middlewares/authentication.js +104 -0
- package/lib/middlewares/file.js +41 -0
- package/lib/middlewares/index.js +36 -0
- package/lib/models/Application.js +65 -0
- package/lib/models/DatabaseHost.js +62 -0
- package/lib/models/EnvConnection.js +41 -0
- package/lib/models/Log.js +67 -0
- package/lib/models/Report.js +65 -0
- package/lib/models/System.js +54 -0
- package/lib/models/index.js +82 -0
- package/lib/routes/api/index.js +16 -0
- package/lib/routes/api/v1/index.js +38 -0
- package/lib/routes/api/v1/logs.js +19 -0
- package/lib/routes/api/v1/report.js +19 -0
- package/lib/routes/api/v1/sse.js +36 -0
- package/lib/routes/index.js +27 -0
- package/lib/routes/messaging/index.js +34 -0
- package/lib/services/database.d.ts +7 -0
- package/lib/services/database.d.ts.map +1 -1
- package/lib/services/database.js +158 -0
- package/lib/services/ecosystem.js +46 -0
- package/lib/services/express.js +162 -0
- package/lib/services/file.js +65 -0
- package/lib/services/file_new.js +136 -0
- package/lib/services/index.js +108 -0
- package/lib/services/log.js +26 -0
- package/lib/services/rabitmq.js +187 -0
- package/lib/services/redis.js +84 -0
- package/lib/services/security.js +231 -0
- package/lib/services/sequelize.js +313 -0
- package/lib/services/socket.js +123 -0
- package/lib/services/task/TaskServiceBase.js +17 -0
- package/lib/services/task/index.js +40 -0
- package/lib/services/task/queue/QueueService.js +48 -0
- package/lib/services/task/queue/QueueTask.js +134 -0
- package/lib/services/task/queue/SingleQueue.js +68 -0
- package/lib/services/task/queue/index.js +27 -0
- package/lib/services/task/scheduler/SchedulerService.js +31 -0
- package/lib/services/task/scheduler/SchedulerTask.js +211 -0
- package/lib/services/task/scheduler/index.js +20 -0
- package/lib/services/test.js +27 -0
- package/lib/socket.d.js +3 -0
- package/lib/tasks/index.js +27 -0
- package/lib/tasks/queue/application.js +44 -0
- package/lib/tasks/queue/environment.js +92 -0
- package/lib/tasks/queue/index.js +20 -0
- package/lib/tasks/queue/teste.js +21 -0
- package/lib/tasks/schedule/index.js +13 -0
- package/lib/tasks/schedule/restart.js +19 -0
- package/lib/tasks/schedule/teste.js +61 -0
- package/lib/utils/api.js +65 -0
- package/lib/utils/database.js +157 -0
- package/lib/utils/date.js +28 -0
- package/lib/utils/index.js +60 -0
- package/lib/utils/logger.js +51 -0
- package/lib/utils/session.js +23 -0
- package/package.json +1 -1
@@ -0,0 +1,65 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.default = void 0;
|
7
|
+
var _service = _interopRequireDefault(require("../app/classes/service"));
|
8
|
+
var _logger = _interopRequireDefault(require("../utils/logger"));
|
9
|
+
var fs = _interopRequireWildcard(require("fs"));
|
10
|
+
var fsPromisses = _interopRequireWildcard(require("fs/promises"));
|
11
|
+
var _app = require("../app");
|
12
|
+
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
13
|
+
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
14
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
15
|
+
class FileService extends _service.default {
|
16
|
+
async onStart() {
|
17
|
+
await Promise.all([this.checkPaths(), this.cleanTemp()]);
|
18
|
+
return true;
|
19
|
+
}
|
20
|
+
async checkPaths() {
|
21
|
+
for (const path of Object.values(_app.Core.settings.getAppication().filePaths)) {
|
22
|
+
!fs.existsSync(path) && fs.mkdirSync(path, {
|
23
|
+
recursive: true
|
24
|
+
});
|
25
|
+
}
|
26
|
+
}
|
27
|
+
async cleanTemp() {
|
28
|
+
this.listFiles('temporary', files => {
|
29
|
+
for (const file of files) {
|
30
|
+
const p = _app.Core.settings.getAppication().filePaths.temporary + '/' + file;
|
31
|
+
fs.rmSync(p, {
|
32
|
+
recursive: true
|
33
|
+
});
|
34
|
+
}
|
35
|
+
});
|
36
|
+
}
|
37
|
+
async save(filename, destiny) {
|
38
|
+
const p = _app.Core.settings.getAppication().filePaths.temporary + '/' + filename;
|
39
|
+
const d = _app.Core.settings.getAppication().filePaths[destiny] + '/' + filename;
|
40
|
+
return await fsPromisses.rename(p, d).then(() => d).catch(e => {
|
41
|
+
(0, _logger.default)().error({
|
42
|
+
filename,
|
43
|
+
destiny,
|
44
|
+
erro: e
|
45
|
+
}, 'Não foi possível salvar o arquivo');
|
46
|
+
return false;
|
47
|
+
});
|
48
|
+
}
|
49
|
+
async delete(filename, destiny) {
|
50
|
+
const d = _app.Core.settings.getAppication().filePaths[destiny] + '/' + filename;
|
51
|
+
return await fsPromisses.unlink(d).then(() => true).catch(e => {
|
52
|
+
(0, _logger.default)().error({
|
53
|
+
filename,
|
54
|
+
destiny,
|
55
|
+
erro: e
|
56
|
+
}, 'Não foi possível deletar o arquivo');
|
57
|
+
return false;
|
58
|
+
});
|
59
|
+
}
|
60
|
+
listFiles(origin, cb) {
|
61
|
+
const p = _app.Core.settings.getAppication().filePaths[origin];
|
62
|
+
fs.readdir(p, (_, file) => cb(file));
|
63
|
+
}
|
64
|
+
}
|
65
|
+
var _default = exports.default = FileService;
|
@@ -0,0 +1,136 @@
|
|
1
|
+
// import { createHash } from 'crypto';
|
2
|
+
// import * as fs from 'fs';
|
3
|
+
// import * as fsPromisses from 'fs/promises';
|
4
|
+
// import NodeCache from 'node-cache';
|
5
|
+
// import { Core, ErrorHandler } from '../app';
|
6
|
+
// import Service from '../app/classes/service';
|
7
|
+
// import { TFilePathSettings } from '../contracts/settings';
|
8
|
+
// import logger from '../utils/logger';
|
9
|
+
|
10
|
+
// class FileService extends Service {
|
11
|
+
// protected cache: NodeCache;
|
12
|
+
|
13
|
+
// async onServiceInit() {
|
14
|
+
// this.cache = new NodeCache();
|
15
|
+
// await Promise.all([this.checkPaths(), this.cleanTemp()]);
|
16
|
+
// }
|
17
|
+
|
18
|
+
// //# Managment
|
19
|
+
// async save(filename: string, destiny: keyof TFilePathSettings) {
|
20
|
+
// const p = Core.settings.getAppication().filePaths.temporary + '/' + filename;
|
21
|
+
// const d = Core.settings.getAppication().filePaths[destiny] + '/' + filename;
|
22
|
+
|
23
|
+
// try {
|
24
|
+
// const md5 = await this.getMD5FromFile(p);
|
25
|
+
|
26
|
+
// if (this.cache.has(md5)) {
|
27
|
+
// return true;
|
28
|
+
// }
|
29
|
+
|
30
|
+
// await this.move(p, d);
|
31
|
+
// this.cache.set(md5, d);
|
32
|
+
|
33
|
+
// return true;
|
34
|
+
// } catch (error) {
|
35
|
+
// new ErrorHandler('Não foi possível salva o arquivo', error);
|
36
|
+
// return false;
|
37
|
+
// }
|
38
|
+
// }
|
39
|
+
// async delete(filename: string, destiny: keyof TFilePathSettings) {
|
40
|
+
// const d = Core.settings.getAppication().filePaths[destiny] + '/' + filename;
|
41
|
+
// return await fsPromisses
|
42
|
+
// .unlink(d)
|
43
|
+
// .then(() => true)
|
44
|
+
// .catch((e) => {
|
45
|
+
// logger().error({ filename, destiny, erro: e }, 'Não foi possível deletar o arquivo');
|
46
|
+
// return false;
|
47
|
+
// });
|
48
|
+
// }
|
49
|
+
// async move(to: string, from: string) {
|
50
|
+
// return await fsPromisses
|
51
|
+
// .rename(to, from)
|
52
|
+
// .then((d) => d)
|
53
|
+
// .catch((e) => {
|
54
|
+
// logger().error({ to, from, erro: e }, 'Não foi possível mover o arquivo');
|
55
|
+
// return false;
|
56
|
+
// });
|
57
|
+
// }
|
58
|
+
|
59
|
+
// //# Getters
|
60
|
+
// public getBuffer(filename: string, destiny: keyof TFilePathSettings) {
|
61
|
+
// const d = Core.settings.getAppication().filePaths[destiny] + '/' + filename;
|
62
|
+
|
63
|
+
// return new Promise((resolve, reject) => {
|
64
|
+
// this.getMD5FromFile(d).then();
|
65
|
+
|
66
|
+
// fsPromisses
|
67
|
+
// .readFile(path)
|
68
|
+
// .then(cb)
|
69
|
+
// .catch((e) => {
|
70
|
+
// logger.error({ path, erro: e }, 'Não foi possível ler o arquivo', path);
|
71
|
+
// });
|
72
|
+
// });
|
73
|
+
// }
|
74
|
+
// /* public getStatByMD5(filename: string, destiny: keyof TFilePathSettings) {
|
75
|
+
// const p = this.getPathByMD5(md5);
|
76
|
+
// if (!p) return;
|
77
|
+
// return fs.statSync(p);
|
78
|
+
// } */
|
79
|
+
|
80
|
+
// //# Utils
|
81
|
+
// private async checkPaths() {
|
82
|
+
// for (const path of Object.values(Core.settings.getAppication().filePaths)) {
|
83
|
+
// !fs.existsSync(path) && fs.mkdirSync(path, { recursive: true });
|
84
|
+
// }
|
85
|
+
// }
|
86
|
+
// private async cleanTemp() {
|
87
|
+
// this.listFiles('temporary', (files) => {
|
88
|
+
// for (const file of files) {
|
89
|
+
// const p = Core.settings.getAppication().filePaths.temporary + '/' + file;
|
90
|
+
// fs.rmSync(p, { recursive: true });
|
91
|
+
// }
|
92
|
+
// });
|
93
|
+
// }
|
94
|
+
// public listFiles(origin: keyof TFilePathSettings, cb: (file: string[]) => void) {
|
95
|
+
// const p = Core.settings.getAppication().filePaths[origin];
|
96
|
+
// fs.readdir(p, (_, file) => cb(file));
|
97
|
+
// }
|
98
|
+
|
99
|
+
// //# MD5
|
100
|
+
// public getMD5FromFile(filePath: string) {
|
101
|
+
// return new Promise<string>((resolve, reject) => {
|
102
|
+
// const hash = createHash('md5');
|
103
|
+
// const stream = fs.createReadStream(filePath);
|
104
|
+
|
105
|
+
// stream.on('data', (dados) => hash.update(dados));
|
106
|
+
// stream.on('error', (error) => reject(error));
|
107
|
+
// stream.on('end', () => resolve(hash.digest('hex')));
|
108
|
+
// });
|
109
|
+
// }
|
110
|
+
// public getMD5FromBuffer(buffer: Buffer) {
|
111
|
+
// const hash = createHash('md5');
|
112
|
+
// hash.update(buffer);
|
113
|
+
// return hash.digest('hex');
|
114
|
+
// }
|
115
|
+
|
116
|
+
// //# Cache
|
117
|
+
// public loadCache() {}
|
118
|
+
// public async hasCached(filePath: string) {
|
119
|
+
// const md5 = await this.getMD5FromFile(filePath);
|
120
|
+
|
121
|
+
// return this.cache.has(md5);
|
122
|
+
// }
|
123
|
+
// //!imcomplete
|
124
|
+
// /* public async loadItem(filename: string, origin: keyof TFilePathSettings) {
|
125
|
+
// try {
|
126
|
+
// const p = Core.settings.getAppication().filePaths[origin] + '/' + filename;
|
127
|
+
// const md5 = await this.getMD5FromFile(p);
|
128
|
+
|
129
|
+
// return md5;
|
130
|
+
// } catch (error) {
|
131
|
+
// return;
|
132
|
+
// }
|
133
|
+
// } */
|
134
|
+
// }
|
135
|
+
// export default FileService;
|
136
|
+
"use strict";
|
@@ -0,0 +1,108 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
var _exportNames = {
|
7
|
+
ExpressService: true,
|
8
|
+
FileService: true,
|
9
|
+
RabbitMQService: true,
|
10
|
+
RedisService: true,
|
11
|
+
SequelizeService: true,
|
12
|
+
SocketIOService: true,
|
13
|
+
SecurityService: true,
|
14
|
+
LogService: true,
|
15
|
+
EcosystemService: true,
|
16
|
+
TestService: true,
|
17
|
+
DatabaseService: true
|
18
|
+
};
|
19
|
+
Object.defineProperty(exports, "DatabaseService", {
|
20
|
+
enumerable: true,
|
21
|
+
get: function () {
|
22
|
+
return _database.default;
|
23
|
+
}
|
24
|
+
});
|
25
|
+
Object.defineProperty(exports, "EcosystemService", {
|
26
|
+
enumerable: true,
|
27
|
+
get: function () {
|
28
|
+
return _ecosystem.default;
|
29
|
+
}
|
30
|
+
});
|
31
|
+
Object.defineProperty(exports, "ExpressService", {
|
32
|
+
enumerable: true,
|
33
|
+
get: function () {
|
34
|
+
return _express.default;
|
35
|
+
}
|
36
|
+
});
|
37
|
+
Object.defineProperty(exports, "FileService", {
|
38
|
+
enumerable: true,
|
39
|
+
get: function () {
|
40
|
+
return _file.default;
|
41
|
+
}
|
42
|
+
});
|
43
|
+
Object.defineProperty(exports, "LogService", {
|
44
|
+
enumerable: true,
|
45
|
+
get: function () {
|
46
|
+
return _log.default;
|
47
|
+
}
|
48
|
+
});
|
49
|
+
Object.defineProperty(exports, "RabbitMQService", {
|
50
|
+
enumerable: true,
|
51
|
+
get: function () {
|
52
|
+
return _rabitmq.default;
|
53
|
+
}
|
54
|
+
});
|
55
|
+
Object.defineProperty(exports, "RedisService", {
|
56
|
+
enumerable: true,
|
57
|
+
get: function () {
|
58
|
+
return _redis.default;
|
59
|
+
}
|
60
|
+
});
|
61
|
+
Object.defineProperty(exports, "SecurityService", {
|
62
|
+
enumerable: true,
|
63
|
+
get: function () {
|
64
|
+
return _security.default;
|
65
|
+
}
|
66
|
+
});
|
67
|
+
Object.defineProperty(exports, "SequelizeService", {
|
68
|
+
enumerable: true,
|
69
|
+
get: function () {
|
70
|
+
return _sequelize.default;
|
71
|
+
}
|
72
|
+
});
|
73
|
+
Object.defineProperty(exports, "SocketIOService", {
|
74
|
+
enumerable: true,
|
75
|
+
get: function () {
|
76
|
+
return _socket.default;
|
77
|
+
}
|
78
|
+
});
|
79
|
+
Object.defineProperty(exports, "TestService", {
|
80
|
+
enumerable: true,
|
81
|
+
get: function () {
|
82
|
+
return _test.default;
|
83
|
+
}
|
84
|
+
});
|
85
|
+
var _express = _interopRequireDefault(require("./express"));
|
86
|
+
var _file = _interopRequireDefault(require("./file"));
|
87
|
+
var _rabitmq = _interopRequireDefault(require("./rabitmq"));
|
88
|
+
var _redis = _interopRequireDefault(require("./redis"));
|
89
|
+
var _sequelize = _interopRequireDefault(require("./sequelize"));
|
90
|
+
var _socket = _interopRequireDefault(require("./socket"));
|
91
|
+
var _security = _interopRequireDefault(require("./security"));
|
92
|
+
var _log = _interopRequireDefault(require("./log"));
|
93
|
+
var _ecosystem = _interopRequireDefault(require("./ecosystem"));
|
94
|
+
var _test = _interopRequireDefault(require("./test"));
|
95
|
+
var _database = _interopRequireDefault(require("./database"));
|
96
|
+
var _task = require("./task");
|
97
|
+
Object.keys(_task).forEach(function (key) {
|
98
|
+
if (key === "default" || key === "__esModule") return;
|
99
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
100
|
+
if (key in exports && exports[key] === _task[key]) return;
|
101
|
+
Object.defineProperty(exports, key, {
|
102
|
+
enumerable: true,
|
103
|
+
get: function () {
|
104
|
+
return _task[key];
|
105
|
+
}
|
106
|
+
});
|
107
|
+
});
|
108
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
@@ -0,0 +1,26 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.default = void 0;
|
7
|
+
var _dateFns = require("date-fns");
|
8
|
+
var _app = require("../app");
|
9
|
+
var _Log = require("../models/Log");
|
10
|
+
var _sequelize = _interopRequireDefault(require("./sequelize"));
|
11
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
12
|
+
class LogService extends _app.Service {
|
13
|
+
async register(tenantId, data) {
|
14
|
+
try {
|
15
|
+
const Entity = await _sequelize.default.singleton().getModel(_Log.Log, tenantId);
|
16
|
+
await Entity.create({
|
17
|
+
...data,
|
18
|
+
happenedAt: (0, _dateFns.format)(new Date(), 'yyyy-MM-dd HH:mm:ss')
|
19
|
+
});
|
20
|
+
return true;
|
21
|
+
} catch (error) {
|
22
|
+
new _app.ErrorHandler('Não foi possível registrar um log', error);
|
23
|
+
}
|
24
|
+
}
|
25
|
+
}
|
26
|
+
var _default = exports.default = LogService;
|
@@ -0,0 +1,187 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.default = void 0;
|
7
|
+
var _amqplib = _interopRequireDefault(require("amqplib"));
|
8
|
+
var _crypto = require("crypto");
|
9
|
+
var _app = require("../app");
|
10
|
+
var _service = _interopRequireDefault(require("../app/classes/service"));
|
11
|
+
var _messaging = _interopRequireDefault(require("../routes/messaging"));
|
12
|
+
var _utils = require("../utils");
|
13
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
14
|
+
/**
|
15
|
+
* Serviço de Mensageria
|
16
|
+
*
|
17
|
+
* @class RabbitMQService
|
18
|
+
* @extends {Service}
|
19
|
+
* @implements {IService}
|
20
|
+
*/
|
21
|
+
class RabbitMQService extends _service.default {
|
22
|
+
connection;
|
23
|
+
channel;
|
24
|
+
mainRouter;
|
25
|
+
eventCustomListeners;
|
26
|
+
exchangeName;
|
27
|
+
queueName;
|
28
|
+
isReconnecting = false; // Flag para evitar múltiplas tentativas simultâneas de reconexão
|
29
|
+
isConnected = false; // Flag para controlar o estado da conexão
|
30
|
+
|
31
|
+
async onServiceClusterInit() {
|
32
|
+
await super.onServiceClusterInit();
|
33
|
+
if (!this.mainRouter) throw new Error('Need router'); //!sem router
|
34
|
+
|
35
|
+
//Start params
|
36
|
+
this.startAttemptDelay = 5 * 1000;
|
37
|
+
this.maxStartAttempts = 3;
|
38
|
+
this.exchangeName = _app.Core.settings.getMessaging().exchanges.main;
|
39
|
+
this.queueName = _app.Core.settings.getAppication().appId;
|
40
|
+
this.eventCustomListeners = new Map();
|
41
|
+
this.mainRouter = {
|
42
|
+
..._messaging.default,
|
43
|
+
...this.mainRouter
|
44
|
+
}; // # Register native router
|
45
|
+
}
|
46
|
+
async onStartCluster() {
|
47
|
+
await super.onStartCluster();
|
48
|
+
return this.connect();
|
49
|
+
}
|
50
|
+
async onExodusClusterStarted() {
|
51
|
+
await super.onExodusClusterStarted();
|
52
|
+
await this.channel.consume(this.queueName, this.consumeFromQueue.bind(this), {
|
53
|
+
noAck: false
|
54
|
+
});
|
55
|
+
}
|
56
|
+
async onStartFail() {
|
57
|
+
await super.onStartFail();
|
58
|
+
this.log(`Not possible to connect RabitMQ`, 'danger');
|
59
|
+
}
|
60
|
+
async connect() {
|
61
|
+
if (this.isConnected) return true;
|
62
|
+
if (this.isReconnecting) return false;
|
63
|
+
this.log(`⌛️ Connecting to Rabbit-MQ Server: ${this.currentStartAttempts}/${this.maxStartAttempts}`);
|
64
|
+
this.isReconnecting = true;
|
65
|
+
try {
|
66
|
+
const query = `amqp://${_app.Core.settings.getMessaging().user}:${_app.Core.settings.getMessaging().pass}@${_app.Core.settings.getMessaging().host}:5672`;
|
67
|
+
this.connection = await _amqplib.default.connect(query);
|
68
|
+
this.channel = await this.connection.createChannel();
|
69
|
+
const mainExchanges = _app.Core.settings.getMessaging().exchanges.main;
|
70
|
+
await this.channel.assertExchange(mainExchanges, 'topic', {
|
71
|
+
durable: true
|
72
|
+
});
|
73
|
+
await this.channel.assertQueue(_app.Core.settings.getAppication().appId, {
|
74
|
+
durable: true
|
75
|
+
});
|
76
|
+
this.isConnected = true;
|
77
|
+
this.isReconnecting = false;
|
78
|
+
this.setupErrorHandles();
|
79
|
+
await this.registerHandles();
|
80
|
+
this.log(`✅ Rabbit MQ Connection is ready`);
|
81
|
+
return true;
|
82
|
+
} catch (error) {
|
83
|
+
(0, _utils.logger)().error(error, `Not connected to MQ Server`);
|
84
|
+
this.isReconnecting = false;
|
85
|
+
return false;
|
86
|
+
}
|
87
|
+
}
|
88
|
+
async registerHandles() {
|
89
|
+
for (const topic of Object.keys(this.mainRouter)) {
|
90
|
+
await this.channel.bindQueue(this.queueName, this.exchangeName, topic);
|
91
|
+
}
|
92
|
+
}
|
93
|
+
setupErrorHandles() {
|
94
|
+
this.channel.on('error', err => {
|
95
|
+
new _app.ErrorHandler('falha no canal rabitmq', err);
|
96
|
+
this.isConnected = false;
|
97
|
+
});
|
98
|
+
this.channel.on('close', err => {
|
99
|
+
new _app.ErrorHandler('o canal rabitmq foi fechado', err);
|
100
|
+
this.isConnected = false;
|
101
|
+
});
|
102
|
+
}
|
103
|
+
|
104
|
+
//# Common
|
105
|
+
async sendToQueue(topic, data, transactionId) {
|
106
|
+
try {
|
107
|
+
if (!this.isConnected) {
|
108
|
+
this.log('RabbitMQ is not connected. Message not sent.');
|
109
|
+
return false;
|
110
|
+
}
|
111
|
+
const messageId = (0, _crypto.randomUUID)();
|
112
|
+
const packeg = Buffer.from(JSON.stringify({
|
113
|
+
data,
|
114
|
+
serviceId: _app.Core.settings.getAppication().appId,
|
115
|
+
transactionId
|
116
|
+
}));
|
117
|
+
this.channel.publish(this.exchangeName, topic, packeg, {
|
118
|
+
messageId,
|
119
|
+
persistent: true
|
120
|
+
});
|
121
|
+
return messageId;
|
122
|
+
} catch (error) {
|
123
|
+
new _app.ErrorHandler('Houve um erro no envio de uma mensagem para o rabitmq', error);
|
124
|
+
}
|
125
|
+
}
|
126
|
+
async consumeFromQueue(msg) {
|
127
|
+
if (!this.isConnected) return;
|
128
|
+
if (!msg) {
|
129
|
+
this.log(`Invalid incoming message in`);
|
130
|
+
return;
|
131
|
+
}
|
132
|
+
try {
|
133
|
+
const topic = msg.fields.routingKey;
|
134
|
+
const content = JSON.parse(msg.content.toString());
|
135
|
+
const callBack = this.mainRouter[topic];
|
136
|
+
if (callBack) await callBack(content.data, () => {}, content.serviceId, content.transactionId);
|
137
|
+
this.channel.ack(msg); //# the next function is temporary and not confirm ack
|
138
|
+
|
139
|
+
if (this.eventCustomListeners.has(topic)) {
|
140
|
+
const cb = this.eventCustomListeners.get(topic);
|
141
|
+
cb(content.data, () => {}, content.serviceId, content.transactionId);
|
142
|
+
}
|
143
|
+
} catch (error) {
|
144
|
+
setTimeout(() => {
|
145
|
+
this.channel.nack(msg, false, true); // Reenfileira para nova tentativa
|
146
|
+
this.log(`Message requeue: ${msg.fields.routingKey}`, 'info');
|
147
|
+
}, 3000);
|
148
|
+
new _app.ErrorHandler('Error on parsing message from queue', error);
|
149
|
+
}
|
150
|
+
}
|
151
|
+
static registerRouter(router) {
|
152
|
+
this.getService().mainRouter = router;
|
153
|
+
}
|
154
|
+
onTransaction(topic, callBack) {
|
155
|
+
if (!this.mainRouter[topic]) {
|
156
|
+
this.channel.bindQueue(_app.Core.settings.getAppication().appId, _app.Core.settings.getMessaging().exchanges.main, topic);
|
157
|
+
}
|
158
|
+
this.eventCustomListeners.set(topic, callBack);
|
159
|
+
}
|
160
|
+
offTransaction(topic) {
|
161
|
+
this.eventCustomListeners.delete(topic);
|
162
|
+
if (!this.mainRouter[topic]) {
|
163
|
+
this.channel.unbindQueue(_app.Core.settings.getAppication().appId, _app.Core.settings.getMessaging().exchanges.main, topic);
|
164
|
+
}
|
165
|
+
}
|
166
|
+
asyncRequest(data) {
|
167
|
+
const timeout = data.listen.timeout || 5000;
|
168
|
+
const send = data.send;
|
169
|
+
const listen = data.listen;
|
170
|
+
const sendtransactionId = (0, _crypto.randomUUID)();
|
171
|
+
this.onTransaction(listen.topic, (data, ack, serviceId, transactionId) => {
|
172
|
+
ack(); // !auto ack because rabbit app crash
|
173
|
+
if (sendtransactionId != transactionId) {
|
174
|
+
return;
|
175
|
+
}
|
176
|
+
this.offTransaction(listen.topic);
|
177
|
+
clearTimeout(fnTimeout);
|
178
|
+
listen.callback(data, ack, serviceId, transactionId);
|
179
|
+
});
|
180
|
+
const fnTimeout = setTimeout(() => {
|
181
|
+
this.offTransaction(listen.topic);
|
182
|
+
listen.onTimeout();
|
183
|
+
}, timeout);
|
184
|
+
this.sendToQueue(send.topic, send.data, sendtransactionId);
|
185
|
+
}
|
186
|
+
}
|
187
|
+
var _default = exports.default = RabbitMQService;
|
@@ -0,0 +1,84 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.default = void 0;
|
7
|
+
var _redis = require("redis");
|
8
|
+
var _app = require("../app");
|
9
|
+
var _service = _interopRequireDefault(require("../app/classes/service"));
|
10
|
+
var _cache = require("../app/classes/cache");
|
11
|
+
var _utils = require("../utils");
|
12
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
13
|
+
/**
|
14
|
+
* Serviço de cache
|
15
|
+
*
|
16
|
+
* @class RedisService
|
17
|
+
* @extends {Service}
|
18
|
+
* @implements {IService}
|
19
|
+
*/
|
20
|
+
class RedisService extends _service.default {
|
21
|
+
client;
|
22
|
+
isReconnecting = false; // Flag para evitar múltiplas tentativas simultâneas de reconexão
|
23
|
+
isConnected = false; // Flag para controlar o estado da conexão
|
24
|
+
|
25
|
+
async onServiceInit() {
|
26
|
+
await super.onServiceInit();
|
27
|
+
this.startAttemptDelay = 5 * 1000;
|
28
|
+
this.maxStartAttempts = 3;
|
29
|
+
}
|
30
|
+
async onStart() {
|
31
|
+
await super.onStart();
|
32
|
+
return this.connect();
|
33
|
+
}
|
34
|
+
async onStartFail() {
|
35
|
+
await super.onStartFail();
|
36
|
+
this.log(`Not possible to connect Redis`, 'danger');
|
37
|
+
}
|
38
|
+
async connect() {
|
39
|
+
if (this.isConnected) return true;
|
40
|
+
if (this.isReconnecting) return false;
|
41
|
+
this.log(`⌛️ Connecting to Redis Server: ${this.currentStartAttempts}/${this.maxStartAttempts}`);
|
42
|
+
this.isReconnecting = true;
|
43
|
+
try {
|
44
|
+
const queryConnect = `redis://${_app.Core.settings.getCache().host}:${_app.Core.settings.getCache().port}`;
|
45
|
+
const password = _app.Core.settings.getCache().password;
|
46
|
+
this.client = (0, _redis.createClient)({
|
47
|
+
url: queryConnect,
|
48
|
+
password
|
49
|
+
});
|
50
|
+
await this.client.connect();
|
51
|
+
this.isConnected = true;
|
52
|
+
this.isReconnecting = false;
|
53
|
+
this.setupErrorHandles();
|
54
|
+
this.log(`✅ Redis Connection is ready`);
|
55
|
+
return true;
|
56
|
+
} catch (error) {
|
57
|
+
(0, _utils.logger)().error(error, `Not connected to Redis Server`);
|
58
|
+
this.isReconnecting = false;
|
59
|
+
return false;
|
60
|
+
}
|
61
|
+
}
|
62
|
+
setupErrorHandles() {
|
63
|
+
this.client.on('error', err => {
|
64
|
+
new _app.ErrorHandler('falha na conexão Redis', err);
|
65
|
+
this.isConnected = false;
|
66
|
+
});
|
67
|
+
}
|
68
|
+
|
69
|
+
//* Common
|
70
|
+
async setValue(key, value) {
|
71
|
+
await this.client.set(key, value);
|
72
|
+
}
|
73
|
+
async getValue(key) {
|
74
|
+
const value = await this.client.get(key);
|
75
|
+
return value;
|
76
|
+
}
|
77
|
+
async deleteValue(key) {
|
78
|
+
await this.client.del(key);
|
79
|
+
}
|
80
|
+
createCache(key) {
|
81
|
+
return new _cache.RedisCache(this.client, key);
|
82
|
+
}
|
83
|
+
}
|
84
|
+
var _default = exports.default = RedisService;
|