Skip to content

Commit

Permalink
refactor(core): Suggestions for #8082
Browse files Browse the repository at this point in the history
  • Loading branch information
ivov committed Dec 19, 2023
1 parent 5e632ca commit bf2bd22
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 22 deletions.
9 changes: 1 addition & 8 deletions packages/cli/src/WorkflowExecuteAdditionalData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,14 +410,7 @@ function hookFunctionsSave(parentProcessMode?: string): IWorkflowExecuteHooks {
workflowId: this.workflowData.id,
});

try {
await restoreBinaryDataId(fullRunData, this.executionId, this.mode);
} catch (e) {
logger.debug('Failed to restore binary data ID', {
executionId: this.executionId,
mode: this.mode,
});
}
await restoreBinaryDataId(fullRunData, this.executionId, this.mode);

const isManualMode = [this.mode, parentProcessMode].includes('manual');

Expand Down
41 changes: 27 additions & 14 deletions packages/cli/src/executionLifecycleHooks/restoreBinaryDataId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { BinaryDataService } from 'n8n-core';
import type { IRun, WorkflowExecuteMode } from 'n8n-workflow';
import type { BinaryData } from 'n8n-core';
import config from '@/config';
import { Logger } from '@/Logger';

/**
* Whenever the execution ID is not available to the binary data service at the
Expand Down Expand Up @@ -32,28 +33,40 @@ export async function restoreBinaryDataId(
return;
}

const { runData } = run.data.resultData;
try {
const { runData } = run.data.resultData;

const promises = Object.keys(runData).map(async (nodeName) => {
const binaryDataId = runData[nodeName]?.[0]?.data?.main?.[0]?.[0]?.binary?.data?.id;
const promises = Object.keys(runData).map(async (nodeName) => {
const binaryDataId = runData[nodeName]?.[0]?.data?.main?.[0]?.[0]?.binary?.data?.id;

if (!binaryDataId) return;
if (!binaryDataId) return;

const [mode, fileId] = binaryDataId.split(':') as [BinaryData.StoredMode, string];
const [mode, fileId] = binaryDataId.split(':') as [BinaryData.StoredMode, string];

const isMissingExecutionId = fileId.includes('/temp/');
const isMissingExecutionId = fileId.includes('/temp/');

if (!isMissingExecutionId) return;
if (!isMissingExecutionId) return;

const correctFileId = fileId.replace('temp', executionId);
const correctFileId = fileId.replace('temp', executionId);

await Container.get(BinaryDataService).rename(fileId, correctFileId);
await Container.get(BinaryDataService).rename(fileId, correctFileId);

const correctBinaryDataId = `${mode}:${correctFileId}`;
const correctBinaryDataId = `${mode}:${correctFileId}`;

// @ts-expect-error Validated at the top
run.data.resultData.runData[nodeName][0].data.main[0][0].binary.data.id = correctBinaryDataId;
});
// @ts-expect-error Validated at the top
run.data.resultData.runData[nodeName][0].data.main[0][0].binary.data.id = correctBinaryDataId;
});

await Promise.all(promises);
await Promise.all(promises);
} catch (e) {
const error = e instanceof Error ? e : new Error(`${e}`);
const logger = Container.get(Logger);

if (error.message.includes('ENOENT')) {
logger.warn('Failed to restore binary data ID - No such file or dir', { executionId, error });
return;
}

logger.warn('Failed to restore binary data ID - Unknown error', { executionId, error });
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,28 @@ for (const mode of ['filesystem-v2', 's3'] as const) {

expect(binaryDataService.rename).not.toHaveBeenCalled();
});

it('should ignore error thrown on renaming', async () => {
const workflowId = '6HYhhKmJch2cYxGj';
const executionId = 'temp';
const binaryDataFileUuid = 'a5c3f1ed-9d59-4155-bc68-9a370b3c51f6';

const incorrectFileId = `workflows/${workflowId}/executions/temp/binary_data/${binaryDataFileUuid}`;

const run = toIRun({
binary: {
data: { id: `s3:${incorrectFileId}` },
},
});

binaryDataService.rename.mockRejectedValueOnce(new Error('ENOENT'));

const promise = restoreBinaryDataId(run, executionId, 'webhook');

await expect(promise).resolves.not.toThrow();

expect(binaryDataService.rename).toHaveBeenCalled();
});
});
}

Expand Down

0 comments on commit bf2bd22

Please sign in to comment.