Skip to content

Commit d034ff1

Browse files
author
Artem
committed
add more logs
1 parent f077748 commit d034ff1

File tree

4 files changed

+45
-16
lines changed

4 files changed

+45
-16
lines changed

redisinsight/api/src/modules/pub-sub/model/abstract.subscription.ts

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import { ISubscription } from 'src/modules/pub-sub/interfaces/subscription.inter
66
import { IMessage } from 'src/modules/pub-sub/interfaces/message.interface';
77
import * as IORedis from 'ioredis';
88

9+
const EMIT_WAIT = 30;
10+
const EMIT_MAX_WAIT = 100;
11+
const MESSAGES_MAX = 5000;
12+
913
export abstract class AbstractSubscription implements ISubscription {
1014
protected readonly id: string;
1115

@@ -26,34 +30,44 @@ export abstract class AbstractSubscription implements ISubscription {
2630
this.id = `${this.type}:${this.channel}`;
2731
this.debounce = debounce(() => {
2832
if (this.messages.length) {
29-
this.userClient.getSocket().emit(this.id, {
30-
messages: this.messages.slice(0, 5000),
31-
count: this.messages.length,
32-
} as MessagesResponse);
33+
this.userClient.getSocket()
34+
.emit(this.id, {
35+
messages: this.messages.slice(0, MESSAGES_MAX),
36+
count: this.messages.length,
37+
} as MessagesResponse);
3338
this.messages = [];
3439
}
35-
}, 30, {
36-
maxWait: 100,
40+
}, EMIT_WAIT, {
41+
maxWait: EMIT_MAX_WAIT,
3742
});
3843
}
3944

40-
getId() { return this.id; }
41-
42-
getChannel() { return this.channel; }
43-
44-
getType() { return this.type; }
45+
getId() {
46+
return this.id;
47+
}
4548

46-
async subscribe(client: IORedis.Redis | IORedis.Cluster): Promise<void> {
47-
throw new Error('"subscribe" method should be implemented');
49+
getChannel() {
50+
return this.channel;
4851
}
4952

50-
async unsubscribe(client: IORedis.Redis | IORedis.Cluster): Promise<void> {
51-
throw new Error('"unsubscribe" method should be implemented');
53+
getType() {
54+
return this.type;
5255
}
5356

57+
abstract subscribe(client: IORedis.Redis | IORedis.Cluster): Promise<void>;
58+
59+
abstract unsubscribe(client: IORedis.Redis | IORedis.Cluster): Promise<void>;
60+
5461
pushMessage(message: IMessage) {
5562
this.messages.push(message);
5663

5764
this.debounce();
5865
}
66+
67+
toString() {
68+
return `${this.constructor.name}:${JSON.stringify({
69+
id: this.id,
70+
mL: this.messages.length,
71+
})}`;
72+
}
5973
}

redisinsight/api/src/modules/pub-sub/model/user-session.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,15 @@ export class UserSession {
3838
* @param subscription
3939
*/
4040
async subscribe(subscription: ISubscription) {
41+
this.logger.debug(`Subscribe ${subscription} ${this}. Getting Redis client...`);
42+
4143
const client = await this.redisClient?.getClient();
4244

4345
if (!client) { throw new Error('There is no Redis client initialized'); }
4446

4547
if (!this.subscriptions.has(subscription.getId())) {
4648
this.subscriptions.set(subscription.getId(), subscription);
49+
this.logger.debug(`Subscribe to Redis ${subscription} ${this}`);
4750
await subscription.subscribe(client);
4851
}
4952
}
@@ -54,14 +57,18 @@ export class UserSession {
5457
* @param subscription
5558
*/
5659
async unsubscribe(subscription: ISubscription) {
60+
this.logger.debug(`Unsubscribe ${subscription} ${this}`);
61+
5762
this.subscriptions.delete(subscription.getId());
5863

5964
const client = await this.redisClient?.getClient();
6065

6166
if (client) {
67+
this.logger.debug(`Unsubscribe from Redis ${subscription} ${this}`);
6268
await subscription.unsubscribe(client);
6369

6470
if (!this.subscriptions.size) {
71+
this.logger.debug(`Unsubscribe: Destroy RedisClient ${this}`);
6572
this.redisClient.destroy();
6673
}
6774
}
@@ -88,6 +95,8 @@ export class UserSession {
8895
* to be sure that there is no open connections left
8996
*/
9097
handleDisconnect() {
98+
this.logger.debug(`Handle disconnect ${this}`);
99+
91100
this.userClient.getSocket().emit(
92101
PubSubServerEvents.Exception,
93102
new PubSubWsException(ERROR_MESSAGES.NO_CONNECTION_TO_REDIS_DB),
@@ -104,6 +113,8 @@ export class UserSession {
104113

105114
this.subscriptions = new Map();
106115
this.redisClient.destroy();
116+
117+
this.logger.debug(`Destroyed ${this}`);
107118
}
108119

109120
toString() {

redisinsight/api/src/modules/pub-sub/pub-sub.service.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ export class PubSubService {
2727
*/
2828
async subscribe(userClient: UserClient, dto: SubscribeDto) {
2929
try {
30+
this.logger.log('Subscribing to channels(s)');
31+
3032
const session = await this.sessionProvider.getOrCreateUserSession(userClient);
3133
await Promise.all(dto.subscriptions.map((subDto) => session.subscribe(
3234
this.subscriptionProvider.createSubscription(userClient, subDto),
@@ -49,6 +51,8 @@ export class PubSubService {
4951
*/
5052
async unsubscribe(userClient: UserClient, dto: SubscribeDto) {
5153
try {
54+
this.logger.log('Unsubscribing from channels(s)');
55+
5256
const session = await this.sessionProvider.getOrCreateUserSession(userClient);
5357
await Promise.all(dto.subscriptions.map((subDto) => session.unsubscribe(
5458
this.subscriptionProvider.createSubscription(userClient, subDto),
@@ -120,6 +124,7 @@ export class PubSubService {
120124
* @param id
121125
*/
122126
async handleDisconnect(id: string) {
127+
this.logger.log(`Handle disconnect event: ${id}`);
123128
const session = this.sessionProvider.getUserSession(id);
124129

125130
if (session) {

redisinsight/api/test/api/ws/pub-sub/pub-sub.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ describe('pub-sub', function () {
100100
expect(ack).to.eql({ status: 'ok' });
101101

102102
client.on('s:channel-a', (data) => {
103-
console.log('___data', data);
104103
expect(data.count).to.be.eql(1);
105104
expect(data.messages.length).to.be.eql(1);
106105
const [message] = data.messages;

0 commit comments

Comments
 (0)