Refactor pipeline task handling and UI flows
This commit is contained in:
@@ -143,6 +143,7 @@ async function queueAuditGenerationAfterPageSpeed(
|
||||
export const processPageSpeedAudit = internalAction({
|
||||
args: {
|
||||
runId: v.id("agentRuns"),
|
||||
queueGeneration: v.optional(v.boolean()),
|
||||
},
|
||||
handler: async (ctx, args) => {
|
||||
const apiKeyRaw = process.env.PAGESPEED_API_KEY?.trim();
|
||||
@@ -185,19 +186,82 @@ export const processPageSpeedAudit = internalAction({
|
||||
let succeededStrategies = 0;
|
||||
|
||||
try {
|
||||
for (const strategy of STRATEGIES) {
|
||||
const fetchedAt = Date.now();
|
||||
try {
|
||||
const raw = await fetchPageSpeedResult({
|
||||
url: sourceUrl,
|
||||
strategy,
|
||||
apiKey,
|
||||
timeoutMs,
|
||||
});
|
||||
const rawJson = JSON.stringify(raw) ?? "null";
|
||||
const rawJsonBytes = new TextEncoder().encode(rawJson).byteLength;
|
||||
if (rawJsonBytes > MAX_RAW_PAGESPEED_BYTES) {
|
||||
failedStrategies += 1;
|
||||
const strategyResults = await Promise.all(
|
||||
STRATEGIES.map(async (strategy) => {
|
||||
const fetchedAt = Date.now();
|
||||
try {
|
||||
const raw = await fetchPageSpeedResult({
|
||||
url: sourceUrl,
|
||||
strategy,
|
||||
apiKey,
|
||||
timeoutMs,
|
||||
});
|
||||
const rawJson = JSON.stringify(raw) ?? "null";
|
||||
const rawJsonBytes = new TextEncoder().encode(rawJson).byteLength;
|
||||
if (rawJsonBytes > MAX_RAW_PAGESPEED_BYTES) {
|
||||
await ctx.runMutation(internal.pageSpeed.persistPageSpeedResult, {
|
||||
leadId: started.lead._id,
|
||||
...(started.auditId ? { auditId: started.auditId } : {}),
|
||||
runId: args.runId,
|
||||
strategy,
|
||||
status: "failed",
|
||||
sourceUrl,
|
||||
errorType: "api_error",
|
||||
errorSummary: RAW_PAGESPEED_BYTES_SUMMARY,
|
||||
fetchedAt,
|
||||
});
|
||||
|
||||
await ctx.runMutation(internal.runs.appendEventInternal, {
|
||||
runId: args.runId,
|
||||
level: "warning",
|
||||
message: `PageSpeed-Analyse für ${strategy} fehlgeschlagen.`,
|
||||
details: [
|
||||
{ label: "Strategie", value: strategy },
|
||||
{
|
||||
label: "Fehler",
|
||||
value: RAW_PAGESPEED_BYTES_SUMMARY,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
return "failed" as const;
|
||||
}
|
||||
|
||||
const rawStorageId = await ctx.storage.store(
|
||||
new Blob([rawJson], { type: "application/json" }),
|
||||
);
|
||||
const normalized = normalizePageSpeedResult({
|
||||
strategy,
|
||||
sourceUrl,
|
||||
raw,
|
||||
});
|
||||
|
||||
await ctx.runMutation(internal.pageSpeed.persistPageSpeedResult, {
|
||||
leadId: started.lead._id,
|
||||
...(started.auditId ? { auditId: started.auditId } : {}),
|
||||
runId: args.runId,
|
||||
strategy,
|
||||
status: "succeeded",
|
||||
sourceUrl,
|
||||
finalUrl: normalized.finalUrl,
|
||||
rawStorageId,
|
||||
fetchedAt,
|
||||
normalized: toPersistedPageSpeedNormalizedResult(normalized),
|
||||
});
|
||||
|
||||
await ctx.runMutation(internal.runs.appendEventInternal, {
|
||||
runId: args.runId,
|
||||
level: "info",
|
||||
message: `PageSpeed-Analyse für ${strategy} abgeschlossen.`,
|
||||
details: [{ label: "Strategie", value: strategy }],
|
||||
});
|
||||
return "succeeded" as const;
|
||||
} catch (error) {
|
||||
const { errorType, errorSummary } = classifyPageSpeedFailure(
|
||||
error,
|
||||
apiKeyRaw,
|
||||
);
|
||||
|
||||
await ctx.runMutation(internal.pageSpeed.persistPageSpeedResult, {
|
||||
leadId: started.lead._id,
|
||||
...(started.auditId ? { auditId: started.auditId } : {}),
|
||||
@@ -205,8 +269,8 @@ export const processPageSpeedAudit = internalAction({
|
||||
strategy,
|
||||
status: "failed",
|
||||
sourceUrl,
|
||||
errorType: "api_error",
|
||||
errorSummary: RAW_PAGESPEED_BYTES_SUMMARY,
|
||||
errorType,
|
||||
errorSummary,
|
||||
fetchedAt,
|
||||
});
|
||||
|
||||
@@ -216,75 +280,18 @@ export const processPageSpeedAudit = internalAction({
|
||||
message: `PageSpeed-Analyse für ${strategy} fehlgeschlagen.`,
|
||||
details: [
|
||||
{ label: "Strategie", value: strategy },
|
||||
{
|
||||
label: "Fehler",
|
||||
value: RAW_PAGESPEED_BYTES_SUMMARY,
|
||||
},
|
||||
{ label: "Fehler", value: errorSummary },
|
||||
],
|
||||
});
|
||||
|
||||
continue;
|
||||
return "failed" as const;
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
const rawStorageId = await ctx.storage.store(
|
||||
new Blob([rawJson], { type: "application/json" }),
|
||||
);
|
||||
const normalized = normalizePageSpeedResult({
|
||||
strategy,
|
||||
sourceUrl,
|
||||
raw,
|
||||
});
|
||||
|
||||
await ctx.runMutation(internal.pageSpeed.persistPageSpeedResult, {
|
||||
leadId: started.lead._id,
|
||||
...(started.auditId ? { auditId: started.auditId } : {}),
|
||||
runId: args.runId,
|
||||
strategy,
|
||||
status: "succeeded",
|
||||
sourceUrl,
|
||||
finalUrl: normalized.finalUrl,
|
||||
rawStorageId,
|
||||
fetchedAt,
|
||||
normalized: toPersistedPageSpeedNormalizedResult(normalized),
|
||||
});
|
||||
|
||||
await ctx.runMutation(internal.runs.appendEventInternal, {
|
||||
runId: args.runId,
|
||||
level: "info",
|
||||
message: `PageSpeed-Analyse für ${strategy} abgeschlossen.`,
|
||||
details: [{ label: "Strategie", value: strategy }],
|
||||
});
|
||||
succeededStrategies += 1;
|
||||
} catch (error) {
|
||||
const { errorType, errorSummary } = classifyPageSpeedFailure(
|
||||
error,
|
||||
apiKeyRaw,
|
||||
);
|
||||
failedStrategies += 1;
|
||||
|
||||
await ctx.runMutation(internal.pageSpeed.persistPageSpeedResult, {
|
||||
leadId: started.lead._id,
|
||||
...(started.auditId ? { auditId: started.auditId } : {}),
|
||||
runId: args.runId,
|
||||
strategy,
|
||||
status: "failed",
|
||||
sourceUrl,
|
||||
errorType,
|
||||
errorSummary,
|
||||
fetchedAt,
|
||||
});
|
||||
|
||||
await ctx.runMutation(internal.runs.appendEventInternal, {
|
||||
runId: args.runId,
|
||||
level: "warning",
|
||||
message: `PageSpeed-Analyse für ${strategy} fehlgeschlagen.`,
|
||||
details: [
|
||||
{ label: "Strategie", value: strategy },
|
||||
{ label: "Fehler", value: errorSummary },
|
||||
],
|
||||
});
|
||||
}
|
||||
}
|
||||
succeededStrategies = strategyResults.filter(
|
||||
(result) => result === "succeeded",
|
||||
).length;
|
||||
failedStrategies = strategyResults.length - succeededStrategies;
|
||||
|
||||
const status = succeededStrategies > 0 ? "succeeded" : "failed";
|
||||
const errors = failedStrategies;
|
||||
@@ -298,7 +305,9 @@ export const processPageSpeedAudit = internalAction({
|
||||
: undefined,
|
||||
});
|
||||
|
||||
await queueAuditGenerationAfterPageSpeed(ctx, args.runId, started);
|
||||
if (args.queueGeneration !== false) {
|
||||
await queueAuditGenerationAfterPageSpeed(ctx, args.runId, started);
|
||||
}
|
||||
|
||||
return args.runId;
|
||||
} catch (error) {
|
||||
@@ -316,8 +325,34 @@ export const processPageSpeedAudit = internalAction({
|
||||
message: "PageSpeed-Analyse fehlgeschlagen.",
|
||||
details: [{ label: "Fehler", value: errorSummary, source: "pagespeed_action" }],
|
||||
});
|
||||
await queueAuditGenerationAfterPageSpeed(ctx, args.runId, started);
|
||||
if (args.queueGeneration !== false) {
|
||||
await queueAuditGenerationAfterPageSpeed(ctx, args.runId, started);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
export const processPageSpeedAuditForWorkflow = internalAction({
|
||||
args: {
|
||||
runId: v.id("agentRuns"),
|
||||
},
|
||||
handler: async (ctx, args): Promise<Id<"agentRuns">> => {
|
||||
const result = await ctx.runAction(
|
||||
internal.pageSpeedAction.processPageSpeedAudit,
|
||||
{
|
||||
runId: args.runId,
|
||||
queueGeneration: false,
|
||||
},
|
||||
);
|
||||
const run = await ctx.runQuery(internal.runs.getAuditRunForWorkflowInternal, {
|
||||
id: args.runId,
|
||||
});
|
||||
|
||||
if (!result || run?.status === "failed" || run?.status === "canceled") {
|
||||
throw new Error("PageSpeed-Analyse konnte nicht abgeschlossen werden.");
|
||||
}
|
||||
|
||||
return args.runId;
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user