@yolo-croket-dev/core 1.9.12 → 1.9.13-beta.1
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/CHANGELOG.md +11 -0
- package/filters/grpc-exception.filter.js +2 -2
- package/functions/amqp-handler.function.js +13 -6
- package/interceptors/logging-service.interceptor.js +2 -2
- package/interceptors/rpc.interceptor.js +2 -2
- package/interfaces/exception.js +3 -3
- package/interfaces/log-level.enum.js +1 -1
- package/interfaces/msa-exception.d.ts +1 -1
- package/interfaces/msa-exception.js +1 -1
- package/modules/graceful-shutdown/graceful-shutdown.module.js +2 -2
- package/modules/graceful-shutdown/graceful-shutdown.rmq.d.ts +10 -4
- package/modules/graceful-shutdown/graceful-shutdown.rmq.js +51 -2
- package/modules/graceful-shutdown/graceful-shutdown.service.js +2 -2
- package/modules/redis.module.js +2 -2
- package/package.json +2 -1
- package/pipes/rpc-validation.pipe.js +2 -2
- package/tests/amqp-handler-timeout.test.cjs +47 -0
- package/tests/graceful-shutdown-rmq.test.cjs +102 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 1.9.13-beta.1
|
|
4
|
+
|
|
5
|
+
- AMQP 요청의 `param.timeout`을 최대 29분 범위에서 적용해 timeout 시 클라이언트 Observable 구독을 해제합니다.
|
|
6
|
+
|
|
7
|
+
## 1.9.12-beta.1
|
|
8
|
+
|
|
9
|
+
- `GracefulServerRMQ`가 RabbitMQ 연결에 queue, environment, owner, hostname, process ID 기반 `connection_name`을 자동으로 추가합니다.
|
|
10
|
+
- 호출자가 지정한 `connectionOptions`, `clientProperties`, 유효한 문자열 `connection_name`은 그대로 보존합니다.
|
|
11
|
+
- 문자열이 아닌 `connection_name`은 런타임 오류 없이 자동 생성값으로 대체합니다.
|
|
@@ -130,7 +130,7 @@ let GrpcExceptionFilter = class GrpcExceptionFilter extends microservices_1.Base
|
|
|
130
130
|
}
|
|
131
131
|
}
|
|
132
132
|
};
|
|
133
|
-
GrpcExceptionFilter =
|
|
133
|
+
exports.GrpcExceptionFilter = GrpcExceptionFilter;
|
|
134
|
+
exports.GrpcExceptionFilter = GrpcExceptionFilter = __decorate([
|
|
134
135
|
(0, common_1.Catch)()
|
|
135
136
|
], GrpcExceptionFilter);
|
|
136
|
-
exports.GrpcExceptionFilter = GrpcExceptionFilter;
|
|
@@ -17,11 +17,18 @@ class HttpExceptionCustom extends common_1.HttpException {
|
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
19
|
exports.HttpExceptionCustom = HttpExceptionCustom;
|
|
20
|
+
function resolveAmqpTimeout(timeoutMs) {
|
|
21
|
+
if (typeof timeoutMs !== 'number' || !Number.isFinite(timeoutMs) || timeoutMs <= 0) {
|
|
22
|
+
return DEFAULT_TIMEOUT;
|
|
23
|
+
}
|
|
24
|
+
return Math.min(timeoutMs, DEFAULT_TIMEOUT);
|
|
25
|
+
}
|
|
20
26
|
async function AmqpHandler(client, messagePattern, payload) {
|
|
21
|
-
var _a, _b, _c, _d;
|
|
27
|
+
var _a, _b, _c, _d, _e;
|
|
22
28
|
let data;
|
|
29
|
+
const timeoutMs = resolveAmqpTimeout((_a = payload === null || payload === void 0 ? void 0 : payload.param) === null || _a === void 0 ? void 0 : _a.timeout);
|
|
23
30
|
try {
|
|
24
|
-
data = await (0, rxjs_1.lastValueFrom)(client.send(messagePattern, payload).pipe((0, operators_1.timeout)(
|
|
31
|
+
data = await (0, rxjs_1.lastValueFrom)(client.send(messagePattern, payload).pipe((0, operators_1.timeout)(timeoutMs)));
|
|
25
32
|
if (data.isError) {
|
|
26
33
|
const status = data.status || 500;
|
|
27
34
|
const subMessage = data.subMessage;
|
|
@@ -29,7 +36,7 @@ async function AmqpHandler(client, messagePattern, payload) {
|
|
|
29
36
|
* 파일 객체 처리
|
|
30
37
|
* 다른 서비스에서 이미 처리가 될 수 있음
|
|
31
38
|
* */
|
|
32
|
-
(0, _1.reWriteFileValue)((
|
|
39
|
+
(0, _1.reWriteFileValue)((_b = data === null || data === void 0 ? void 0 : data.arg) === null || _b === void 0 ? void 0 : _b.file);
|
|
33
40
|
const errInfo = data.errInfo ? data.errInfo : {
|
|
34
41
|
message: data.errMessage,
|
|
35
42
|
type: data.errType,
|
|
@@ -50,7 +57,7 @@ async function AmqpHandler(client, messagePattern, payload) {
|
|
|
50
57
|
catch (err) {
|
|
51
58
|
if (typeof err === 'string' && err.includes('There is no matching message handler defined in the remote service.')) {
|
|
52
59
|
msaLogger.error('NOT_EXISTS_MESSAGE_PATTERN', {
|
|
53
|
-
traceId: ((
|
|
60
|
+
traceId: ((_c = payload === null || payload === void 0 ? void 0 : payload.param) === null || _c === void 0 ? void 0 : _c.traceId) || '-',
|
|
54
61
|
errInfo: {
|
|
55
62
|
errType: 'NOT_EXISTS_MESSAGE_PATTERN',
|
|
56
63
|
message: `${messagePattern} 해당 메시지 패턴은 존재하지 않습니다.`,
|
|
@@ -64,7 +71,7 @@ async function AmqpHandler(client, messagePattern, payload) {
|
|
|
64
71
|
}
|
|
65
72
|
else if (err instanceof rxjs_1.TimeoutError || err.message === DEFAULT_TIMEOUT_MESSAGE) {
|
|
66
73
|
msaLogger.error('TIMEOUT_ERROR', {
|
|
67
|
-
traceId: ((
|
|
74
|
+
traceId: ((_d = payload === null || payload === void 0 ? void 0 : payload.param) === null || _d === void 0 ? void 0 : _d.traceId) || '-',
|
|
68
75
|
errInfo: {
|
|
69
76
|
errType: 'TIMEOUT_ERROR',
|
|
70
77
|
message: DEFAULT_TIMEOUT_MESSAGE,
|
|
@@ -77,7 +84,7 @@ async function AmqpHandler(client, messagePattern, payload) {
|
|
|
77
84
|
});
|
|
78
85
|
}
|
|
79
86
|
else {
|
|
80
|
-
msaLogger.warn('AMQP_HANDLER_ERROR', { traceId: ((
|
|
87
|
+
msaLogger.warn('AMQP_HANDLER_ERROR', { traceId: ((_e = payload === null || payload === void 0 ? void 0 : payload.param) === null || _e === void 0 ? void 0 : _e.traceId) || '-' }, { err });
|
|
81
88
|
throw err;
|
|
82
89
|
}
|
|
83
90
|
}
|
|
@@ -219,7 +219,7 @@ let LoggingServiceInterceptor = class LoggingServiceInterceptor {
|
|
|
219
219
|
}));
|
|
220
220
|
}
|
|
221
221
|
};
|
|
222
|
-
LoggingServiceInterceptor =
|
|
222
|
+
exports.LoggingServiceInterceptor = LoggingServiceInterceptor;
|
|
223
|
+
exports.LoggingServiceInterceptor = LoggingServiceInterceptor = __decorate([
|
|
223
224
|
(0, common_1.Injectable)()
|
|
224
225
|
], LoggingServiceInterceptor);
|
|
225
|
-
exports.LoggingServiceInterceptor = LoggingServiceInterceptor;
|
|
@@ -370,9 +370,9 @@ let RPCInterceptor = class RPCInterceptor {
|
|
|
370
370
|
})));
|
|
371
371
|
}
|
|
372
372
|
};
|
|
373
|
-
RPCInterceptor =
|
|
373
|
+
exports.RPCInterceptor = RPCInterceptor;
|
|
374
|
+
exports.RPCInterceptor = RPCInterceptor = __decorate([
|
|
374
375
|
(0, common_1.Injectable)(),
|
|
375
376
|
__metadata("design:paramtypes", [core_1.Reflector,
|
|
376
377
|
graceful_shutdown_service_1.GracefulShutdownService])
|
|
377
378
|
], RPCInterceptor);
|
|
378
|
-
exports.RPCInterceptor = RPCInterceptor;
|
package/interfaces/exception.js
CHANGED
|
@@ -18,9 +18,10 @@ var ErrActionTypeEnum;
|
|
|
18
18
|
ErrActionTypeEnum["LEGACY_SHOW_TOAST"] = "LEGACY_SHOW_TOAST";
|
|
19
19
|
ErrActionTypeEnum["FORCE_UPDATE"] = "FORCE_UPDATE";
|
|
20
20
|
ErrActionTypeEnum["UPDATE_ACCESS_TOKEN"] = "UPDATE_ACCESS_TOKEN";
|
|
21
|
-
})(ErrActionTypeEnum
|
|
21
|
+
})(ErrActionTypeEnum || (exports.ErrActionTypeEnum = ErrActionTypeEnum = {}));
|
|
22
22
|
class ErrAction {
|
|
23
23
|
}
|
|
24
|
+
exports.ErrAction = ErrAction;
|
|
24
25
|
__decorate([
|
|
25
26
|
(0, swagger_1.ApiProperty)({
|
|
26
27
|
description: '액션 타입',
|
|
@@ -57,9 +58,9 @@ __decorate([
|
|
|
57
58
|
}),
|
|
58
59
|
__metadata("design:type", String)
|
|
59
60
|
], ErrAction.prototype, "webViewUrl", void 0);
|
|
60
|
-
exports.ErrAction = ErrAction;
|
|
61
61
|
class ErrInfo {
|
|
62
62
|
}
|
|
63
|
+
exports.ErrInfo = ErrInfo;
|
|
63
64
|
__decorate([
|
|
64
65
|
(0, swagger_1.ApiProperty)({
|
|
65
66
|
description: '에러 종류 식별자',
|
|
@@ -88,4 +89,3 @@ __decorate([
|
|
|
88
89
|
}),
|
|
89
90
|
__metadata("design:type", Object)
|
|
90
91
|
], ErrInfo.prototype, "errOption", void 0);
|
|
91
|
-
exports.ErrInfo = ErrInfo;
|
|
@@ -37,7 +37,7 @@ export interface MsaException {
|
|
|
37
37
|
errOption?: MsaExceptionOption;
|
|
38
38
|
}
|
|
39
39
|
/** HttpException과 MsaException을 결합한 타입 */
|
|
40
|
-
export
|
|
40
|
+
export type MsaHttpException = MsaException & HttpException;
|
|
41
41
|
/**
|
|
42
42
|
* @description MsaException 타입 가드
|
|
43
43
|
* @param error 검사할 에러 객체
|
|
@@ -6,7 +6,7 @@ var MsaExceptionMessageAckBehaviorEnum;
|
|
|
6
6
|
(function (MsaExceptionMessageAckBehaviorEnum) {
|
|
7
7
|
MsaExceptionMessageAckBehaviorEnum["ACK"] = "ACK";
|
|
8
8
|
MsaExceptionMessageAckBehaviorEnum["NACK"] = "NACK";
|
|
9
|
-
})(MsaExceptionMessageAckBehaviorEnum
|
|
9
|
+
})(MsaExceptionMessageAckBehaviorEnum || (exports.MsaExceptionMessageAckBehaviorEnum = MsaExceptionMessageAckBehaviorEnum = {}));
|
|
10
10
|
/**
|
|
11
11
|
* @description MsaException 타입 가드
|
|
12
12
|
* @param error 검사할 에러 객체
|
|
@@ -48,7 +48,8 @@ let GracefulShutdownModule = class GracefulShutdownModule {
|
|
|
48
48
|
msaLogger.info('GracefulShutdownService.onModuleDestroy: all interceptors finished, shutting down', { traceId: 'ON_MODULE_DESTROY' });
|
|
49
49
|
}
|
|
50
50
|
};
|
|
51
|
-
GracefulShutdownModule =
|
|
51
|
+
exports.GracefulShutdownModule = GracefulShutdownModule;
|
|
52
|
+
exports.GracefulShutdownModule = GracefulShutdownModule = __decorate([
|
|
52
53
|
(0, common_1.Global)(),
|
|
53
54
|
(0, common_1.Module)({
|
|
54
55
|
providers: [graceful_shutdown_service_1.GracefulShutdownService],
|
|
@@ -56,4 +57,3 @@ GracefulShutdownModule = __decorate([
|
|
|
56
57
|
}),
|
|
57
58
|
__metadata("design:paramtypes", [graceful_shutdown_service_1.GracefulShutdownService])
|
|
58
59
|
], GracefulShutdownModule);
|
|
59
|
-
exports.GracefulShutdownModule = GracefulShutdownModule;
|
|
@@ -1,10 +1,16 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
1
|
+
import { ServerRMQ } from '@nestjs/microservices';
|
|
2
|
+
import type { RmqOptions } from '@nestjs/microservices';
|
|
3
|
+
import type { Channel } from 'amqp-connection-manager';
|
|
4
|
+
import type { ConsumeMessage } from 'amqplib';
|
|
5
|
+
export type GracefulRmqOptions = RmqOptions['options'] & {
|
|
5
6
|
waitingEndingHandlersTimeoutMs?: number;
|
|
6
7
|
waitingEndingHandlersIntervalMs?: number;
|
|
7
8
|
};
|
|
9
|
+
/**
|
|
10
|
+
* @author jun
|
|
11
|
+
* @description RabbitMQ 연결 식별 정보와 graceful shutdown을 제공하는 RMQ 서버
|
|
12
|
+
* @process 연결 옵션에 추적 가능한 connection_name을 추가한 뒤 메시지 종료를 보장한다.
|
|
13
|
+
*/
|
|
8
14
|
export declare class GracefulServerRMQ extends ServerRMQ {
|
|
9
15
|
protected readonly options: GracefulRmqOptions;
|
|
10
16
|
protected runningMessages: number;
|
|
@@ -1,18 +1,67 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
6
|
exports.GracefulServerRMQ = void 0;
|
|
4
7
|
const microservices_1 = require("@nestjs/microservices");
|
|
8
|
+
const os_1 = __importDefault(require("os"));
|
|
5
9
|
const node_util_1 = require("node:util");
|
|
6
10
|
const sleep = (0, node_util_1.promisify)(setTimeout);
|
|
7
11
|
const RMQ_DEFAULT_WAITING_ENDING_HANDLERS_TIMEOUT_MS = 5000;
|
|
8
12
|
const RMQ_DEFAULT_WAITING_ENDING_HANDLERS_INTERVAL_MS = 500;
|
|
13
|
+
const RMQ_UNKNOWN_CONNECTION_IDENTITY = 'unknown';
|
|
14
|
+
function createRmqConnectionName(queue) {
|
|
15
|
+
const environment = process.env.ENV || process.env.NODE_ENV || RMQ_UNKNOWN_CONNECTION_IDENTITY;
|
|
16
|
+
const owner = process.env.RMQ_OWNER || process.env.USER || process.env.USERNAME || RMQ_UNKNOWN_CONNECTION_IDENTITY;
|
|
17
|
+
return [
|
|
18
|
+
queue || RMQ_UNKNOWN_CONNECTION_IDENTITY,
|
|
19
|
+
environment,
|
|
20
|
+
owner,
|
|
21
|
+
os_1.default.hostname(),
|
|
22
|
+
`pid-${process.pid}`,
|
|
23
|
+
].join('/');
|
|
24
|
+
}
|
|
25
|
+
function addRmqConnectionName(options) {
|
|
26
|
+
var _a;
|
|
27
|
+
const connectionOptions = (_a = options.socketOptions) === null || _a === void 0 ? void 0 : _a.connectionOptions;
|
|
28
|
+
const configuredClientProperties = connectionOptions === null || connectionOptions === void 0 ? void 0 : connectionOptions.clientProperties;
|
|
29
|
+
const clientProperties = configuredClientProperties
|
|
30
|
+
&& typeof configuredClientProperties === 'object'
|
|
31
|
+
&& !Array.isArray(configuredClientProperties)
|
|
32
|
+
? configuredClientProperties
|
|
33
|
+
: {};
|
|
34
|
+
const configuredConnectionName = clientProperties.connection_name;
|
|
35
|
+
const connectionName = typeof configuredConnectionName === 'string' && configuredConnectionName.trim()
|
|
36
|
+
? configuredConnectionName
|
|
37
|
+
: createRmqConnectionName(options.queue);
|
|
38
|
+
return {
|
|
39
|
+
...options,
|
|
40
|
+
socketOptions: {
|
|
41
|
+
...options.socketOptions,
|
|
42
|
+
connectionOptions: {
|
|
43
|
+
...connectionOptions,
|
|
44
|
+
clientProperties: {
|
|
45
|
+
...clientProperties,
|
|
46
|
+
connection_name: connectionName,
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* @author jun
|
|
54
|
+
* @description RabbitMQ 연결 식별 정보와 graceful shutdown을 제공하는 RMQ 서버
|
|
55
|
+
* @process 연결 옵션에 추적 가능한 connection_name을 추가한 뒤 메시지 종료를 보장한다.
|
|
56
|
+
*/
|
|
9
57
|
class GracefulServerRMQ extends microservices_1.ServerRMQ {
|
|
10
58
|
constructor(options) {
|
|
11
|
-
|
|
12
|
-
|
|
59
|
+
const optionsWithConnectionName = addRmqConnectionName(options);
|
|
60
|
+
super(optionsWithConnectionName);
|
|
13
61
|
this.runningMessages = 0;
|
|
14
62
|
this.closing = false;
|
|
15
63
|
this.consumerTag = null;
|
|
64
|
+
this.options = optionsWithConnectionName;
|
|
16
65
|
this.waitingEndingHandlersTimeoutMs = this.getOptionsProp(this.options, 'waitingEndingHandlersTimeoutMs')
|
|
17
66
|
|| RMQ_DEFAULT_WAITING_ENDING_HANDLERS_TIMEOUT_MS;
|
|
18
67
|
this.waitingEndingHandlersIntervalMs = this.getOptionsProp(this.options, 'waitingEndingHandlersIntervalMs')
|
|
@@ -133,7 +133,7 @@ let GracefulShutdownService = class GracefulShutdownService {
|
|
|
133
133
|
});
|
|
134
134
|
}
|
|
135
135
|
};
|
|
136
|
-
GracefulShutdownService =
|
|
136
|
+
exports.GracefulShutdownService = GracefulShutdownService;
|
|
137
|
+
exports.GracefulShutdownService = GracefulShutdownService = __decorate([
|
|
137
138
|
(0, common_1.Injectable)()
|
|
138
139
|
], GracefulShutdownService);
|
|
139
|
-
exports.GracefulShutdownService = GracefulShutdownService;
|
package/modules/redis.module.js
CHANGED
|
@@ -15,10 +15,10 @@ const redisProvider = {
|
|
|
15
15
|
};
|
|
16
16
|
let RedisModule = class RedisModule {
|
|
17
17
|
};
|
|
18
|
-
RedisModule =
|
|
18
|
+
exports.RedisModule = RedisModule;
|
|
19
|
+
exports.RedisModule = RedisModule = __decorate([
|
|
19
20
|
(0, common_1.Module)({
|
|
20
21
|
providers: [redisProvider],
|
|
21
22
|
exports: [redisProvider],
|
|
22
23
|
})
|
|
23
24
|
], RedisModule);
|
|
24
|
-
exports.RedisModule = RedisModule;
|
package/package.json
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yolo-croket-dev/core",
|
|
3
|
-
"version": "1.9.
|
|
3
|
+
"version": "1.9.13-beta.1",
|
|
4
4
|
"author": "Yolo Co., Ltd.",
|
|
5
5
|
"description": "Yolo commonization module",
|
|
6
6
|
"main": "index.js",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"build": "tsc",
|
|
9
9
|
"lint": "eslint src/**/*.ts",
|
|
10
|
+
"test": "yarn build && node --test tests/*.test.cjs",
|
|
10
11
|
"fix": "eslint --fix src/**/*.ts",
|
|
11
12
|
"clean": "tsc --build --clean",
|
|
12
13
|
"prepublishOnly": "npm run build",
|
|
@@ -31,8 +31,8 @@ let ValidationPipe = class ValidationPipe {
|
|
|
31
31
|
return value;
|
|
32
32
|
}
|
|
33
33
|
};
|
|
34
|
-
ValidationPipe =
|
|
34
|
+
exports.ValidationPipe = ValidationPipe;
|
|
35
|
+
exports.ValidationPipe = ValidationPipe = __decorate([
|
|
35
36
|
(0, common_1.Injectable)(),
|
|
36
37
|
__metadata("design:paramtypes", [Object])
|
|
37
38
|
], ValidationPipe);
|
|
38
|
-
exports.ValidationPipe = ValidationPipe;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
const assert = require('node:assert/strict');
|
|
2
|
+
const test = require('node:test');
|
|
3
|
+
const { Observable } = require('rxjs');
|
|
4
|
+
|
|
5
|
+
const { AmqpHandler, HttpExceptionCustom } = require('../functions/amqp-handler.function.js');
|
|
6
|
+
|
|
7
|
+
test('param.timeout이 지나면 AMQP Observable 구독을 해제한다', async () => {
|
|
8
|
+
let isUnsubscribed = false;
|
|
9
|
+
const client = {
|
|
10
|
+
send() {
|
|
11
|
+
return new Observable(() => () => {
|
|
12
|
+
isUnsubscribed = true;
|
|
13
|
+
});
|
|
14
|
+
},
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
await assert.rejects(
|
|
18
|
+
AmqpHandler(client, 'test.timeout', {
|
|
19
|
+
payload: {},
|
|
20
|
+
param: {
|
|
21
|
+
traceId: 'trace-id',
|
|
22
|
+
timeout: 5,
|
|
23
|
+
},
|
|
24
|
+
}),
|
|
25
|
+
(error) => {
|
|
26
|
+
assert.equal(error instanceof HttpExceptionCustom, true);
|
|
27
|
+
assert.equal(error.errInfo.type, 'TIMEOUT_ERROR');
|
|
28
|
+
return true;
|
|
29
|
+
},
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
assert.equal(isUnsubscribed, true);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
test('응답이 먼저 완료되면 기존 AMQP 응답을 반환한다', async () => {
|
|
36
|
+
const client = {
|
|
37
|
+
send() {
|
|
38
|
+
return new Observable((subscriber) => {
|
|
39
|
+
subscriber.next({ isError: false, data: { ok: true } });
|
|
40
|
+
subscriber.complete();
|
|
41
|
+
});
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const result = await AmqpHandler(client, 'test.success', { payload: {}, param: { traceId: 'trace-id', timeout: 50 } });
|
|
46
|
+
assert.deepEqual(result, { data: { ok: true } });
|
|
47
|
+
});
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
const assert = require('node:assert/strict');
|
|
2
|
+
const os = require('node:os');
|
|
3
|
+
const test = require('node:test');
|
|
4
|
+
|
|
5
|
+
const {
|
|
6
|
+
GracefulServerRMQ,
|
|
7
|
+
} = require('../modules/graceful-shutdown/graceful-shutdown.rmq.js');
|
|
8
|
+
|
|
9
|
+
const CONNECTION_NAME_ENV_KEYS = [
|
|
10
|
+
'ENV',
|
|
11
|
+
'NODE_ENV',
|
|
12
|
+
'RMQ_OWNER',
|
|
13
|
+
'USER',
|
|
14
|
+
'USERNAME',
|
|
15
|
+
];
|
|
16
|
+
|
|
17
|
+
function getClientProperties(options) {
|
|
18
|
+
const server = new GracefulServerRMQ(options);
|
|
19
|
+
return server.options.socketOptions.connectionOptions.clientProperties;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function runWithEnvironment(environment, callback) {
|
|
23
|
+
const originalEnvironment = Object.fromEntries(
|
|
24
|
+
CONNECTION_NAME_ENV_KEYS.map((key) => [key, process.env[key]]),
|
|
25
|
+
);
|
|
26
|
+
try {
|
|
27
|
+
for (const key of CONNECTION_NAME_ENV_KEYS) {
|
|
28
|
+
delete process.env[key];
|
|
29
|
+
}
|
|
30
|
+
Object.assign(process.env, environment);
|
|
31
|
+
callback();
|
|
32
|
+
} finally {
|
|
33
|
+
for (const key of CONNECTION_NAME_ENV_KEYS) {
|
|
34
|
+
const originalValue = originalEnvironment[key];
|
|
35
|
+
if (originalValue === undefined) {
|
|
36
|
+
delete process.env[key];
|
|
37
|
+
} else {
|
|
38
|
+
process.env[key] = originalValue;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
test('자동 connection_name에 큐와 실행 주체 정보를 포함한다', () => {
|
|
45
|
+
runWithEnvironment({ ENV: 'release', RMQ_OWNER: 'jun' }, () => {
|
|
46
|
+
const actualClientProperties = getClientProperties({ queue: 'STORE_ITEM_SERVER' });
|
|
47
|
+
const expectedConnectionName = `STORE_ITEM_SERVER/release/jun/${os.hostname()}/pid-${process.pid}`;
|
|
48
|
+
assert.equal(actualClientProperties.connection_name, expectedConnectionName);
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test('기존 connectionOptions와 clientProperties를 보존한다', () => {
|
|
53
|
+
runWithEnvironment({ NODE_ENV: 'production', USER: 'deploy' }, () => {
|
|
54
|
+
const inputOptions = {
|
|
55
|
+
queue: 'USER_SERVICE',
|
|
56
|
+
socketOptions: {
|
|
57
|
+
noDelay: true,
|
|
58
|
+
connectionOptions: {
|
|
59
|
+
locale: 'ko_KR',
|
|
60
|
+
clientProperties: {
|
|
61
|
+
product: 'croket',
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
const server = new GracefulServerRMQ(inputOptions);
|
|
67
|
+
assert.equal(server.options.socketOptions.noDelay, true);
|
|
68
|
+
assert.equal(server.options.socketOptions.connectionOptions.locale, 'ko_KR');
|
|
69
|
+
assert.equal(server.options.socketOptions.connectionOptions.clientProperties.product, 'croket');
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
test('명시적으로 설정한 문자열 connection_name을 보존한다', () => {
|
|
74
|
+
const actualClientProperties = getClientProperties({
|
|
75
|
+
queue: 'USER_SERVICE',
|
|
76
|
+
socketOptions: {
|
|
77
|
+
connectionOptions: {
|
|
78
|
+
clientProperties: {
|
|
79
|
+
connection_name: 'custom-connection-name',
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
});
|
|
84
|
+
assert.equal(actualClientProperties.connection_name, 'custom-connection-name');
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test('문자열이 아닌 connection_name은 자동 생성값으로 대체한다', () => {
|
|
88
|
+
runWithEnvironment({ ENV: 'release', RMQ_OWNER: 'jun' }, () => {
|
|
89
|
+
const actualClientProperties = getClientProperties({
|
|
90
|
+
queue: 'STORE_ITEM_SERVER',
|
|
91
|
+
socketOptions: {
|
|
92
|
+
connectionOptions: {
|
|
93
|
+
clientProperties: {
|
|
94
|
+
connection_name: 123,
|
|
95
|
+
},
|
|
96
|
+
},
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
const expectedConnectionName = `STORE_ITEM_SERVER/release/jun/${os.hostname()}/pid-${process.pid}`;
|
|
100
|
+
assert.equal(actualClientProperties.connection_name, expectedConnectionName);
|
|
101
|
+
});
|
|
102
|
+
});
|