Skip to content

New metadata deployed task fix #1588

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

Merged
merged 2 commits into from
Jan 8, 2025
Merged
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
2 changes: 1 addition & 1 deletion apps/webapp/app/routes/api.v1.runs.$runId.metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const { action } = createActionApiRoute(
method: "PUT",
},
async ({ authentication, body, params }) => {
const result = await updateMetadataService.call(authentication.environment, params.runId, body);
const result = await updateMetadataService.call(params.runId, body, authentication.environment);

if (!result) {
return json({ error: "Task Run not found" }, { status: 404 });
Expand Down
54 changes: 44 additions & 10 deletions apps/webapp/app/services/metadata/updateMetadata.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,17 +229,21 @@ export class UpdateMetadataService extends BaseService {
}

public async call(
environment: AuthenticatedEnvironment,
runId: string,
body: UpdateMetadataRequestBody
body: UpdateMetadataRequestBody,
environment?: AuthenticatedEnvironment
) {
const runIdType = runId.startsWith("run_") ? "friendly" : "internal";

const taskRun = await this._prisma.taskRun.findFirst({
where: {
runtimeEnvironmentId: environment.id,
...(runIdType === "internal" ? { id: runId } : { friendlyId: runId }),
},
where: environment
? {
runtimeEnvironmentId: environment.id,
...(runIdType === "internal" ? { id: runId } : { friendlyId: runId }),
}
: {
...(runIdType === "internal" ? { id: runId } : { friendlyId: runId }),
},
select: {
id: true,
status: true,
Expand Down Expand Up @@ -351,6 +355,15 @@ export class UpdateMetadataService extends BaseService {
});

if (result.count === 0) {
if (this.flushLoggingEnabled) {
logger.debug(
`[UpdateMetadataService][updateRunMetadataWithOperations] Optimistic lock failed for run ${runId}`,
{
metadataVersion: run.metadataVersion,
}
);
}

// If this was our last attempt, buffer the operations and return optimistically
if (attempts === MAX_RETRIES) {
this.#ingestRunOperations(runId, operations);
Expand All @@ -363,6 +376,15 @@ export class UpdateMetadataService extends BaseService {
continue;
}

if (this.flushLoggingEnabled) {
logger.debug(
`[UpdateMetadataService][updateRunMetadataWithOperations] Updated metadata for run ${runId}`,
{
metadata: applyResults.newMetadata,
}
);
}

// Success! Return the new metadata
return applyResults.newMetadata;
}
Expand All @@ -383,10 +405,15 @@ export class UpdateMetadataService extends BaseService {
metadataPacket.data !== "{}" ||
(existingMetadata.data && metadataPacket.data !== existingMetadata.data)
) {
logger.debug(`Updating metadata directly for run`, {
metadata: metadataPacket.data,
runId,
});
if (this.flushLoggingEnabled) {
logger.debug(
`[UpdateMetadataService][updateRunMetadataDirectly] Updating metadata directly for run`,
{
metadata: metadataPacket.data,
runId,
}
);
}

// Update the metadata without version check
await this._prisma.taskRun.update({
Expand Down Expand Up @@ -416,6 +443,13 @@ export class UpdateMetadataService extends BaseService {
};
});

if (this.flushLoggingEnabled) {
logger.debug(`[UpdateMetadataService] Ingesting operations for run`, {
runId,
bufferedOperations,
});
}

const existingBufferedOperations = this._bufferedOperations.get(runId) ?? [];

this._bufferedOperations.set(runId, [...existingBufferedOperations, ...bufferedOperations]);
Expand Down
4 changes: 2 additions & 2 deletions apps/webapp/app/v3/services/finalizeTaskRun.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ export class FinalizeTaskRunService extends BaseService {
completedAt,
});

if (env && metadata) {
if (metadata) {
try {
await updateMetadataService.call(env, id, metadata);
await updateMetadataService.call(id, metadata, env);
} catch (e) {
logger.error("[FinalizeTaskRunService] Failed to update metadata", {
taskRun: id,
Expand Down
18 changes: 0 additions & 18 deletions references/nextjs-realtime/src/trigger/csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,6 @@ export const handleCSVUpload = schemaTask({
const successfulRows = results.runs.filter((r) => r.ok);
const failedRows = results.runs.filter((r) => !r.ok);

const firstSuccessfulRow = successfulRows[0];

if (firstSuccessfulRow) {
const stream = await metadata.fetchStream<string>(firstSuccessfulRow.id);

for await (const value of stream) {
logger.info(`Stream value from ${firstSuccessfulRow.id}`, { value });
}
}

return {
file,
rows,
Expand All @@ -93,14 +83,6 @@ export const handleCSVRow = schemaTask({

metadata.parent.increment("processedRows", 1).append("rowRuns", ctx.run.id);

await metadata.parent.stream(
ctx.run.id,
(async function* () {
yield "hello";
yield "world";
})()
);

return row;
},
});