Skip to content

Commit a917b8f

Browse files
authored
Update paths for artifacts (#250)
1 parent fdee274 commit a917b8f

File tree

3 files changed

+26
-30
lines changed

3 files changed

+26
-30
lines changed

src/datastore/LMDB.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,22 @@ export class LMDBStore implements DataStore {
5151
export class LMDBStoreFactory implements DataStoreFactory {
5252
@Telemetry({ scope: 'LMDB.Global' }) private readonly telemetry!: ScopedTelemetry;
5353

54-
private readonly rootDir = pathToArtifact('lmdb');
55-
private readonly storePath = join(this.rootDir, Version);
54+
private readonly storePath: string;
5655
private readonly timeout: NodeJS.Timeout;
57-
58-
private readonly env: RootDatabase = open({
59-
path: this.storePath,
60-
maxDbs: 10, // 10 max databases
61-
mapSize: 100 * 1024 * 1024, // 100MB max size
62-
encoding: Encoding,
63-
encryptionKey: encryptionStrategy(Version),
64-
});
56+
private readonly env: RootDatabase;
6557

6658
private readonly stores = new Map<string, LMDBStore>();
6759

68-
constructor() {
60+
constructor(private readonly rootDir: string = pathToArtifact('lmdb')) {
61+
this.storePath = join(rootDir, Version);
62+
this.env = open({
63+
path: this.storePath,
64+
maxDbs: 10, // 10 max databases
65+
mapSize: 100 * 1024 * 1024, // 100MB max size
66+
encoding: Encoding,
67+
encryptionKey: encryptionStrategy(Version),
68+
});
69+
6970
this.registerLMDBGauges();
7071

7172
this.timeout = setTimeout(

src/telemetry/LoggerFactory.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ export const LogLevel: Record<LevelWithSilent, number> = {
2020
} as const;
2121

2222
export class LoggerFactory implements Closeable {
23-
private static readonly LogsDirectory = pathToArtifact('logs');
2423
private static readonly MaxFileSize = 50 * 1024 * 1024; // 50MB
2524

2625
private static readonly _instance: LoggerFactory = new LoggerFactory();
@@ -30,7 +29,10 @@ export class LoggerFactory implements Closeable {
3029
private readonly loggers = new Map<string, Logger>();
3130
private readonly interval: NodeJS.Timeout;
3231

33-
private constructor(level?: LevelWithSilent) {
32+
private constructor(
33+
private readonly logsDirectory: string = pathToArtifact('logs'),
34+
level?: LevelWithSilent,
35+
) {
3436
this.logLevel = level ?? TelemetrySettings.logLevel;
3537

3638
this.baseLogger = pino({
@@ -51,7 +53,7 @@ export class LoggerFactory implements Closeable {
5153
target: 'pino/file',
5254
options: {
5355
destination: join(
54-
LoggerFactory.LogsDirectory,
56+
logsDirectory,
5557
`${ExtensionId}-${DateTime.utc().toFormat('yyyy-MM-dd')}.log`,
5658
),
5759
mkdir: true,
@@ -72,13 +74,13 @@ export class LoggerFactory implements Closeable {
7274

7375
private async cleanOldLogs() {
7476
try {
75-
const files = await readdir(LoggerFactory.LogsDirectory);
77+
const files = await readdir(this.logsDirectory);
7678
const oneWeekAgo = DateTime.utc().minus({ weeks: 1 });
7779

7880
for (const file of files) {
7981
if (!file.endsWith('.log')) continue;
8082

81-
const filePath = join(LoggerFactory.LogsDirectory, file);
83+
const filePath = join(this.logsDirectory, file);
8284
const stats = await stat(filePath);
8385

8486
if (DateTime.fromJSDate(stats.mtime) < oneWeekAgo) {
@@ -94,12 +96,12 @@ export class LoggerFactory implements Closeable {
9496

9597
private async trimLogs() {
9698
try {
97-
const files = await readdir(LoggerFactory.LogsDirectory);
99+
const files = await readdir(this.logsDirectory);
98100

99101
for (const file of files) {
100102
if (!file.endsWith('.log')) continue;
101103

102-
const filePath = join(LoggerFactory.LogsDirectory, file);
104+
const filePath = join(this.logsDirectory, file);
103105
const stats = await stat(filePath);
104106

105107
if (stats.size > LoggerFactory.MaxFileSize) {

tst/unit/datastore/LMDB.test.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,20 @@
11
import fs from 'fs';
2-
import path from 'path';
3-
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
2+
import path, { join } from 'path';
3+
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
44
import { DataStore } from '../../../src/datastore/DataStore';
55
import { LMDBStoreFactory } from '../../../src/datastore/LMDB';
66

7-
const tstDir = 'tmp-tst';
8-
vi.mock('../../../src/utils/ArtifactsDir', () => ({
9-
pathToArtifact: vi.fn().mockImplementation((dir) => {
10-
return path.join(process.cwd(), tstDir, dir ?? '');
11-
}),
12-
}));
13-
147
describe('LMDB', () => {
158
let lmdbFactory: LMDBStoreFactory;
169
let lmdbStore: DataStore;
17-
const testDir = path.join(process.cwd(), tstDir);
10+
const testDir = join(path.join(process.cwd(), 'tmp-tst'), 'lmdb');
1811

1912
beforeEach(() => {
2013
if (!fs.existsSync(testDir)) {
2114
fs.mkdirSync(testDir, { recursive: true });
2215
}
2316

24-
lmdbFactory = new LMDBStoreFactory();
17+
lmdbFactory = new LMDBStoreFactory(testDir);
2518
lmdbStore = lmdbFactory.getOrCreate('test-store');
2619
});
2720

@@ -205,7 +198,7 @@ describe('LMDB', () => {
205198
await lmdbFactory.close();
206199

207200
// Create new instance that should load from the same files
208-
const newFactory = new LMDBStoreFactory();
201+
const newFactory = new LMDBStoreFactory(testDir);
209202
const newStore = newFactory.getOrCreate('test-store');
210203
const result = newStore.get<typeof value>(key);
211204

0 commit comments

Comments
 (0)