From 5440104585574d6a43b316f0b3c1b7cb7926fe32 Mon Sep 17 00:00:00 2001 From: martin-mfg <2026226+martin-mfg@users.noreply.github.com> Date: Sun, 31 Aug 2025 14:12:42 +0200 Subject: [PATCH] split database.js --- src/common/database.js | 63 ++---------------------------------------- src/repeatRequests.js | 60 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 61 deletions(-) create mode 100644 src/repeatRequests.js diff --git a/src/common/database.js b/src/common/database.js index eca07cd0..841645eb 100644 --- a/src/common/database.js +++ b/src/common/database.js @@ -1,4 +1,3 @@ -import axios from "axios"; import pkg from "pg"; const { Pool } = pkg; @@ -55,7 +54,7 @@ export async function storeRequest(req) { /** * Deletes all requests older than 8 days from the database. */ -async function deleteOldRequests() { +export async function deleteOldRequests() { if (!pool) { return; } @@ -72,7 +71,7 @@ async function deleteOldRequests() { * Fetches all requests which are between 11 hours and 8 days old. * @returns {Promise} Array of all requests between 11 hours and 8 days old. */ -async function getRecentRequests() { +export async function getRecentRequests() { if (!pool) { return []; } @@ -87,61 +86,3 @@ async function getRecentRequests() { const { rows } = await pool.query(query); return rows.map((row) => row.request); } - -/** - * Processes URLs with a thread pool of given size using axios.get. - * @param {string[]} urls An array of URLs to process. - * @param {number} poolSize The number of concurrent requests to process. - * @returns {Promise} A promise that resolves when all requests are processed. - */ -async function makeRequests(urls, poolSize) { - let current = 0; - - /** - * Worker function to process `urls`. - */ - async function worker() { - while (true) { - let idx = current++; - if (idx >= urls.length) { - break; - } - const url = "https://" + process.env.VERCEL_BRANCH_URL + urls[idx]; - try { - if (idx % 10 === 0) { - console.log(`Processing request ${idx + 1} out of ${urls.length}`); - } - await axios.get(url, { - timeout: 10000, - headers: { "x-bypass-store": "true" }, - }); - } catch (err) { - console.error(`Error fetching ${url}:`, err.message); - } - } - } - - const workers = []; - for (let i = 0; i < poolSize; i++) { - workers.push(worker()); - } - await Promise.all(workers); -} - -/** - * Repeats requests made in the last 8 days, excluding those made in the last 11 hours. - */ -export async function repeatRecentRequests() { - if (!pool) { - console.error("Postgres pool is not initialized."); - return; - } - - await deleteOldRequests(); - const urls = await getRecentRequests(); - if (urls.length === 0) { - console.log("No recent requests found."); - } else { - await makeRequests(urls, 5); - } -} diff --git a/src/repeatRequests.js b/src/repeatRequests.js new file mode 100644 index 00000000..afd740ae --- /dev/null +++ b/src/repeatRequests.js @@ -0,0 +1,60 @@ +import axios from "axios"; +import { pool, deleteOldRequests, getRecentRequests } from "./common/database.js"; + +/** + * Processes URLs with a thread pool of given size using axios.get. + * @param {string[]} urls An array of URLs to process. + * @param {number} poolSize The number of concurrent requests to process. + * @returns {Promise} A promise that resolves when all requests are processed. + */ +async function makeRequests(urls, poolSize) { + let current = 0; + + /** + * Worker function to process `urls`. + */ + async function worker() { + while (true) { + let idx = current++; + if (idx >= urls.length) { + break; + } + const url = "https://" + process.env.VERCEL_BRANCH_URL + urls[idx]; + try { + if (idx % 10 === 0) { + console.log(`Processing request ${idx + 1} out of ${urls.length}`); + } + await axios.get(url, { + timeout: 10000, + headers: { "x-bypass-store": "true" }, + }); + } catch (err) { + console.error(`Error fetching ${url}:`, err.message); + } + } + } + + const workers = []; + for (let i = 0; i < poolSize; i++) { + workers.push(worker()); + } + await Promise.all(workers); +} + +/** + * Repeats requests made in the last 8 days, excluding those made in the last 11 hours. + */ +export async function repeatRecentRequests() { + if (!pool) { + console.error("Postgres pool is not initialized."); + return; + } + + await deleteOldRequests(); + const urls = await getRecentRequests(); + if (urls.length === 0) { + console.log("No recent requests found."); + } else { + await makeRequests(urls, 5); + } +}