Added logic to remove no-longer-valid channels from cache and handle those TypeErrors. But does not remove it from database
This commit is contained in:
+17
-6
@@ -25,8 +25,8 @@ module.exports = {
|
|||||||
// Main interval loop
|
// Main interval loop
|
||||||
setInterval(async () => {
|
setInterval(async () => {
|
||||||
await updateCache();
|
await updateCache();
|
||||||
await notifySubscribers();
|
await checkLive();
|
||||||
}, 5000); // Every 5 seconds
|
}, 15000); // Every 15 seconds
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -62,30 +62,41 @@ async function updateCache() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Notify subscribers about live status changes
|
// Notify subscribers about live status changes
|
||||||
async function notifySubscribers() {
|
async function checkLive() {
|
||||||
for (const [channelName, subscribers] of channelList) {
|
for (const [channelName, subscribers] of channelList) {
|
||||||
try {
|
try {
|
||||||
|
// Attempt to fetch stream data
|
||||||
const streamData = await tClient.getStreams({ channel: channelName });
|
const streamData = await tClient.getStreams({ channel: channelName });
|
||||||
|
|
||||||
if (streamData?.data[0]) {
|
if (streamData?.data && streamData.data.length > 0) {
|
||||||
// Stream is live
|
// Stream is live
|
||||||
if (!isLive.has(channelName)) {
|
if (!isLive.has(channelName)) {
|
||||||
await notifyLive(channelName, subscribers, streamData.data[0]);
|
await notifyLive(channelName, subscribers, streamData.data[0]);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Stream is offline
|
// Stream is offline, do not remove from channelList
|
||||||
if (isLive.has(channelName)) {
|
if (isLive.has(channelName)) {
|
||||||
console.log(`${channelName} went offline!`);
|
console.log(`${channelName} went offline!`);
|
||||||
isLive.delete(channelName);
|
isLive.delete(channelName);
|
||||||
}
|
}
|
||||||
isOffline.add(channelName);
|
isOffline.add(channelName); // Mark as offline
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
// Specific handling for the TypeError
|
||||||
|
if (error instanceof TypeError && error.message.includes("Cannot read properties of undefined")) {
|
||||||
|
console.warn(`Channel "${channelName}" returned an invalid response. Removing from cache.`);
|
||||||
|
channelList.delete(channelName);
|
||||||
|
isLive.delete(channelName);
|
||||||
|
isOffline.delete(channelName);
|
||||||
|
} else {
|
||||||
|
// Log unexpected errors
|
||||||
console.error(`Error notifying for ${channelName}:`, error);
|
console.error(`Error notifying for ${channelName}:`, error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Notify subscribers that a channel has gone live
|
// Notify subscribers that a channel has gone live
|
||||||
async function notifyLive(channelName, subscribers, streamData) {
|
async function notifyLive(channelName, subscribers, streamData) {
|
||||||
const liveData = {
|
const liveData = {
|
||||||
|
|||||||
Reference in New Issue
Block a user