@puca/puca-core 12.13.13 → 12.14.0
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/dist/core/services/domain-life-cycle.service.d.ts +1 -3
- package/dist/core/services/domain-life-cycle.service.js +1 -4
- package/dist/core/services/domain-life-cycle.service.js.map +1 -1
- package/dist/core/services/index.js +3 -3
- package/dist/core/services/index.js.map +1 -1
- package/dist/core/services/llm/llm.service.d.ts +2 -11
- package/dist/core/services/llm/llm.service.js +70 -123
- package/dist/core/services/llm/llm.service.js.map +1 -1
- package/dist/core/services/llm/token-budget.service.d.ts +2 -7
- package/dist/core/services/llm/token-budget.service.js +23 -57
- package/dist/core/services/llm/token-budget.service.js.map +1 -1
- package/dist/core/services/mcp/mcp-server.service.d.ts +1 -16
- package/dist/core/services/mcp/mcp-server.service.js +9 -17
- package/dist/core/services/mcp/mcp-server.service.js.map +1 -1
- package/dist/core/services/monitor.d.ts +4 -5
- package/dist/core/services/monitor.js +5 -14
- package/dist/core/services/monitor.js.map +1 -1
- package/dist/core/services/mq/mq.service.d.ts +9 -1
- package/dist/core/services/mq/mq.service.js +20 -99
- package/dist/core/services/mq/mq.service.js.map +1 -1
- package/dist/core/services/report.service.js +0 -13
- package/dist/core/services/report.service.js.map +1 -1
- package/dist/core/services/task.service.d.ts +0 -2
- package/dist/core/services/task.service.js.map +1 -1
- package/dist/essential/module-config.service.js +2 -2
- package/dist/essential/module-config.service.js.map +1 -1
- package/dist/packages/crm/crm.service.js +1 -3
- package/dist/packages/crm/crm.service.js.map +1 -1
- package/dist/plugins/final-user-query-language/final-user-query-language.js +5 -5
- package/dist/plugins/final-user-query-language/final-user-query-language.js.map +1 -1
- package/dist/plugins/final-user-query-language/fuql-2-pql-conversor.d.ts +0 -3
- package/dist/plugins/final-user-query-language/fuql-2-pql-conversor.js +6 -13
- package/dist/plugins/final-user-query-language/fuql-2-pql-conversor.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/fuql-grammar.js +6 -113
- package/fuql-grammar.ne +6 -113
- package/package.json +1 -1
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.TokenBudgetService = void 0;
|
|
4
|
-
const _ = require("lodash");
|
|
5
4
|
const essential_1 = require("../../../essential");
|
|
6
5
|
const logger_1 = require("../logger");
|
|
7
6
|
class TokenBudgetService {
|
|
@@ -26,16 +25,6 @@ class TokenBudgetService {
|
|
|
26
25
|
if (!maxSharedTokenBudget) {
|
|
27
26
|
this.logger.debug('No max shared token budget defined');
|
|
28
27
|
}
|
|
29
|
-
if (sharedTokenBudget < 0) {
|
|
30
|
-
this.logger.debug('Shared token budget is negative, resetting to 0');
|
|
31
|
-
sharedTokenBudget = 0;
|
|
32
|
-
await this.secondaryDatabase.set(`shared-token-budget`, `${sharedTokenBudget}`);
|
|
33
|
-
}
|
|
34
|
-
else if (sharedTokenBudget > maxSharedTokenBudget) {
|
|
35
|
-
this.logger.debug('Shared token budget is greater than max, resetting to max');
|
|
36
|
-
sharedTokenBudget = maxSharedTokenBudget;
|
|
37
|
-
await this.secondaryDatabase.set(`shared-token-budget`, `${sharedTokenBudget}`);
|
|
38
|
-
}
|
|
39
28
|
const increaseSharedTokenBudget = async (oAmount) => {
|
|
40
29
|
const amount = Math.max(Math.min(oAmount, maxSharedTokenBudget - sharedTokenBudget), 0);
|
|
41
30
|
if (!amount) {
|
|
@@ -47,23 +36,19 @@ class TokenBudgetService {
|
|
|
47
36
|
const setConsumedBudget = async (userId, amount) => {
|
|
48
37
|
this.secondaryDatabase.set(`user-token-tokens-${userId}`, `${amount}`);
|
|
49
38
|
};
|
|
50
|
-
for (const
|
|
51
|
-
const
|
|
52
|
-
const recharge = Math.floor(((+(await this.envCofigService.getEnvByName('USER_TOKEN_BUDGET_PER_HOUR', 0))) * 8 / 24));
|
|
39
|
+
for (const userId of activeUsers) {
|
|
40
|
+
const hourBudget = +(await this.envCofigService.getEnvByName('USER_TOKEN_BUDGET_PER_HOUR', 0));
|
|
53
41
|
const userBudget = await this._getTokenBudget(userId);
|
|
54
|
-
|
|
55
|
-
|
|
42
|
+
this.monitorService.hit('user-token-budget', { userId }, userBudget.budget, 'h');
|
|
43
|
+
if (userBudget.consumed < hourBudget) {
|
|
44
|
+
increaseSharedTokenBudget(hourBudget - userBudget.consumed);
|
|
56
45
|
}
|
|
57
46
|
if (userBudget.consumed) {
|
|
58
|
-
const newConsumed = Math.max(userBudget.consumed -
|
|
47
|
+
const newConsumed = Math.max(userBudget.consumed - hourBudget, 0);
|
|
59
48
|
setConsumedBudget(userId, newConsumed);
|
|
60
|
-
this.monitorService.hit('user-token-budget', {
|
|
61
|
-
userId,
|
|
62
|
-
user: username,
|
|
63
|
-
}, userBudget.budget - newConsumed);
|
|
64
49
|
}
|
|
65
50
|
}
|
|
66
|
-
this.monitorService.hit('shared-token-budget', {}, sharedTokenBudget);
|
|
51
|
+
this.monitorService.hit('shared-token-budget', {}, sharedTokenBudget, 'h');
|
|
67
52
|
};
|
|
68
53
|
const jobExecutor = this.jobExecutorFactory.get('budget-consolidation');
|
|
69
54
|
jobExecutor.process(1, budgetConsolidation);
|
|
@@ -85,7 +70,7 @@ class TokenBudgetService {
|
|
|
85
70
|
};
|
|
86
71
|
};
|
|
87
72
|
const { data: activeUsers } = await userRepository.findBy({
|
|
88
|
-
fields: ['puca_pid'
|
|
73
|
+
fields: ['puca_pid'],
|
|
89
74
|
key: [
|
|
90
75
|
coalesce('active', '$active'),
|
|
91
76
|
coalesce('billing_mode', '$billing_mode'),
|
|
@@ -101,16 +86,16 @@ class TokenBudgetService {
|
|
|
101
86
|
user: null,
|
|
102
87
|
disablePermissionSystem: true,
|
|
103
88
|
});
|
|
104
|
-
return activeUsers;
|
|
89
|
+
return activeUsers.map((user) => user.puca_pid);
|
|
105
90
|
}
|
|
106
91
|
async _getTokenBudget(userId) {
|
|
107
92
|
const userTokenBudget = +(await this.envCofigService.getEnvByName('USER_TOKEN_BUDGET_PER_HOUR', Number.MAX_SAFE_INTEGER));
|
|
108
93
|
if (userTokenBudget === Number.MAX_SAFE_INTEGER) {
|
|
109
94
|
this.logger.debug('No user token budget defined');
|
|
110
95
|
}
|
|
111
|
-
const userConsumedTokens = +(await this.secondaryDatabase.get(`user-token-tokens-${userId}`));
|
|
96
|
+
const userConsumedTokens = +(await this.secondaryDatabase.get(`user-token-tokens-${userId}`)) || 0;
|
|
112
97
|
return {
|
|
113
|
-
budget: userTokenBudget *
|
|
98
|
+
budget: userTokenBudget * 24,
|
|
114
99
|
consumed: userConsumedTokens
|
|
115
100
|
};
|
|
116
101
|
}
|
|
@@ -119,56 +104,37 @@ class TokenBudgetService {
|
|
|
119
104
|
return sharedTokenBudget;
|
|
120
105
|
}
|
|
121
106
|
async getUserTokenBudget(userId) {
|
|
122
|
-
const { budget:
|
|
107
|
+
const { budget: userTokenBudget, consumed: userConsumedBudget } = await this._getTokenBudget(userId);
|
|
123
108
|
const sharedTokenBudget = Math.max(await this._getSharedTokenBudget(), 0);
|
|
124
|
-
const userTokenBudget = oUserTokenBudget + (process.env.ENVIRONMENT.toUpperCase() === 'PRODUCTION' ? 0 : userConsumedBudget);
|
|
125
109
|
return {
|
|
110
|
+
budget: userTokenBudget + sharedTokenBudget,
|
|
126
111
|
consumed: userConsumedBudget,
|
|
127
112
|
userBudget: userTokenBudget,
|
|
113
|
+
userConsumed: userConsumedBudget,
|
|
128
114
|
sharedBudget: sharedTokenBudget,
|
|
129
|
-
totalBudget: userTokenBudget + sharedTokenBudget,
|
|
130
115
|
};
|
|
131
116
|
}
|
|
132
|
-
async decrementUserTokenBudget(userId, inputToken, outputToken
|
|
133
|
-
if (!(opts === null || opts === void 0 ? void 0 : opts.preference)) {
|
|
134
|
-
_.set(opts, 'preference', 'user');
|
|
135
|
-
}
|
|
117
|
+
async decrementUserTokenBudget(userId, inputToken, outputToken) {
|
|
136
118
|
const outputTokenRatio = +(await this.envCofigService.getEnvByName('OUTPUT_TOKEN_RATIO', 1));
|
|
137
119
|
const budget = await this.getUserTokenBudget(userId);
|
|
138
|
-
const userAvailableBudget = budget.userBudget - budget.
|
|
120
|
+
const userAvailableBudget = budget.userBudget - budget.userConsumed;
|
|
139
121
|
const incUserTokenTokens = async (userId, amount) => {
|
|
140
122
|
if (!amount) {
|
|
141
123
|
return;
|
|
142
124
|
}
|
|
143
125
|
this.secondaryDatabase.incBy(`user-token-tokens-${userId}`, Math.ceil(amount));
|
|
144
126
|
};
|
|
145
|
-
const applyToUser = (amount) => {
|
|
146
|
-
incUserTokenTokens(userId, amount);
|
|
147
|
-
};
|
|
148
|
-
const applyToShared = async (amount) => {
|
|
149
|
-
this.secondaryDatabase.incBy(`shared-token-budget`, Math.floor(-amount));
|
|
150
|
-
};
|
|
151
|
-
const [apply1, apply2] = opts.preference === 'shared' ? [applyToShared, applyToUser] : [applyToUser, applyToShared];
|
|
152
|
-
const budget1 = opts.preference === 'shared' ?
|
|
153
|
-
budget.sharedBudget :
|
|
154
|
-
userAvailableBudget;
|
|
155
127
|
const amount = inputToken + (outputToken * outputTokenRatio);
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
const overConsumedBudget = Math.max(amount - budget1, 0);
|
|
128
|
+
incUserTokenTokens(userId, Math.min(amount, userAvailableBudget));
|
|
129
|
+
const overConsumedBudget = Math.max(amount - userAvailableBudget, 0);
|
|
159
130
|
if (!overConsumedBudget) {
|
|
160
|
-
return
|
|
161
|
-
userConsumed: opts.preference === 'user' ? amount : 0,
|
|
162
|
-
sharedBudget: opts.preference === 'shared' ? amount : 0,
|
|
163
|
-
totalConsumed: amount
|
|
164
|
-
};
|
|
131
|
+
return amount;
|
|
165
132
|
}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
userConsumed: opts.preference === 'user' ? consumedBudget1 : overConsumedBudget,
|
|
169
|
-
sharedBudget: opts.preference === 'shared' ? consumedBudget1 : overConsumedBudget,
|
|
170
|
-
totalConsumed: amount
|
|
133
|
+
const descrSharedTokenBudget = async (amount) => {
|
|
134
|
+
this.secondaryDatabase.incBy(`shared-token-budget`, Math.floor(-amount));
|
|
171
135
|
};
|
|
136
|
+
descrSharedTokenBudget(overConsumedBudget);
|
|
137
|
+
return amount;
|
|
172
138
|
}
|
|
173
139
|
}
|
|
174
140
|
exports.TokenBudgetService = TokenBudgetService;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"token-budget.service.js","sourceRoot":"","sources":["../../../../src/core/services/llm/token-budget.service.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"token-budget.service.js","sourceRoot":"","sources":["../../../../src/core/services/llm/token-budget.service.ts"],"names":[],"mappings":";;;AAAA,kDAAwD;AAMxD,sCAAkC;AAIlC,MAAa,kBAAkB;IAG7B,YACmB,iBAAoC,EACpC,iBAAoC,EACpC,eAAiC,EACjC,kBAAsC,EACtC,cAA8B;QAJ9B,sBAAiB,GAAjB,iBAAiB,CAAmB;QACpC,sBAAiB,GAAjB,iBAAiB,CAAmB;QACpC,oBAAe,GAAf,eAAe,CAAkB;QACjC,uBAAkB,GAAlB,kBAAkB,CAAoB;QACtC,mBAAc,GAAd,cAAc,CAAgB;QAPhC,WAAM,GAAG,IAAI,eAAM,CAAC,kBAAkB,CAAC,IAAI,CAAC,CAAA;IAQzD,CAAC;IAEL,KAAK,CAAC,IAAI;QACR,MAAM,MAAM,GAAG,CAAC,MAAM,+BAAmB,CAAC,eAAe,EAAE,CAAC,CAAC,IAAI,CAAA;QACjE,IAAG,MAAM,KAAK,eAAe,EAAE;YAC7B,OAAM;SACP;QACD,MAAM,mBAAmB,GAAG,KAAK,IAAI,EAAE;YACrC,IAAI,iBAAiB,GAAG,MAAM,IAAI,CAAC,qBAAqB,EAAE,CAAA;YAE1D,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC,cAAc,EAAE,CAAA;YAC/C,MAAM,oBAAoB,GAAG,CAAC,CAAC,MAAM,IAAI,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,CACpB,CAAA;QACH,CAAC,CAAA;QAED,sBAAsB,CAAC,kBAAkB,CAAC,CAAA;QAC1C,OAAO,MAAM,CAAA;IACf,CAAC;CAEF;AA3MD,gDA2MC"}
|
|
@@ -19,31 +19,18 @@ export interface ToolDefinitionConfig {
|
|
|
19
19
|
};
|
|
20
20
|
};
|
|
21
21
|
required?: string[];
|
|
22
|
-
oneOf?: {
|
|
23
|
-
required: string[];
|
|
24
|
-
}[];
|
|
25
22
|
additionalProperties?: boolean;
|
|
26
23
|
};
|
|
27
24
|
}
|
|
28
25
|
export interface ToolDefinition<T> {
|
|
29
26
|
config: ToolDefinitionConfig;
|
|
30
27
|
handler: (args: T) => Promise<{
|
|
31
|
-
isError: boolean;
|
|
32
28
|
content: {
|
|
33
29
|
type: 'text';
|
|
34
30
|
text: string;
|
|
35
31
|
}[];
|
|
36
32
|
}>;
|
|
37
33
|
}
|
|
38
|
-
export interface PucaInternalMcpEspecification {
|
|
39
|
-
path: string;
|
|
40
|
-
scope: string;
|
|
41
|
-
availableToAssistent?: boolean;
|
|
42
|
-
assistantProps?: {
|
|
43
|
-
examples: string;
|
|
44
|
-
keywords: string[];
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
34
|
export declare class McpServer {
|
|
48
35
|
private readonly secondaryDataBase;
|
|
49
36
|
private readonly server;
|
|
@@ -66,9 +53,7 @@ export declare class McpServerInstanceGerenciator {
|
|
|
66
53
|
private readonly servers;
|
|
67
54
|
private instances;
|
|
68
55
|
constructor(secondaryDataBase: SecondaryDatabase);
|
|
69
|
-
registerMcpServer(name: string, server: McpServerBuildFunction
|
|
70
|
-
pucaInternalEspecification: PucaInternalMcpEspecification;
|
|
71
|
-
}): Promise<void>;
|
|
56
|
+
registerMcpServer(name: string, server: McpServerBuildFunction): Promise<void>;
|
|
72
57
|
process(name: string, { req, res, user }: {
|
|
73
58
|
req: Request;
|
|
74
59
|
res: Response;
|
|
@@ -27,6 +27,12 @@ class McpServer {
|
|
|
27
27
|
additionalProperties: false
|
|
28
28
|
};
|
|
29
29
|
}
|
|
30
|
+
if (!tool.config.inputSchema.required) {
|
|
31
|
+
tool.config.inputSchema.required = [];
|
|
32
|
+
}
|
|
33
|
+
if (!tool.config.inputSchema.additionalProperties) {
|
|
34
|
+
tool.config.inputSchema.additionalProperties = false;
|
|
35
|
+
}
|
|
30
36
|
});
|
|
31
37
|
this.server.setRequestHandler(types_js_1.ListToolsRequestSchema, async () => ({
|
|
32
38
|
tools: list.map((tool) => tool.config)
|
|
@@ -55,8 +61,8 @@ class McpServer {
|
|
|
55
61
|
await this.secondaryDataBase.get(resource.uri);
|
|
56
62
|
if (!r) {
|
|
57
63
|
r = await (async () => {
|
|
58
|
-
const
|
|
59
|
-
return
|
|
64
|
+
const manual = await axios_1.default.get(resource.uri);
|
|
65
|
+
return manual.data.raw;
|
|
60
66
|
})();
|
|
61
67
|
await this.secondaryDataBase.setex(resource.uri, 60 * 60 * 1, r);
|
|
62
68
|
}
|
|
@@ -92,22 +98,8 @@ class McpServerInstanceGerenciator {
|
|
|
92
98
|
});
|
|
93
99
|
}, 1000 * 60);
|
|
94
100
|
}
|
|
95
|
-
async registerMcpServer(name, server
|
|
101
|
+
async registerMcpServer(name, server) {
|
|
96
102
|
this.servers[name] = server;
|
|
97
|
-
const internalEspecification = opts.pucaInternalEspecification;
|
|
98
|
-
if (!internalEspecification) {
|
|
99
|
-
return;
|
|
100
|
-
}
|
|
101
|
-
if (!internalEspecification.path) {
|
|
102
|
-
throw new Error('pucaInternalEspecification.path is required');
|
|
103
|
-
}
|
|
104
|
-
if (!internalEspecification.scope) {
|
|
105
|
-
throw new Error('pucaInternalEspecification.scope is required');
|
|
106
|
-
}
|
|
107
|
-
if (!internalEspecification.availableToAssistent) {
|
|
108
|
-
internalEspecification.availableToAssistent = false;
|
|
109
|
-
}
|
|
110
|
-
await this.secondaryDataBase.set(`puca-mcp-server:${name}:config`, JSON.stringify(internalEspecification));
|
|
111
103
|
}
|
|
112
104
|
async process(name, { req, res, user }) {
|
|
113
105
|
const sessionId = req.headers['mcp-session-id'] || user.puca_pid;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mcp-server.service.js","sourceRoot":"","sources":["../../../../src/core/services/mcp/mcp-server.service.ts"],"names":[],"mappings":";;;AACA,wEAAgF;AAChF,0FAAkG;AAClG,iCAAyB;AACzB,iEAK2C;
|
|
1
|
+
{"version":3,"file":"mcp-server.service.js","sourceRoot":"","sources":["../../../../src/core/services/mcp/mcp-server.service.ts"],"names":[],"mappings":";;;AACA,wEAAgF;AAChF,0FAAkG;AAClG,iCAAyB;AACzB,iEAK2C;AAqC3C,MAAa,SAAS;IAGpB,YACmB,iBAAoC,EACrD,IAGC;QAJgB,sBAAiB,GAAjB,iBAAiB,CAAmB;QAKrD,IAAI,CAAC,MAAM,GAAG,IAAI,iBAAU,CAAC,IAAI,EAAE;YACjC,YAAY,EAAE;gBACZ,KAAK,EAAE,EAAE;gBACT,SAAS,EAAE,EAAE;aACd;SACF,CAAC,CAAA;QAEF,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,CAAA;QACd,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC,CAAA;IACpB,CAAC;IAED,KAAK,CACH,IAA2B;QAG3B,IAAI,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE;YACpB,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,WAAW,EAAE;gBAC5B,IAAI,CAAC,MAAM,CAAC,WAAW,GAAG;oBACxB,IAAI,EAAE,QAAQ;oBACd,UAAU,EAAE,EAAE;oBACd,QAAQ,EAAE,EAAE;oBACZ,oBAAoB,EAAE,KAAK;iBAC5B,CAAA;aACF;YAED,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,QAAQ,EAAE;gBACrC,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,QAAQ,GAAG,EAAE,CAAA;aACtC;YAED,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,WAAW,CA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|
|
2
|
+
import { EnvConfigService } from '../../essential/envs-config.service';
|
|
2
3
|
import { ConfigService } from '../../essential/config.service';
|
|
3
4
|
export declare class MonitorService {
|
|
4
5
|
private readonly moduleConfigService;
|
|
6
|
+
private readonly envConfigService;
|
|
5
7
|
private readonly configService;
|
|
6
8
|
private readonly logger;
|
|
7
9
|
private static readonly URL;
|
|
@@ -9,11 +11,8 @@ export declare class MonitorService {
|
|
|
9
11
|
private static readonly ORG;
|
|
10
12
|
private static readonly BUCKET;
|
|
11
13
|
private readonly batch;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
init(): Promise<void>;
|
|
15
|
-
destroy(): Promise<void>;
|
|
16
|
-
hit(measurement: string, tags: any, value: number, precision?: "s" | "ms" | "us" | "ns"): Promise<void>;
|
|
14
|
+
constructor(moduleConfigService: ModuleConfigService, envConfigService: EnvConfigService, configService: ConfigService);
|
|
15
|
+
hit(measurement: string, tags: any, value: number, precision: "d" | "h" | "m" | "s" | "ms" | "us" | "ns"): Promise<void>;
|
|
17
16
|
private computeData;
|
|
18
17
|
private flush;
|
|
19
18
|
}
|
|
@@ -5,21 +5,17 @@ const _ = require("lodash");
|
|
|
5
5
|
const axios_1 = require("axios");
|
|
6
6
|
const logger_1 = require("./logger");
|
|
7
7
|
class MonitorService {
|
|
8
|
-
constructor(moduleConfigService, configService) {
|
|
8
|
+
constructor(moduleConfigService, envConfigService, configService) {
|
|
9
9
|
this.moduleConfigService = moduleConfigService;
|
|
10
|
+
this.envConfigService = envConfigService;
|
|
10
11
|
this.configService = configService;
|
|
11
12
|
this.logger = new logger_1.Logger('MonitorService');
|
|
12
13
|
this.batch = [];
|
|
13
|
-
|
|
14
|
-
async init() {
|
|
15
|
-
this.interval = setInterval(() => {
|
|
14
|
+
setInterval(() => {
|
|
16
15
|
this.flush();
|
|
17
16
|
}, 60000);
|
|
18
17
|
}
|
|
19
|
-
async
|
|
20
|
-
clearInterval(this.interval);
|
|
21
|
-
}
|
|
22
|
-
async hit(measurement, tags, value, precision = 's') {
|
|
18
|
+
async hit(measurement, tags, value, precision) {
|
|
23
19
|
this.batch.push({
|
|
24
20
|
precision,
|
|
25
21
|
measurement,
|
|
@@ -40,11 +36,7 @@ class MonitorService {
|
|
|
40
36
|
data.push(['version', version]);
|
|
41
37
|
data.push(['domain', secret.namespace]);
|
|
42
38
|
data.push(['env', env]);
|
|
43
|
-
const
|
|
44
|
-
const valueStr = `${value}`;
|
|
45
|
-
return valueStr.replace(/[^a-zA-Z0-9]/g, '').replace(/ /g, '_');
|
|
46
|
-
};
|
|
47
|
-
const dataStr = data.map(([key, value]) => `${_.snakeCase(key)}=${normalize(value)}`).join(',');
|
|
39
|
+
const dataStr = data.map(([key, value]) => `${_.snakeCase(key)}=${value}`).join(',');
|
|
48
40
|
result.push(`${_.snakeCase(point.measurement)},${dataStr} value=${point.value}`);
|
|
49
41
|
}
|
|
50
42
|
return result.join('\n');
|
|
@@ -65,7 +57,6 @@ class MonitorService {
|
|
|
65
57
|
timeout: 30000
|
|
66
58
|
}).catch(e => {
|
|
67
59
|
var _a;
|
|
68
|
-
debugger;
|
|
69
60
|
this.logger.error('Error sending data to monitor', ((_a = e.response) === null || _a === void 0 ? void 0 : _a.data) || e.message, points.length);
|
|
70
61
|
});
|
|
71
62
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"monitor.js","sourceRoot":"","sources":["../../../src/core/services/monitor.ts"],"names":[],"mappings":";;;AAAA,4BAA2B;AAC3B,iCAAyB;AAGzB,qCAAiC;
|
|
1
|
+
{"version":3,"file":"monitor.js","sourceRoot":"","sources":["../../../src/core/services/monitor.ts"],"names":[],"mappings":";;;AAAA,4BAA2B;AAC3B,iCAAyB;AAGzB,qCAAiC;AAYjC,MAAa,cAAc;IAUzB,YACmB,mBAAwC,EACxC,gBAAkC,EAClC,aAA4B;QAF5B,wBAAmB,GAAnB,mBAAmB,CAAqB;QACxC,qBAAgB,GAAhB,gBAAgB,CAAkB;QAClC,kBAAa,G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C,CAAC,QAAQ,0CAAE,IAAI,KAAI,CAAC,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,CAAC,CAAA;gBAElG,CAAC,CAAC,CAAA;aACL;SACF;QAAC,OAAO,CAAC,EAAE;YACV,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,+BAA+B,EAAE,CAAA,MAAA,CAAC,CAAC,QAAQ,0CAAE,IAAI,KAAI,CAAC,CAAC,OAAO,CAAC,CAAA;SAElF;IACH,CAAC;;AAhFH,wCAiFC;AA9EyB,kBAAG,GAAG,0BAA0B,CAAA;AAChC,oBAAK,GAAG,0FAA0F,CAAA;AAClG,kBAAG,GAAG,MAAM,CAAA;AACZ,qBAAM,GAAG,SAAS,CAAA"}
|
|
@@ -10,11 +10,19 @@ export interface PublishOptions extends Options.Publish {
|
|
|
10
10
|
export declare class MqService {
|
|
11
11
|
private readonly domain;
|
|
12
12
|
private readonly configService;
|
|
13
|
+
private closing;
|
|
14
|
+
private static readonly DEAD_LETTER_QUEUE;
|
|
15
|
+
private static readonly RETRY_EXCHANGE;
|
|
13
16
|
private readonly logger;
|
|
14
|
-
private
|
|
17
|
+
private mainChannel;
|
|
18
|
+
private readonly queues;
|
|
19
|
+
private connection;
|
|
20
|
+
private additionalChannels;
|
|
15
21
|
constructor(domain: string, configService: ConfigService);
|
|
22
|
+
private connect;
|
|
16
23
|
init(): Promise<void>;
|
|
17
24
|
shutdown(): Promise<void>;
|
|
25
|
+
private computeExpBackoff;
|
|
18
26
|
assertQueue(queue: string, options?: Options.AssertQueue): Promise<import("amqplib").Replies.AssertQueue>;
|
|
19
27
|
assertExchange(exchange: string, type?: string, options?: Options.AssertExchange): Promise<import("amqplib").Replies.AssertExchange>;
|
|
20
28
|
bindQueue(queue: string, exchange: string, pattern: string, args?: any): Promise<import("amqplib").Replies.Empty>;
|
|
@@ -4,14 +4,12 @@ exports.MqService = void 0;
|
|
|
4
4
|
const _ = require("lodash");
|
|
5
5
|
const amqplib_1 = require("amqplib");
|
|
6
6
|
const logger_1 = require("../logger");
|
|
7
|
-
|
|
8
|
-
class MqServiceInstance {
|
|
7
|
+
class MqService {
|
|
9
8
|
constructor(domain, configService) {
|
|
10
9
|
this.domain = domain;
|
|
11
10
|
this.configService = configService;
|
|
12
11
|
this.closing = false;
|
|
13
|
-
this.logger = new logger_1.Logger(
|
|
14
|
-
this.eventEmmiter = new events_1.EventEmitter();
|
|
12
|
+
this.logger = new logger_1.Logger('MqService');
|
|
15
13
|
this.queues = {};
|
|
16
14
|
this.additionalChannels = [];
|
|
17
15
|
}
|
|
@@ -42,16 +40,17 @@ class MqServiceInstance {
|
|
|
42
40
|
if (this.closing) {
|
|
43
41
|
return;
|
|
44
42
|
}
|
|
45
|
-
this.
|
|
43
|
+
this.logger.warn('Connection to RabbitMQ closed, reconnecting...');
|
|
44
|
+
throw new Error('Connection to RabbitMQ closed');
|
|
46
45
|
});
|
|
47
46
|
this.connection.on('error', (error) => {
|
|
48
47
|
this.logger.error('Connection to RabbitMQ error', error);
|
|
49
48
|
});
|
|
50
|
-
const mainChannel = await this.createChannel(
|
|
51
|
-
await mainChannel.assertQueue(
|
|
49
|
+
const mainChannel = await this.connection.createChannel();
|
|
50
|
+
await mainChannel.assertQueue(MqService.DEAD_LETTER_QUEUE, {
|
|
52
51
|
durable: true
|
|
53
52
|
});
|
|
54
|
-
await mainChannel.assertExchange(
|
|
53
|
+
await mainChannel.assertExchange(MqService.RETRY_EXCHANGE, 'x-delayed-message', {
|
|
55
54
|
durable: true,
|
|
56
55
|
arguments: {
|
|
57
56
|
'x-delayed-type': 'direct'
|
|
@@ -59,30 +58,13 @@ class MqServiceInstance {
|
|
|
59
58
|
});
|
|
60
59
|
this.mainChannel = mainChannel;
|
|
61
60
|
}
|
|
62
|
-
async createChannel(name) {
|
|
63
|
-
const channel = await this.connection.createChannel();
|
|
64
|
-
channel.on('error', (error) => {
|
|
65
|
-
this.logger.error('Main channel error', error);
|
|
66
|
-
});
|
|
67
|
-
channel.on('close', () => {
|
|
68
|
-
this.eventEmmiter.emit('channelClosed', name);
|
|
69
|
-
});
|
|
70
|
-
return channel;
|
|
71
|
-
}
|
|
72
61
|
async init() {
|
|
73
62
|
await this.connect();
|
|
74
63
|
}
|
|
75
64
|
async shutdown() {
|
|
76
65
|
this.closing = true;
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
}
|
|
80
|
-
const connection = this.connection;
|
|
81
|
-
this.connection = null;
|
|
82
|
-
this.eventEmmiter.removeAllListeners('connectionClosed');
|
|
83
|
-
this.eventEmmiter.removeAllListeners('channelClosed');
|
|
84
|
-
connection.removeAllListeners('close');
|
|
85
|
-
connection.removeAllListeners('error');
|
|
66
|
+
this.connection.removeAllListeners('close');
|
|
67
|
+
this.connection.removeAllListeners('error');
|
|
86
68
|
await this.mainChannel.close();
|
|
87
69
|
for (const { channel } of Object.values(this.queues)) {
|
|
88
70
|
await channel.close();
|
|
@@ -90,7 +72,7 @@ class MqServiceInstance {
|
|
|
90
72
|
for (const channel of this.additionalChannels) {
|
|
91
73
|
await channel.close();
|
|
92
74
|
}
|
|
93
|
-
await connection.close();
|
|
75
|
+
await this.connection.close();
|
|
94
76
|
}
|
|
95
77
|
computeExpBackoff(delay, retryCount) {
|
|
96
78
|
const base = 2;
|
|
@@ -100,7 +82,7 @@ class MqServiceInstance {
|
|
|
100
82
|
async assertQueue(queue, options) {
|
|
101
83
|
const mainChannel = this.mainChannel;
|
|
102
84
|
const queueCreated = await mainChannel.assertQueue(queue, Object.assign({ durable: true }, options));
|
|
103
|
-
await mainChannel.bindQueue(queue,
|
|
85
|
+
await mainChannel.bindQueue(queue, MqService.RETRY_EXCHANGE, queue);
|
|
104
86
|
return queueCreated;
|
|
105
87
|
}
|
|
106
88
|
async assertExchange(exchange, type = 'fanout', options) {
|
|
@@ -112,6 +94,10 @@ class MqServiceInstance {
|
|
|
112
94
|
return await mainChannel.bindQueue(queue, exchange, pattern, args);
|
|
113
95
|
}
|
|
114
96
|
async publish(exchange, routingKey, content, options) {
|
|
97
|
+
if (!this.connection) {
|
|
98
|
+
this.logger.warn('channel not initialized');
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
115
101
|
const mainChannel = this.mainChannel;
|
|
116
102
|
return await mainChannel.publish(exchange, routingKey, Buffer.from(JSON.stringify(content)), Object.assign(Object.assign({}, _.omit(options, 'disableRetry')), { headers: Object.assign(Object.assign({}, options === null || options === void 0 ? void 0 : options.headers), { 'max-retry-count': (options === null || options === void 0 ? void 0 : options.disableRetry) ? 0 : 15 }) }));
|
|
117
103
|
}
|
|
@@ -120,7 +106,7 @@ class MqServiceInstance {
|
|
|
120
106
|
return await mainChannel.sendToQueue(queue, Buffer.from(JSON.stringify(content)), Object.assign(Object.assign({}, _.omit(options, 'disableRetry')), { headers: Object.assign(Object.assign({}, options === null || options === void 0 ? void 0 : options.headers), { 'max-retry-count': (options === null || options === void 0 ? void 0 : options.disableRetry) ? 0 : 15 }) }));
|
|
121
107
|
}
|
|
122
108
|
async consume(queue, onMessage, options) {
|
|
123
|
-
const channel = ((options === null || options === void 0 ? void 0 : options.maxSimultaneousMessages) ? await this.createChannel(
|
|
109
|
+
const channel = ((options === null || options === void 0 ? void 0 : options.maxSimultaneousMessages) ? await this.connection.createChannel() : this.mainChannel);
|
|
124
110
|
if (options === null || options === void 0 ? void 0 : options.maxSimultaneousMessages) {
|
|
125
111
|
await channel.prefetch(options.maxSimultaneousMessages);
|
|
126
112
|
this.additionalChannels.push(channel);
|
|
@@ -164,7 +150,7 @@ class MqServiceInstance {
|
|
|
164
150
|
'max-retry-count': maxRetryCount,
|
|
165
151
|
'first-try': firstTry
|
|
166
152
|
};
|
|
167
|
-
channel.publish(
|
|
153
|
+
channel.publish(MqService.RETRY_EXCHANGE, queue, msg.content, { headers });
|
|
168
154
|
channel.ack(msg);
|
|
169
155
|
}
|
|
170
156
|
else if (retryCount >= maxRetryCount) {
|
|
@@ -176,7 +162,7 @@ class MqServiceInstance {
|
|
|
176
162
|
max_retry_count: maxRetryCount,
|
|
177
163
|
first_try: firstTry,
|
|
178
164
|
};
|
|
179
|
-
channel.sendToQueue(
|
|
165
|
+
channel.sendToQueue(MqService.DEAD_LETTER_QUEUE, Buffer.from(JSON.stringify(deadContent)));
|
|
180
166
|
channel.ack(msg);
|
|
181
167
|
this.logger.error('Mensagem descartada', msg.content.toString(), error);
|
|
182
168
|
}
|
|
@@ -203,73 +189,8 @@ class MqServiceInstance {
|
|
|
203
189
|
});
|
|
204
190
|
await mainChannel.sendToQueue(queue, Buffer.from(JSON.stringify(args)));
|
|
205
191
|
}
|
|
206
|
-
onDisconnect(callback) {
|
|
207
|
-
this.eventEmmiter.once('connectionClosed', () => {
|
|
208
|
-
if (this.closing)
|
|
209
|
-
return;
|
|
210
|
-
callback();
|
|
211
|
-
});
|
|
212
|
-
this.eventEmmiter.once('channelClosed', (name) => {
|
|
213
|
-
if (this.closing)
|
|
214
|
-
return;
|
|
215
|
-
callback();
|
|
216
|
-
});
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
MqServiceInstance.DEAD_LETTER_QUEUE = 'general-dead-letter-queue';
|
|
220
|
-
MqServiceInstance.RETRY_EXCHANGE = 'general-retry-exchange';
|
|
221
|
-
class MqService {
|
|
222
|
-
constructor(domain, configService) {
|
|
223
|
-
this.domain = domain;
|
|
224
|
-
this.configService = configService;
|
|
225
|
-
this.logger = new logger_1.Logger(MqService.name);
|
|
226
|
-
}
|
|
227
|
-
async init() {
|
|
228
|
-
const connect = async () => {
|
|
229
|
-
const instance = new MqServiceInstance(this.domain, this.configService);
|
|
230
|
-
instance.onDisconnect(async () => {
|
|
231
|
-
this.logger.warn('RabbitMQ connection lost, reinitializing instance...');
|
|
232
|
-
return await connect()
|
|
233
|
-
.then(async () => {
|
|
234
|
-
instance.shutdown()
|
|
235
|
-
.catch((error) => {
|
|
236
|
-
});
|
|
237
|
-
})
|
|
238
|
-
.catch(async (error) => {
|
|
239
|
-
this.logger.error('Failed to reconnect to RabbitMQ', error);
|
|
240
|
-
this.logger.error('Critical error in RabbitMQ service, shutting down the process...');
|
|
241
|
-
process.exit(1);
|
|
242
|
-
});
|
|
243
|
-
});
|
|
244
|
-
await instance.init();
|
|
245
|
-
this.instance = instance;
|
|
246
|
-
};
|
|
247
|
-
await connect();
|
|
248
|
-
}
|
|
249
|
-
async shutdown() {
|
|
250
|
-
await this.instance.shutdown();
|
|
251
|
-
}
|
|
252
|
-
async assertQueue(queue, options) {
|
|
253
|
-
return await this.instance.assertQueue(queue, options);
|
|
254
|
-
}
|
|
255
|
-
async assertExchange(exchange, type = 'fanout', options) {
|
|
256
|
-
return await this.instance.assertExchange(exchange, type, options);
|
|
257
|
-
}
|
|
258
|
-
async bindQueue(queue, exchange, pattern, args) {
|
|
259
|
-
return await this.instance.bindQueue(queue, exchange, pattern, args);
|
|
260
|
-
}
|
|
261
|
-
async publish(exchange, routingKey, content, options) {
|
|
262
|
-
return await this.instance.publish(exchange, routingKey, content, options);
|
|
263
|
-
}
|
|
264
|
-
async sendToQueue(queue, content, options) {
|
|
265
|
-
return await this.instance.sendToQueue(queue, content, options);
|
|
266
|
-
}
|
|
267
|
-
async consume(queue, onMessage, options) {
|
|
268
|
-
return await this.instance.consume(queue, onMessage, options);
|
|
269
|
-
}
|
|
270
|
-
async ensure(funcImpl, args = null) {
|
|
271
|
-
return await this.instance.ensure(funcImpl, args);
|
|
272
|
-
}
|
|
273
192
|
}
|
|
274
193
|
exports.MqService = MqService;
|
|
194
|
+
MqService.DEAD_LETTER_QUEUE = 'general-dead-letter-queue';
|
|
195
|
+
MqService.RETRY_EXCHANGE = 'general-retry-exchange';
|
|
275
196
|
//# sourceMappingURL=mq.service.js.map
|