From 6f3d80e73ca7bfa0022f59b9475f261b069cb668 Mon Sep 17 00:00:00 2001 From: Yuxuan Zhang Date: Wed, 3 Jan 2024 06:32:47 -0500 Subject: [PATCH] remove unneeded file --- src/node/build/render-worker.ts | 88 --------------------------------- 1 file changed, 88 deletions(-) delete mode 100644 src/node/build/render-worker.ts diff --git a/src/node/build/render-worker.ts b/src/node/build/render-worker.ts deleted file mode 100644 index ab799093..00000000 --- a/src/node/build/render-worker.ts +++ /dev/null @@ -1,88 +0,0 @@ -import { Worker, workerData, isMainThread, parentPort } from 'worker_threads' -import { type UpdateHandle } from '../utils/task' -import { type RenderPageContext } from './render' - -type TaskAllocator = () => Promise - -import RpcContext from 'rpc-magic-proxy' - -export default async function cluster( - entryPath: string, - context: RenderPageContext, - pages: string[], - update: UpdateHandle -) { - // - Each render worker could consume up to 150% of a CPU core. - // - One extra core is allocated to the main thread. - // - Excess worker will cause too much RPC workload for main thread, - // therefore harm the overall performance. - const concurrency = Math.round( - Math.max((context.config.concurrency - 1) / 1.5, 1) - ) - - const num_tasks = pages.length - - const pageAlloc: TaskAllocator = async () => { - const page = pages.shift() - if (page) update(num_tasks - pages.length, num_tasks) - return page - } - - const tasks = [] - - const ctx = new RpcContext() - const workerData = await ctx.serialize({ - concurrency, - entryPath, - pageAlloc, - context, - workload: 'render' - }) - - for (let _ = 0; _ < concurrency; _++) { - const worker = new Worker(new URL(import.meta.url), { workerData }) - ctx.bind(worker) - tasks.push( - new Promise((res, rej) => - worker.once('exit', (code) => { - if (code === 0) res(code) - else rej() - }) - ) - ) - } - - await Promise.all(tasks) -} - -async function renderWorker() { - const ctx = new RpcContext(parentPort!) - try { - const { - entryPath, - pageAlloc, - context - }: { - entryPath: string - pageAlloc: TaskAllocator - context: RenderPageContext - } = ctx.deserialize(workerData) - const { renderPage } = await import('./render') - const { render } = await import(entryPath) - async function executor() { - while (true) { - const page = await pageAlloc() - if (!page) break - await renderPage(render, page, context) - } - } - const concurrency = Math.max(context.config.concurrency, 1) - await Promise.all(Array.from({ length: concurrency }, () => executor())) - } catch (e) { - console.error(e) - } finally { - ctx.reset() - } -} - -if (!isMainThread && workerData?.workload === 'render') renderWorker()