Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 26 additions & 24 deletions packages/core/src/storages/storage_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,32 +70,34 @@ export class StorageManager<T extends IStorage = IStorage> {
async openStorage(idOrName?: string | null, client?: StorageClient): Promise<T> {
await this.storageOpenQueue.wait();

if (!idOrName) {
const defaultIdConfigKey = DEFAULT_ID_CONFIG_KEYS[this.name];
idOrName = this.config.get(defaultIdConfigKey) as string;
}

const cacheKey = idOrName;
let storage = this.cache.get(cacheKey);

if (!storage) {
client ??= this.config.getStorageClient();
const storageObject = await this._getOrCreateStorage(idOrName, this.name, client);
storage = new this.StorageConstructor(
{
id: storageObject.id,
name: storageObject.name,
storageObject,
client,
},
this.config,
);
this._addStorageToCache(storage);
}
try {
if (!idOrName) {
const defaultIdConfigKey = DEFAULT_ID_CONFIG_KEYS[this.name];
idOrName = this.config.get(defaultIdConfigKey) as string;
}

this.storageOpenQueue.shift();
const cacheKey = idOrName;
let storage = this.cache.get(cacheKey);

if (!storage) {
client ??= this.config.getStorageClient();
const storageObject = await this._getOrCreateStorage(idOrName, this.name, client);
storage = new this.StorageConstructor(
{
id: storageObject.id,
name: storageObject.name,
storageObject,
client,
},
this.config,
);
this._addStorageToCache(storage);
}

return storage;
return storage;
} finally {
this.storageOpenQueue.shift();
}
}

closeStorage(storage: { id: string; name?: string }): void {
Expand Down
37 changes: 37 additions & 0 deletions test/core/storages/storage_manager.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Configuration, Dataset } from '@crawlee/core';

import { MemoryStorageEmulator } from '../../shared/MemoryStorageEmulator';

const localStorageEmulator = new MemoryStorageEmulator();

beforeEach(async () => {
await localStorageEmulator.init();
});

afterAll(async () => {
await localStorageEmulator.destroy();
});

describe('StorageManager', () => {
test('failed openStorage call does not block subsequent calls (#3661)', async () => {
const goodClient = Configuration.getStorageClient();
const failingClient = {
...goodClient,
datasets: () => {
throw new Error('boom');
},
dataset: () => {
throw new Error('boom');
},
};

await expect(Dataset.open('will-fail', { storageClient: failingClient as any })).rejects.toThrow('boom');

await expect(
Promise.race([
Dataset.open('fallback'),
new Promise((_, reject) => setTimeout(() => reject(new Error('timeout')), 1000)),
]),
).resolves.toBeDefined();
});
});
Loading