|
| 1 | +import NameRecordRepository from "./nameRecordRepository.js"; |
| 2 | + |
| 3 | +const VANISH_STREAM_KEY = "vanish_requests"; |
| 4 | +const LAST_PROCESSED_ID_KEY = "vanish_requests:nip05_service:last_id"; |
| 5 | +const BLOCK_TIME_MS = 5000; // 5 seconds |
| 6 | + |
| 7 | +class VanishSubscriber { |
| 8 | + constructor(vanishRequestsRedis, nip05Redis) { |
| 9 | + // Right now we have a local redis instance for nip05 data and a remote one |
| 10 | + // used by all our services. For the momen, the remote one is only used for |
| 11 | + // the vanish stream. |
| 12 | + // TODO: Refactor to migrate and use only one redis instance. |
| 13 | + |
| 14 | + const nameRecordRepository = new NameRecordRepository(nip05Redis); |
| 15 | + |
| 16 | + this.vanishRequestsRedis = vanishRequestsRedis; |
| 17 | + this.nameRecordRepository = nameRecordRepository; |
| 18 | + this.abortController = new AbortController(); |
| 19 | + this.isRunning = false; |
| 20 | + } |
| 21 | + |
| 22 | + async processPubkey(pubkey) { |
| 23 | + console.log(`Deleting pubkey: ${pubkey}`); |
| 24 | + await this.nameRecordRepository.deleteByPubkey(pubkey); |
| 25 | + } |
| 26 | + |
| 27 | + async run() { |
| 28 | + if (this.isRunning) return; // Prevent multiple runs |
| 29 | + this.isRunning = true; |
| 30 | + |
| 31 | + let lastProcessedID; |
| 32 | + |
| 33 | + try { |
| 34 | + lastProcessedID = |
| 35 | + (await this.vanishRequestsRedis.get(LAST_PROCESSED_ID_KEY)) || "0-0"; |
| 36 | + console.log(`Starting from last processed ID: ${lastProcessedID}`); |
| 37 | + } catch (err) { |
| 38 | + console.error("Error fetching last processed ID from Redis", err); |
| 39 | + this.isRunning = false; |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + const abortSignal = this.abortController.signal; |
| 44 | + |
| 45 | + while (!abortSignal.aborted) { |
| 46 | + try { |
| 47 | + const streamEntries = await this.vanishRequestsRedis.xread( |
| 48 | + "BLOCK", |
| 49 | + BLOCK_TIME_MS, |
| 50 | + "STREAMS", |
| 51 | + VANISH_STREAM_KEY, |
| 52 | + lastProcessedID |
| 53 | + ); |
| 54 | + |
| 55 | + if (!streamEntries) { |
| 56 | + continue; |
| 57 | + } |
| 58 | + |
| 59 | + for (const [stream, messages] of streamEntries) { |
| 60 | + for (const [messageID, messageData] of messages) { |
| 61 | + const event = createObjectFromPairs(messageData); |
| 62 | + |
| 63 | + console.log(`Vanish requests event: ${JSON.stringify(event)} `); |
| 64 | + const pubkey = event.pubkey; |
| 65 | + |
| 66 | + console.log( |
| 67 | + `Processing message ID: ${messageID} with pubkey: ${pubkey}` |
| 68 | + ); |
| 69 | + |
| 70 | + try { |
| 71 | + await this.processPubkey(pubkey); |
| 72 | + } catch (err) { |
| 73 | + console.error(`Error processing pubkey: ${pubkey}`, err); |
| 74 | + } |
| 75 | + |
| 76 | + try { |
| 77 | + await this.vanishRequestsRedis.set( |
| 78 | + LAST_PROCESSED_ID_KEY, |
| 79 | + messageID |
| 80 | + ); |
| 81 | + lastProcessedID = messageID; |
| 82 | + console.log(`Updated last processed ID to: ${lastProcessedID}`); |
| 83 | + } catch (err) { |
| 84 | + console.error( |
| 85 | + `Error updating last processed ID: ${messageID}`, |
| 86 | + err |
| 87 | + ); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } catch (err) { |
| 92 | + if (abortSignal.aborted) { |
| 93 | + break; |
| 94 | + } |
| 95 | + console.error("Error reading from Redis stream", err); |
| 96 | + await new Promise((resolve) => setTimeout(resolve, 1000)); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + console.log("Cancellation signal received. Exiting gracefully..."); |
| 101 | + await this.vanishRequestsRedis.set(LAST_PROCESSED_ID_KEY, lastProcessedID); |
| 102 | + console.log(`Final last processed ID saved: ${lastProcessedID}`); |
| 103 | + |
| 104 | + this.isRunning = false; |
| 105 | + } |
| 106 | + |
| 107 | + stop() { |
| 108 | + if (!this.isRunning) return; |
| 109 | + this.abortController.abort(); |
| 110 | + console.log( |
| 111 | + "Abort signal sent. Waiting for current processing to finish..." |
| 112 | + ); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +function createObjectFromPairs(messageData) { |
| 117 | + return messageData.reduce((acc, value, index, arr) => { |
| 118 | + if (index % 2 === 0) { |
| 119 | + acc[value] = arr[index + 1]; |
| 120 | + } |
| 121 | + return acc; |
| 122 | + }, {}); |
| 123 | +} |
| 124 | + |
| 125 | +export default VanishSubscriber; |
0 commit comments