Skip to content

Commit

Permalink
fix(fetch): remove open-connections from the log on failure (#7359)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidfirst authored May 4, 2023
1 parent 9f01e10 commit 43c04ce
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 25 deletions.
53 changes: 35 additions & 18 deletions src/api/scope/lib/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,48 @@ fetchOptions`,
const useCachedScope = true;
const scope: Scope = await loadScope(path, useCachedScope);
const objectList = new ObjectList();
const finishLog = () => {
const finishLog = (err?: Error) => {
const duration = new Date().getTime() - startTime;
openConnections.splice(openConnections.indexOf(currentFetch), 1);
logger.debug(`scope.fetch [${currentFetch}] completed.
const successOrErr = `${err ? 'with errors' : 'successfully'}`;
logger.debug(`scope.fetch [${currentFetch}] completed ${successOrErr}.
open connections: [${openConnections.join(', ')}]. (total ${openConnections.length}).
memory usage: ${getMemoryUsageInMB()} MB.
took: ${duration} ms.`);
};
const objectsReadableGenerator = new ObjectsReadableGenerator(scope.objects, finishLog);

try {
await fetchByType(fetchOptions, ids, clientSupportsVersionHistory, scope, objectsReadableGenerator);
} catch (err: any) {
finishLog(err);
throw err;
}

if (HooksManagerInstance) {
await HooksManagerInstance?.triggerHook(
POST_SEND_OBJECTS,
{
objectList,
scopePath: path,
ids,
scopeName: scope.scopeJson.name,
},
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
headers
);
}
logger.debug('scope.fetch returns readable');
return objectsReadableGenerator.readable;
}

async function fetchByType(
fetchOptions: FETCH_OPTIONS,
ids: string[],
clientSupportsVersionHistory: boolean,
scope: Scope,
objectsReadableGenerator: ObjectsReadableGenerator
): Promise<void> {
const shouldFetchDependencies = () => {
if (fetchOptions.includeDependencies) return true;
// backward compatible before 0.0.900
Expand Down Expand Up @@ -253,22 +286,6 @@ took: ${duration} ms.`);
default:
throw new Error(`type ${fetchOptions.type} was not implemented`);
}

if (HooksManagerInstance) {
await HooksManagerInstance?.triggerHook(
POST_SEND_OBJECTS,
{
objectList,
scopePath: path,
ids,
scopeName: scope.scopeJson.name,
},
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
headers
);
}
logger.debug('scope.fetch returns readable');
return objectsReadableGenerator.readable;
}

function bitIdsToLatest(bitIds: BitIds, lane: Lane | null) {
Expand Down
21 changes: 14 additions & 7 deletions src/scope/objects/objects-readable-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ export class ObjectsReadableGenerator {
await pMapSeries(componentsWithOptions, async (componentWithOptions) =>
this.pushComponentObjects(componentWithOptions)
);
this.closeReadable();
this.closeReadableSuccessfully();
} catch (err: any) {
this.readable.destroy(err);
this.closeReadableFailure(err);
}
}

Expand All @@ -44,9 +44,9 @@ export class ObjectsReadableGenerator {
this.push({ ref: laneToFetch.hash(), buffer: laneBuffer });
})
);
this.closeReadable();
this.closeReadableSuccessfully();
} catch (err: any) {
this.readable.destroy(err);
this.closeReadableFailure(err);
}
}

Expand All @@ -56,18 +56,25 @@ export class ObjectsReadableGenerator {
const objectItem = await this.getObjectGracefully(ref, scope);
if (objectItem) this.push(objectItem);
});
this.closeReadable();
this.closeReadableSuccessfully();
} catch (err: any) {
this.readable.destroy(err);
this.closeReadableFailure(err);
}
}

private closeReadable() {
private closeReadableSuccessfully() {
logger.debug(`ObjectsReadableGenerator, pushed ${this.pushed.length} objects`);
this.callbackOnceDone();
this.readable.push(null);
}

private closeReadableFailure(err: Error) {
logger.debug(`ObjectsReadableGenerator, pushed ${this.pushed.length} objects`);
logger.error(`ObjectsReadableGenerator, got an error`, err);
this.callbackOnceDone(err);
this.readable.destroy(err);
}

private async getObjectGracefully(ref: Ref, scope: Scope) {
try {
return await scope.getObjectItem(ref);
Expand Down

0 comments on commit 43c04ce

Please sign in to comment.