Skip to content

Crawlee—A web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation.

1.2 KB
Raw Blame
import { FileSystemStorageBackend } from '@crawlee/fs-storage';
import { RequestQueue } from 'crawlee';

// The request queue shared by all the parallel workers
let queue;

// The `shop-urls` queue is opened concurrently by every worker process, so it must use the
// concurrency-safe locking behavior. With `requestQueueAccess: 'shared'`, a request another worker
// is still processing is treated as a live peer's lock and is not handed out again until that lock
// expires — so two workers never scrape the same URL at once. (We point at the `./storage`
// location, which is where this shared queue lives.)
const sharedStorageBackend = new FileSystemStorageBackend({
    localDataDirectory: './storage',
    requestQueueAccess: 'shared',
});

/**
 * @param {boolean} makeFresh Whether the queue should be cleared before returning it
 * @returns The queue
 */
export async function getOrInitQueue(makeFresh = false) {
    if (queue) {
        return queue;
    }

    queue = await RequestQueue.open('shop-urls', { storageBackend: sharedStorageBackend });

    if (makeFresh) {
        await queue.drop();
        queue = await RequestQueue.open('shop-urls', { storageBackend: sharedStorageBackend });
    }

    return queue;
}