#5 replace all console.logs with debugger
All checks were successful
DRB Tests / drb_mocha_tests (pull_request) Successful in 32s

This commit is contained in:
Logan Cusano
2024-05-25 23:52:18 -04:00
parent 81a215f048
commit 2ab5a181bd
22 changed files with 192 additions and 133 deletions

View File

@@ -5,6 +5,9 @@ import { DebugBuilder } from "../modules/debugger.mjs";
import UserAgent from "user-agents";
import Parser from 'rss-parser';
import dotenv from 'dotenv';
dotenv.config()
// Initialize the User-Agent string
process.env.USER_AGENT_STRING = new UserAgent({ platform: 'Win32' }).toString();
@@ -16,8 +19,9 @@ const parser = new Parser({
});
const log = new DebugBuilder("server", "feedHandler");
const runningPostsToRemove = {};
const sourceFailureLimit = 5;
const sourceFailureLimit = process.env.RSS_SOURCE_FAILURE_LIMIT ?? 5;
const runningSourcesToRemove = {}; // This holds the sources that are pending removal (they've failed to load, return data, etc.)
export const returnHash = (...stringsIncluded) => {
return crypto.createHash('sha1').update(stringsIncluded.join("-<<??//\\\\??>>-")).digest("base64");
@@ -100,22 +104,22 @@ export const addSource = async (title, link, category, guildId, channelId, callb
export const removeSource = async (sourceURL) => {
log.INFO("Removing source:", sourceURL);
if (!runningPostsToRemove[sourceURL]) {
runningPostsToRemove[sourceURL] = { count: 1, timestamp: Date.now(), ignoredAttempts: 0 };
if (!runningSourcesToRemove[sourceURL]) {
runningSourcesToRemove[sourceURL] = { count: 1, timestamp: Date.now(), ignoredAttempts: 0 };
return;
}
const elapsedTime = Date.now() - runningPostsToRemove[sourceURL].timestamp;
const waitTime = runningPostsToRemove[sourceURL].count * 30000;
const elapsedTime = Date.now() - runningSourcesToRemove[sourceURL].timestamp;
const waitTime = runningSourcesToRemove[sourceURL].count * 30000;
if (elapsedTime <= waitTime) {
runningPostsToRemove[sourceURL].ignoredAttempts += 1;
runningSourcesToRemove[sourceURL].ignoredAttempts += 1;
return;
}
if (runningPostsToRemove[sourceURL].count < sourceFailureLimit) {
runningPostsToRemove[sourceURL].count += 1;
runningPostsToRemove[sourceURL].timestamp = Date.now();
if (runningSourcesToRemove[sourceURL].count < sourceFailureLimit) {
runningSourcesToRemove[sourceURL].count += 1;
runningSourcesToRemove[sourceURL].timestamp = Date.now();
return;
}

View File

@@ -1,37 +1,49 @@
//Will handle updating feeds in all channels
// Will handle updating feeds in all channels
import { DebugBuilder } from "../modules/debugger.mjs";
import { updateFeeds } from "./feedHandler.mjs";
import dotenv from 'dotenv';
dotenv.config()
dotenv.config();
const log = new DebugBuilder("server", "rssController");
const refreshInterval = process.env.RSS_REFRESH_INTERVAL ?? 300000;
const refreshInterval = parseInt(process.env.RSS_REFRESH_INTERVAL) || 300000;
export class RSSController {
constructor(client) {
this.client = client;
this.client = client;
this.intervalId = null;
}
async start() {
// Wait for the refresh period before starting RSS feeds, so the rest of the bot can start
await new Promise(resolve => setTimeout(resolve, refreshInterval));
log.INFO("Starting RSS Controller");
// Get initial feeds before starting the infinite loop
await updateFeeds(this.client);
while(true) {
// Wait for the refresh interval, then wait for the posts to return, then wait a quarter of the refresh interval to make sure everything is cleared up
await new Promise(resolve => setTimeout(resolve, refreshInterval));
try {
log.INFO("Starting RSS Controller");
// Get initial feeds before starting the interval loop
await this.collectLatestPosts();
await new Promise(resolve => setTimeout(resolve, refreshInterval / 4));
}
// Start the interval loop for updating feeds
this.intervalId = setInterval(async () => {
await this.collectLatestPosts();
}, refreshInterval);
} catch (error) {
log.ERROR(`Failed to start RSS Controller: ${error.message}`);
}
}
async stop() {
if (this.intervalId) {
clearInterval(this.intervalId);
log.INFO("RSS Controller stopped");
}
}
async collectLatestPosts() {
log.INFO("Updating sources");
await updateFeeds(this.client);
try {
log.INFO("Updating sources");
await updateFeeds(this.client);
} catch (error) {
log.ERROR(`Error updating feeds: ${error.message}`);
}
}
}