Skip to content

Commit

Permalink
Better solution for InstanceSettings user home dir
Browse files Browse the repository at this point in the history
  • Loading branch information
ivov committed Apr 26, 2024
1 parent c8bf11b commit b61f8c1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
2 changes: 1 addition & 1 deletion packages/cli/src/benchmark/benchmark.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ To create a benchmark suite:

- [using "Respond immediately" mode](./suites/workflows/001-1.json)
- [using "When last node finishes" mode](./suites/workflows/001-2.json)
- [using "Respond to Webhook" node mode](./suites/workflows/001-3.json)
- [using "Respond to Webhook node" mode](./suites/workflows/001-3.json)

<!-- /BENCHMARK_SUITES_LIST -->
30 changes: 15 additions & 15 deletions packages/cli/src/benchmark/lib/hooks/n8nDir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,32 @@ import path from 'node:path';
import { mkdirSync, mkdtempSync, writeFileSync } from 'node:fs';
import Container from 'typedi';
import { InstanceSettings } from 'n8n-core';
import { Logger } from '@/Logger';

/**
* Create a temp .n8n user dir for benchmarking.
*/
export function n8nDir() {
const baseDirPath = path.join(tmpdir(), 'n8n-benchmarks/');
const tempBaseDir = path.join(tmpdir(), 'n8n-benchmarks/');

mkdirSync(baseDirPath, { recursive: true });
mkdirSync(tempBaseDir, { recursive: true });

const userDir = mkdtempSync(baseDirPath);
const tempUserHomeDir = mkdtempSync(tempBaseDir);

const n8nDirPath = path.join(userDir, '.n8n');
const tempN8nDir = path.join(tempUserHomeDir, '.n8n');

mkdirSync(n8nDirPath);
mkdirSync(tempN8nDir);

writeFileSync(
path.join(n8nDirPath, 'config'),
path.join(tempN8nDir, 'config'),
JSON.stringify({ encryptionKey: 'temp_encryption_key', instanceId: 'temp-123' }),
'utf-8',
);

// @TODO: Find better approach than overriding like this
// Setting N8N_USER_FOLDER has no effect
const instanceSettings = Container.get(InstanceSettings);
instanceSettings.n8nFolder = n8nDirPath;
Container.set(InstanceSettings, instanceSettings);
process.env.N8N_USER_FOLDER = tempUserHomeDir;

Container.get(Logger).info(
`[Benchmarking] Temp .n8n dir location: ${instanceSettings.n8nFolder}`,
);
/**
* `typedi` has already instantiated `InstanceSettings` using the default user home,
* so re-instantiate it to ensure it picks up the temp user home dir path.
*/
Container.set(InstanceSettings, new InstanceSettings());
}
2 changes: 1 addition & 1 deletion packages/core/src/InstanceSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class InstanceSettings {
private readonly userHome = this.getUserHome();

/** The path to the n8n folder in which all n8n related data gets saved */
n8nFolder = path.join(this.userHome, '.n8n'); // @TODO: Solution that keeps this readonly
readonly n8nFolder = path.join(this.userHome, '.n8n'); // @TODO: Solution that keeps this readonly

/** The path to the folder where all generated static assets are copied to */
readonly staticCacheDir = path.join(this.userHome, '.cache/n8n/public');
Expand Down

0 comments on commit b61f8c1

Please sign in to comment.