Skip to content

Commit

Permalink
Reuse tmp dir for ipfs based projects (#2551)
Browse files Browse the repository at this point in the history
* Reuse tmp dir for ipfs based projects

* Update changelogs
  • Loading branch information
stwiname authored Sep 5, 2024
1 parent b3dbaf9 commit 3dbdc3d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
2 changes: 2 additions & 0 deletions packages/common/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Changed
- Expose CID on IPFS Reader (#2551)

## [5.1.1] - 2024-08-14
### Added
Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/project/readers/ipfs-reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class IPFSReader implements Reader {
private ipfs: IPFSHTTPClientLite;
private cache: Record<string, Promise<string>> = {};

constructor(private readonly cid: string, gateway?: string) {
constructor(readonly cid: string, gateway?: string) {
if (!CIDv0.test(cid) && !CIDv1.test(cid)) {
throw new Error('IPFS project path CID is not valid');
}
Expand Down
2 changes: 2 additions & 0 deletions packages/node-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Changed
- Reuse the same temp dir when project is from IPFS (#2551)

## [14.1.3] - 2024-08-30
### Fixed
Expand Down
26 changes: 18 additions & 8 deletions packages/node-core/src/configure/configure.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Copyright 2020-2024 SubQuery Pte Ltd authors & contributors
// SPDX-License-Identifier: GPL-3.0

import {handleCreateSubqueryProjectError, LocalReader, makeTempDir, ReaderFactory} from '@subql/common';
import fs from 'fs';
import os from 'os';
import path from 'path';
import {handleCreateSubqueryProjectError, IPFSReader, LocalReader, makeTempDir, ReaderFactory} from '@subql/common';
import {IEndpointConfig, Reader} from '@subql/types-core';
import {camelCase, isNil, omitBy} from 'lodash';
import {ISubqueryProject} from '../indexer';
Expand Down Expand Up @@ -64,13 +67,10 @@ export function yargsToIConfig(yargs: Args, nameMapping: Record<string, string>
value = [value];
}
if (Array.isArray(value)) {
value = value.reduce(
(acc, endpoint, index) => {
acc[endpoint] = endpointConfig[index] ?? {};
return acc;
},
{} as Record<string, IEndpointConfig>
);
value = value.reduce((acc, endpoint, index) => {
acc[endpoint] = endpointConfig[index] ?? {};
return acc;
}, {} as Record<string, IEndpointConfig>);
}
}
if (key === 'primary-network-endpoint') {
Expand All @@ -95,8 +95,18 @@ let rootDir: string;
async function getCachedRoot(reader: Reader, configRoot?: string): Promise<string> {
if (reader instanceof LocalReader) return reader.root;

// Case for in workers when the parent has decided the directory
if (configRoot) return configRoot;

// Allows reusing the same directory on restarts when project is run from ipfs, this can stop duplicating files in the tmp dir
if (reader instanceof IPFSReader) {
rootDir = path.resolve(os.tmpdir(), reader.cid);
if (!fs.existsSync(rootDir)) {
await fs.promises.mkdir(rootDir);
}
return rootDir;
}

if (!rootDir) {
rootDir = await makeTempDir();
}
Expand Down

0 comments on commit 3dbdc3d

Please sign in to comment.