/** * Copyright 2026 aphrody-code * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { AutonomousCrawler } from "../crawler/AutonomousCrawler.ts"; import { type CommonOptions, logger, EXIT } from "./shared.ts"; function printUsage(): void { Bun.stdout.write( `bxc crawl-worker — Run the autonomous crawler worker daemon in the background 24/7. Usage: bxc crawl-worker [options] [initial_urls...] Options: --allowed-domains Comma-separated domains to restrict crawling (e.g. "example.com,google.com") --max-depth Maximum recursion depth (default: 5) --max-requests Limit total requests to process (default: Infinity) --min-concurrency Adaptive worker-pool floor (default: 2) --max-concurrency Adaptive worker-pool ceiling (default: 20) --max-retries Retry attempts before dead-lettering (default: 3) --exclude Never enqueue URLs matching this regex (repeatable), e.g. "[?&]m_orderby=" --profile Fetch profile: static | http | fast | stealth | max (default: stealth) --queue Custom RequestQueue name (default: bxc-autonomous-crawler) --proxy-pool Comma-separated proxy URLs to rotate (e.g. "http://p1.com,http://p2.com") --no-daemon Run until queue is empty, then exit (disables 24/7 polling mode) --help, -h Print this help Examples: bxc crawl-worker --allowed-domains example.com https://example.com bxc crawl-worker --profile fast --queue my-custom-queue `, ); } export async function main( argv: string[], baseOpts: CommonOptions, cliOptions?: { exitProcess?: boolean }, ): Promise { const exitProcess = cliOptions?.exitProcess ?? process.env.NODE_ENV !== "test"; let allowedDomains: string[] | undefined; let maxDepth = 5; let maxRequests = Infinity; let minConcurrency: number | undefined; let maxConcurrency: number | undefined; let maxRetries: number | undefined; let profile: "static" | "http" | "fast" | "stealth" | "max" = "stealth"; let queueName = "bxc-autonomous-crawler"; let daemon = true; let proxyPool: string[] | undefined; const excludePatterns: string[] = []; const initialUrls: string[] = []; for (let i = 0; i < argv.length; i++) { const arg = argv[i]; if (arg === "--allowed-domains") { allowedDomains = argv[++i].split(",").map((s) => s.trim()); } else if (arg === "--max-depth") { maxDepth = parseInt(argv[++i], 10); } else if (arg === "--max-requests") { maxRequests = parseInt(argv[++i], 10); } else if (arg === "--min-concurrency") { minConcurrency = parseInt(argv[++i], 10); } else if (arg === "--max-concurrency") { maxConcurrency = parseInt(argv[++i], 10); } else if (arg === "--max-retries") { maxRetries = parseInt(argv[++i], 10); } else if (arg === "--profile") { const val = argv[++i]; if (val === "static" || val === "http" || val === "fast" || val === "stealth" || val === "max") { profile = val; } else { logger.error(`Invalid profile: ${val}. Expected static, http, fast, stealth, or max.`); if (exitProcess) process.exit(EXIT.MISUSE); throw new Error(`Invalid profile: ${val}`); } } else if (arg === "--exclude") { const motif = argv[++i]; try { new RegExp(motif); } catch { logger.error(`Invalid --exclude regex: ${motif}`); if (exitProcess) process.exit(EXIT.MISUSE); throw new Error(`Invalid --exclude regex: ${motif}`); } excludePatterns.push(motif); } else if (arg === "--queue") { queueName = argv[++i]; } else if (arg === "--proxy-pool") { proxyPool = argv[++i].split(",").map((s) => s.trim()); } else if (arg === "--no-daemon") { daemon = false; } else if (arg === "--help" || arg === "-h") { printUsage(); return; } else if (arg.startsWith("-")) { logger.error(`Unknown option: ${arg}`); printUsage(); if (exitProcess) process.exit(EXIT.MISUSE); throw new Error(`Unknown option: ${arg}`); } else { initialUrls.push(arg); } } if ( (minConcurrency !== undefined && (!Number.isInteger(minConcurrency) || minConcurrency < 1)) || (maxConcurrency !== undefined && (!Number.isInteger(maxConcurrency) || maxConcurrency < 1)) || (maxRetries !== undefined && (!Number.isInteger(maxRetries) || maxRetries < 0)) || (minConcurrency !== undefined && maxConcurrency !== undefined && minConcurrency > maxConcurrency) ) { const message = "Concurrency values must be positive integers, retries must be a non-negative integer, and --min-concurrency cannot exceed --max-concurrency."; logger.error(message); if (exitProcess) process.exit(EXIT.MISUSE); throw new Error(message); } logger.log(`[crawl-worker] Starting 24/7 worker queue="${queueName}" profile="${profile}" maxDepth=${maxDepth} concurrency=${minConcurrency ?? 2}-${maxConcurrency ?? 20} retries=${maxRetries ?? 3} daemon=${daemon}`); const crawler = new AutonomousCrawler({ requestQueueName: queueName, allowedDomains, maxDepth, maxRequests, minConcurrency, maxConcurrency, maxRetries, profile, daemon, proxyPool, excludePatterns, }); const stopHandler = () => { logger.log("\n[crawl-worker] Received termination signal. Shutting down worker gracefully..."); crawler.stop(); }; process.on("SIGINT", stopHandler); process.on("SIGTERM", stopHandler); try { await crawler.run(initialUrls); logger.log("[crawl-worker] Worker run completed successfully."); } catch (err) { logger.error(`[crawl-worker] Fatal error in crawler execution: ${err}`); if (exitProcess) process.exit(EXIT.SOFTWARE); throw err; } finally { (process as NodeJS.EventEmitter).removeListener("SIGINT", stopHandler); (process as NodeJS.EventEmitter).removeListener("SIGTERM", stopHandler); } }