Skip to content

Commit

Permalink
Merge pull request #1400 from input-output-hk/fix/release-query-runne…
Browse files Browse the repository at this point in the history
…r-once

fix: typeorm asset provider release query runner once
  • Loading branch information
mkazlauskas authored Jul 31, 2024
2 parents 2181645 + 6cb9d61 commit 7f12b0d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ export class TypeOrmNftMetadataService extends TypeormService implements NftMeta
}

async getNftMetadata(assetInfo: AssetPolicyIdAndName): Promise<Asset.NftMetadata | null> {
return this.withDataSource(async (dataSource) => {
const queryRunner = dataSource.createQueryRunner();
return this.getNftMetadataWith(assetInfo, queryRunner);
});
return this.withQueryRunner((queryRunner) => this.getNftMetadataWith(assetInfo, queryRunner));
}

async getNftMetadataWith(assetInfo: AssetPolicyIdAndName, queryRunner: QueryRunner) {
Expand All @@ -28,8 +25,6 @@ export class TypeOrmNftMetadataService extends TypeormService implements NftMeta
} catch (error) {
this.logger.error(error);
asset = null;
} finally {
await queryRunner.release();
}

if (!asset) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,15 @@ export class TypeormAssetProvider extends TypeormProvider implements AssetProvid
}

async getAsset({ assetId, extraData }: GetAssetArgs): Promise<Asset.AssetInfo> {
return this.withDataSource(async (dataSource) => {
const queryRunner = dataSource.createQueryRunner();
try {
const assetInfo = await this.#getAssetInfo(assetId, queryRunner);

if (extraData?.nftMetadata) assetInfo.nftMetadata = await this.#getNftMetadata(assetInfo, queryRunner);
if (extraData?.tokenMetadata) {
assetInfo.tokenMetadata = (await this.#fetchTokenMetadataList([assetId]))[0];
}
return this.withQueryRunner(async (queryRunner) => {
const assetInfo = await this.#getAssetInfo(assetId, queryRunner);

return assetInfo;
} finally {
await queryRunner.release();
if (extraData?.nftMetadata) assetInfo.nftMetadata = await this.#getNftMetadata(assetInfo, queryRunner);
if (extraData?.tokenMetadata) {
assetInfo.tokenMetadata = (await this.#fetchTokenMetadataList([assetId]))[0];
}

return assetInfo;
});
}

Expand All @@ -63,31 +58,26 @@ export class TypeormAssetProvider extends TypeormProvider implements AssetProvid
);
}

return this.withDataSource(async (dataSource) => {
const queryRunner = dataSource.createQueryRunner();
try {
const assetInfoList = await Promise.all(assetIds.map((assetId) => this.#getAssetInfo(assetId, queryRunner)));

if (extraData?.nftMetadata) {
await Promise.all(
assetInfoList.map(async (assetInfo) => {
assetInfo.nftMetadata = await this.#getNftMetadata(assetInfo, queryRunner);
})
);
}
return this.withQueryRunner(async (queryRunner) => {
const assetInfoList = await Promise.all(assetIds.map((assetId) => this.#getAssetInfo(assetId, queryRunner)));

if (extraData?.tokenMetadata) {
const tokenMetadataList = await this.#fetchTokenMetadataList(assetIds);
if (extraData?.nftMetadata) {
await Promise.all(
assetInfoList.map(async (assetInfo) => {
assetInfo.nftMetadata = await this.#getNftMetadata(assetInfo, queryRunner);
})
);
}

for (const [index, assetInfo] of assetInfoList.entries()) {
assetInfo.tokenMetadata = tokenMetadataList[index];
}
}
if (extraData?.tokenMetadata) {
const tokenMetadataList = await this.#fetchTokenMetadataList(assetIds);

return assetInfoList;
} finally {
await queryRunner.release();
for (const [index, assetInfo] of assetInfoList.entries()) {
assetInfo.tokenMetadata = tokenMetadataList[index];
}
}

return assetInfoList;
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BehaviorSubject, Observable, Subscription, filter, firstValueFrom } from 'rxjs';
import { DataSource } from 'typeorm';
import { DataSource, QueryRunner } from 'typeorm';
import { Logger } from 'ts-log';
import { PgConnectionConfig } from '@cardano-sdk/projection-typeorm';
import { RunnableModule, isNotNil } from '@cardano-sdk/util';
Expand Down Expand Up @@ -50,6 +50,24 @@ export abstract class TypeormService extends RunnableModule {
}
}

async withQueryRunner<T>(callback: (queryRunner: QueryRunner) => Promise<T>): Promise<T> {
return this.withDataSource(async (dataSource) => {
const queryRunner = dataSource.createQueryRunner();
try {
const result = await callback(queryRunner);
await queryRunner.release();
return result;
} catch (error) {
try {
await queryRunner.release();
} catch (releaseError) {
this.logger.warn(releaseError);
}
throw error;
}
});
}

async initializeImpl() {
return Promise.resolve();
}
Expand Down

0 comments on commit 7f12b0d

Please sign in to comment.