simple cache tag to dedupe

This commit is contained in:
Nate Kelley 2025-10-08 16:55:44 -06:00
parent edcb3ad577
commit 39eb037b8b
No known key found for this signature in database
GPG Key ID: FD90372AB8D98B4F
5 changed files with 40 additions and 20 deletions

View File

@ -247,7 +247,7 @@ export async function getDashboardHandler(
workspace_member_count: workspaceMemberCount, workspace_member_count: workspaceMemberCount,
}; };
const tag = `take-dashboard-screenshot-${dashboardId}-${versionNumber}`; const tag = `take-dashboard-screenshot-${dashboardId}`;
if ( if (
await shouldTakeScreenshot({ await shouldTakeScreenshot({
tag, tag,

View File

@ -29,7 +29,7 @@ const app = new Hono()
password password
); );
const tag = `take-metric-screenshot-${id}-${version_number}`; const tag = `take-metric-screenshot-${id}`;
if ( if (
await shouldTakeScreenshot({ await shouldTakeScreenshot({
tag, tag,

View File

@ -91,7 +91,7 @@ const app = new Hono()
password password
); );
const tag = `take-report-screenshot-${reportId}-${versionNumber}`; const tag = `take-report-screenshot-${reportId}`;
if ( if (
await shouldTakeScreenshot({ await shouldTakeScreenshot({

View File

@ -5,6 +5,9 @@ import type { Context } from 'hono';
// This helper ensures that we do not run multiple trigger jobs for the same screenshot task concurrently. // This helper ensures that we do not run multiple trigger jobs for the same screenshot task concurrently.
// It checks if a job for the given tag and key is already running or queued before starting a new one. // It checks if a job for the given tag and key is already running or queued before starting a new one.
const currentlyCheckingTags = new Set<string>();
const CACHE_TAG_EXPIRATION_TIME = 1000 * 15; // 15 seconds
export const shouldTakeScreenshot = async ({ export const shouldTakeScreenshot = async ({
tag, tag,
key, key,
@ -16,18 +19,29 @@ export const shouldTakeScreenshot = async ({
}): Promise<boolean> => { }): Promise<boolean> => {
const hasReferrer = !!context.req.header('referer'); const hasReferrer = !!context.req.header('referer');
if (!hasReferrer) { if (!hasReferrer || currentlyCheckingTags.has(tag)) {
return false; return false;
} }
const lastTask = await runs currentlyCheckingTags.add(tag);
.list({
status: ['EXECUTING', 'QUEUED'],
taskIdentifier: key,
tag,
limit: 1,
})
.then((res) => res.data[0]);
return !lastTask; try {
const lastTask = await runs
.list({
status: ['EXECUTING', 'QUEUED'],
taskIdentifier: key,
tag,
limit: 1,
})
.then((res) => res.data[0]);
return !lastTask;
} catch (error) {
console.error('Error checking if screenshot should be taken', { error });
throw error;
} finally {
setTimeout(() => {
currentlyCheckingTags.delete(tag);
}, CACHE_TAG_EXPIRATION_TIME);
}
}; };

View File

@ -8,12 +8,14 @@ export const Route = createFileRoute('/screenshots/reports/$reportId/content')({
component: RouteComponent, component: RouteComponent,
validateSearch: GetReportScreenshotQuerySchema, validateSearch: GetReportScreenshotQuerySchema,
ssr: true, ssr: true,
beforeLoad: async ({ context, params, search }) => { beforeLoad: ({ search }) => {
const report = await prefetchGetReport( return {
context.queryClient, version_number: search.version_number,
params.reportId, };
search.version_number },
); loader: async ({ context, params }) => {
const { version_number } = context;
const report = await prefetchGetReport(context.queryClient, params.reportId, version_number);
if (!report) { if (!report) {
throw redirect({ throw redirect({
@ -23,7 +25,7 @@ export const Route = createFileRoute('/screenshots/reports/$reportId/content')({
const allMetrics = Object.entries(report?.metrics || {}); const allMetrics = Object.entries(report?.metrics || {});
await Promise.all( const res = await Promise.all(
allMetrics.map(([metricId, metric]) => { allMetrics.map(([metricId, metric]) => {
return ensureMetricData(context.queryClient, { return ensureMetricData(context.queryClient, {
id: metricId, id: metricId,
@ -32,6 +34,10 @@ export const Route = createFileRoute('/screenshots/reports/$reportId/content')({
}) })
); );
res.forEach((metric) => {
console.log('metric', metric.data?.length);
});
return { report }; return { report };
}, },
}); });