Skip to content

Commit f1af098

Browse files
author
Artem
committed
#RI-2941 BE Delete consumers from the group
1 parent c3425ec commit f1af098

File tree

4 files changed

+86
-4
lines changed

4 files changed

+86
-4
lines changed

redisinsight/api/src/modules/browser/constants/browser-tool-commands.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ export enum BrowserToolStreamCommands {
8888
XGroupCreate = 'xgroup create',
8989
XGroupSetId = 'xgroup setid',
9090
XGroupDestroy = 'xgroup destroy',
91+
XGroupDelConsumer = 'xgroup delconsumer',
9192
}
9293

9394
export enum BrowserToolTSCommands {

redisinsight/api/src/modules/browser/controllers/stream/consumer.controller.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {
22
Body,
3-
Controller,
3+
Controller, Delete,
44
Param,
55
Post,
66
UsePipes,
@@ -11,7 +11,7 @@ import { ApiRedisInstanceOperation } from 'src/decorators/api-redis-instance-ope
1111
import {
1212
AckPendingEntriesDto, AckPendingEntriesResponse, ClaimPendingEntriesResponse, ClaimPendingEntryDto,
1313
ConsumerDto,
14-
ConsumerGroupDto,
14+
ConsumerGroupDto, DeleteConsumersDto,
1515
GetConsumersDto, GetPendingEntriesDto, PendingEntryDto,
1616
} from 'src/modules/browser/dto/stream.dto';
1717
import { ConsumerService } from 'src/modules/browser/services/stream/consumer.service';
@@ -41,6 +41,18 @@ export class ConsumerController {
4141
return this.service.getConsumers({ instanceId }, dto);
4242
}
4343

44+
@Delete('')
45+
@ApiRedisInstanceOperation({
46+
description: 'Delete Consumer(s) from the Consumer Group',
47+
statusCode: 200,
48+
})
49+
async deleteConsumers(
50+
@Param('dbInstance') instanceId: string,
51+
@Body() dto: DeleteConsumersDto,
52+
): Promise<void> {
53+
return this.service.deleteConsumers({ instanceId }, dto);
54+
}
55+
4456
@Post('/pending-messages/get')
4557
@ApiRedisInstanceOperation({
4658
description: 'Get pending entries list',

redisinsight/api/src/modules/browser/dto/stream.dto.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,20 @@ export class GetConsumersDto extends KeyDto {
325325
groupName: string;
326326
}
327327

328+
export class DeleteConsumersDto extends GetConsumersDto {
329+
@ApiProperty({
330+
description: 'Names of consumers to delete',
331+
type: String,
332+
isArray: true,
333+
example: ['consumer-1', 'consumer-2'],
334+
})
335+
@IsDefined()
336+
@IsArray()
337+
@ArrayNotEmpty()
338+
@Type(() => String)
339+
consumerNames: string[];
340+
}
341+
328342
export class PendingEntryDto {
329343
@ApiProperty({
330344
type: String,

redisinsight/api/src/modules/browser/services/stream/consumer.service.ts

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ import {
33
} from '@nestjs/common';
44
import { IFindRedisClientInstanceByOptions } from 'src/modules/core/services/redis/redis.service';
55
import { RedisErrorCodes } from 'src/constants';
6-
import { catchAclError, convertStringsArrayToObject } from 'src/utils';
6+
import {catchAclError, catchTransactionError, convertStringsArrayToObject} from 'src/utils';
77
import {
8+
BrowserToolCommands,
89
BrowserToolKeysCommands, BrowserToolStreamCommands,
910
} from 'src/modules/browser/constants/browser-tool-commands';
1011
import { BrowserToolService } from 'src/modules/browser/services/browser-tool/browser-tool.service';
1112
import ERROR_MESSAGES from 'src/constants/error-messages';
1213
import {
1314
AckPendingEntriesDto, AckPendingEntriesResponse, ClaimPendingEntriesResponse, ClaimPendingEntryDto,
14-
ConsumerDto,
15+
ConsumerDto, DeleteConsumersDto,
1516
GetConsumersDto, GetPendingEntriesDto, PendingEntryDto,
1617
} from 'src/modules/browser/dto/stream.dto';
1718

@@ -61,6 +62,60 @@ export class ConsumerService {
6162
}
6263
}
6364

65+
/**
66+
* Get consumers list inside particular group
67+
* @param clientOptions
68+
* @param dto
69+
*/
70+
async deleteConsumers(
71+
clientOptions: IFindRedisClientInstanceByOptions,
72+
dto: DeleteConsumersDto,
73+
): Promise<void> {
74+
try {
75+
this.logger.log('Deleting consumers from the group.');
76+
77+
const exists = await this.browserTool.execCommand(
78+
clientOptions,
79+
BrowserToolKeysCommands.Exists,
80+
[dto.keyName],
81+
);
82+
83+
if (!exists) {
84+
return Promise.reject(new NotFoundException(ERROR_MESSAGES.KEY_NOT_EXIST));
85+
}
86+
87+
const toolCommands: Array<[
88+
toolCommand: BrowserToolCommands,
89+
...args: Array<string | number>,
90+
]> = dto.consumerNames.map((consumerName) => (
91+
[
92+
BrowserToolStreamCommands.XGroupDelConsumer,
93+
dto.keyName,
94+
dto.groupName,
95+
consumerName,
96+
]
97+
));
98+
99+
const [
100+
transactionError,
101+
transactionResults,
102+
] = await this.browserTool.execMulti(clientOptions, toolCommands);
103+
catchTransactionError(transactionError, transactionResults);
104+
105+
return undefined;
106+
} catch (error) {
107+
if (error instanceof NotFoundException) {
108+
throw error;
109+
}
110+
111+
if (error?.message.includes(RedisErrorCodes.WrongType)) {
112+
throw new BadRequestException(error.message);
113+
}
114+
115+
throw catchAclError(error);
116+
}
117+
}
118+
64119
/**
65120
* Get list of pending entries info for particular consumer
66121
* @param clientOptions

0 commit comments

Comments
 (0)