Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 | 1x 1x 1x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 1x 1x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 1x 3x 3x 3x 3x 3x 3x 3x 1x 18x 18x 1x 10x 10x 1x 10x 9x 8x 8x 1x 1x 10x 1x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 9x 6x 6x 6x 3x 3x 3x 9x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 3x 10x 1x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 16x 16x 16x 16x 16x 16x 15x 15x 9x 9x 15x 8x 8x 4x 4x 8x 1x 1x 3x 3x 3x 3x 3x 3x 3x 8x 8x 8x 15x 9x 10x 1x 12x 12x 1x 2x 2x 1x 5x 5x 1x 1x 1x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 15x 8x 8x 8x 15x 16x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 7x 7x 7x 7x 7x 7x 1x | import { WebhookMessageObject } from "@/types/api"; import { Redis, ReplyError } from "ioredis"; import { webhook } from "@line/bot-sdk"; import { Logger } from "winston"; import { Env } from "@/Env"; export const MESSAGE_KEY = "message"; export type Id = string; export type WebhookStreamObject = { requestId: string; signature: string; destination: string; events: webhook.Event[]; }; export type RedisRawMessage = { messageId: string; messageValue: string; }; export function createRedisClientByEnv( env: Env, channelId: string ): RedisClient { return new RedisClient( env.redisHost, env.redisPort, env.redisStreamPrefixForLine, channelId, env.redisGroupNameForLine, env.redisMaxRetriesPerRequest ); } export class RedisClient { private streamPrefix: string; private streamName: string; private groupName: string; private client: Redis; constructor( host: string, port: number, streamPrefix: string, channelId: string, groupName: string, maxRetriesPerRequest: number ) { this.client = new Redis({ host, port, maxRetriesPerRequest, }); // エラー情報が標準出力されないようにする this.client.on("error", (_err) => {}); this.streamPrefix = streamPrefix; this.streamName = `${streamPrefix}:${channelId}`; this.groupName = groupName; } async addWebhookStreamObject(object: WebhookStreamObject): Promise<Id> { return await this.client.xadd( this.streamName, "*", MESSAGE_KEY, JSON.stringify(object) ); } async disconnect(): Promise<void> { await this.client.quit(); } async createConsumerGroupIfNotExists(): Promise<boolean> { try { await this.client.xgroup("CREATE", this.streamName, this.groupName, 0); return true; } catch (err) { if (err instanceof Error && err.message.includes("BUSYGROUP")) { return false; } throw err; } } async readNewMessages( logger: Logger, consumerName: string, maxCount: number ): Promise<WebhookMessageObject[]> { const result = await this.client.xreadgroup( "GROUP", this.groupName, consumerName, "COUNT", maxCount, "STREAMS", this.streamName, ">" ); if (!result) { logger.debug("result of readNewMessages is empty"); return []; } logger.debug("result of readNewMessages is not empty"); const [_stream, messages] = result[0] as [ string, Array<[string, Array<string>]> ]; const messageObjects: WebhookMessageObject[] = []; for (const [messageId, fields] of messages) { const childLogger = logger.child({ messageId }); try { const value = this.findValue(fields, MESSAGE_KEY); const message = JSON.parse(value) as WebhookStreamObject; messageObjects.push({ messageId: messageId, requestId: message.requestId, signature: message.signature, destination: message.destination, events: message.events, }); } catch (err) { childLogger.error(err); } } return messageObjects; } async readLongPendingMessages( logger: Logger, consumerName: string, maxIdleTimeMs: number, maxCount: number, maxDeliveryCount: number ): Promise<WebhookMessageObject[]> { const messages: WebhookMessageObject[] = []; let shouldContinue = true; let begin = "-"; const end = "+"; while (shouldContinue) { const pendingMessages = await this.xpending( maxIdleTimeMs, begin, end, 10 ); logger.debug(`pendingMessages count: ${pendingMessages.length}`); if (pendingMessages.length === 0) { shouldContinue = false; } for (const pendingMessage of pendingMessages) { begin = "(" + pendingMessage.messageId; if (pendingMessage.consumer === consumerName) { continue; } if (pendingMessage.deliveryCount >= maxDeliveryCount) { continue; } try { const message = await this.xclaim( consumerName, maxIdleTimeMs, pendingMessage.messageId ); messages.push(message); } catch (err) { logger.warn("skip xclaim for redis pending message", { messageId: pendingMessage.messageId, message: err, }); } if (maxCount && messages.length >= maxCount) { shouldContinue = false; break; } } } return messages; } async countStreamEntries(): Promise<number> { return await this.client.xlen(this.streamName); } async deleteMessage(messageId: string): Promise<number> { return await this.client.xdel(this.streamName, messageId); } async ackMessage(messageId: string): Promise<number> { return await this.client.xack(this.streamName, this.groupName, messageId); } async eachStream( callback: (streamName: string) => Promise<void> ): Promise<void> { let cursor = "0"; while (true) { const [nextCursor, keys] = await this.client.scan( cursor, "MATCH", `${this.streamPrefix}:*`, "COUNT", "100", "TYPE", "stream" ); for (const key of keys) { await callback(key); } if (nextCursor === "0") { break; } cursor = nextCursor; } } async autoClaim( streamName: string, consumer: string, minIdleTime: number, count: number ): Promise<RedisRawMessage[]> { try { const result = await this.client.xautoclaim( streamName, this.groupName, consumer, minIdleTime, "0-0", "COUNT", count ); /* 取得結果は次の形式。2つ目の要素が再割り当てされたメッセージ。 1) "1728895288510-0" 2) 1) 1) "1728891713419-0" 2) 1) "message" 2) "value" 2) 1) "1728893217213-0" 2) 1) "message" 2) "value" 3) 1) "1728895085887-0" 2) 1) "message" 2) "value" 3) (empty array) */ const claimedMessages = result[1] as unknown[]; const messages: RedisRawMessage[] = []; for (const claimedMessage of claimedMessages) { const messageId = claimedMessage[0] as string; const fields = claimedMessage[1] as string[]; const value = this.findValue(fields, MESSAGE_KEY); messages.push({ messageId, messageValue: value }); } return messages; } catch (err) { if (err instanceof ReplyError && err.message.includes("NOGROUP")) { return []; } throw err; } } private async xpending( maxIdleTimeMs: number, begin: string, end: string, count: number ): Promise<{ messageId: string; consumer: string; deliveryCount: number }[]> { const result = (await this.client.xpending( this.streamName, this.groupName, "IDLE", maxIdleTimeMs, begin, end, count )) as Array<unknown[]>; return result.map((msg) => ({ messageId: msg[0] as string, consumer: msg[1] as string, deliveryCount: msg[3] as number, })); } private async xclaim( consumerName: string, maxIdleTimeMs: number, messageId: string ): Promise<WebhookMessageObject> { const result = await this.client.xclaim( this.streamName, this.groupName, consumerName, maxIdleTimeMs, messageId ); const fields = result[0][1] as string[]; const value = this.findValue(fields, "message"); return JSON.parse(value); } private findValue(fields: string[], key: string): string { for (let index = 0; index < fields.length; index++) { if (fields[index] === key) { return fields[index + 1]; } } throw new Error(`key[${key}] is not found in redis message`); } } |