Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Improve caching mechanism for URL indexing status #75

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 44 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,39 @@ import { readFileSync, existsSync, mkdirSync, writeFileSync } from "fs";
import path from "path";

const CACHE_TIMEOUT = 1000 * 60 * 60 * 24 * 14; // 14 days
const SHORT_TIMEOUT = 1000 * 60 * 60; // 1 hour

const indexableStatuses = [
Status.SubmittedAndIndexed,
Status.CrawledCurrentlyNotIndexed,
Status.DiscoveredCurrentlyNotIndexed,
Status.Forbidden,
Status.Error,
Status.RateLimited,
];

const quickFixStatuses = [Status.NotFound, Status.PageWithRedirect];

const shouldRecheck = (status: Status, lastCheckedAt: string) => {
const timeSinceLastCheck = Date.now() - new Date(lastCheckedAt).getTime();

if (indexableStatuses.includes(status)) {
if (status === Status.SubmittedAndIndexed) {
return timeSinceLastCheck > CACHE_TIMEOUT;
}
// For other indexable statuses, check more frequently
return timeSinceLastCheck > SHORT_TIMEOUT;
}

if (quickFixStatuses.includes(status)) {
// For statuses that might be quickly fixed, use a shorter timeout
return timeSinceLastCheck > SHORT_TIMEOUT;
}

// For any other status, use the standard cache timeout
return timeSinceLastCheck > CACHE_TIMEOUT;
};

export const QUOTA = {
rpm: {
retries: 3,
Expand Down Expand Up @@ -108,22 +141,26 @@ export const index = async (input: string = process.argv[2], options: IndexOptio
[Status.RateLimited]: [],
[Status.Forbidden]: [],
[Status.Error]: [],
[Status.NotFound]: [],
};

const indexableStatuses = [
Status.DiscoveredCurrentlyNotIndexed,
Status.SubmittedAndIndexed,
Status.CrawledCurrentlyNotIndexed,
Status.DiscoveredCurrentlyNotIndexed,
Status.URLIsUnknownToGoogle,
Status.Forbidden,
Status.Error,
Status.RateLimited,
Status.NotFound,
];

const shouldRecheck = (status: Status, lastCheckedAt: string) => {
const shouldIndexIt = indexableStatuses.includes(status);
const isOld = new Date(lastCheckedAt) < new Date(Date.now() - CACHE_TIMEOUT);
return shouldIndexIt && isOld;
};
const urlsToProcess = pages.filter((url) => {
const result = statusPerUrl[url];
return !result || shouldRecheck(result.status, result.lastCheckedAt);
});

console.log(`👉 Found ${urlsToProcess.length} URLs that need processing out of ${pages.length} total URLs`);

await batch(
async (url) => {
Expand All @@ -136,7 +173,7 @@ export const index = async (input: string = process.argv[2], options: IndexOptio

pagesPerStatus[result.status] = pagesPerStatus[result.status] ? [...pagesPerStatus[result.status], url] : [url];
},
pages,
urlsToProcess,
50,
(batchIndex, batchCount) => {
console.log(`📦 Batch ${batchIndex + 1} of ${batchCount} complete`);
Expand Down
1 change: 1 addition & 0 deletions src/shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ export enum Status {
RateLimited = "RateLimited",
Forbidden = "Forbidden",
Error = "Error",
NotFound = "Not found (404)",
}