refactor(compute-core): centralize runtime cpu config exports
This commit is contained in:
parent
51fbfa117d
commit
754fbee227
7 changed files with 20 additions and 31 deletions
|
|
@ -15,6 +15,7 @@
|
||||||
".": "./src/index.ts",
|
".": "./src/index.ts",
|
||||||
"./contracts": "./src/contracts.ts",
|
"./contracts": "./src/contracts.ts",
|
||||||
"./local-runtime": "./src/local-runtime.ts",
|
"./local-runtime": "./src/local-runtime.ts",
|
||||||
|
"./runtime": "./src/runtime/index.ts",
|
||||||
"./runtime/timeout-config": "./src/runtime/timeout-config.ts",
|
"./runtime/timeout-config": "./src/runtime/timeout-config.ts",
|
||||||
"./pdf-layout/*": "./src/pdf-layout/*.ts"
|
"./pdf-layout/*": "./src/pdf-layout/*.ts"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
|
|
||||||
export const DOCSTORE_DIR = process.env.COMPUTE_DOCSTORE_DIR?.trim() || path.join(process.cwd(), 'docstore');
|
export const DOCSTORE_DIR = path.join(process.cwd(), 'docstore');
|
||||||
|
|
||||||
export function getDocstoreDir(): string {
|
export function getDocstoreDir(): string {
|
||||||
return DOCSTORE_DIR;
|
return DOCSTORE_DIR;
|
||||||
|
|
|
||||||
15
compute/core/src/runtime/index.ts
Normal file
15
compute/core/src/runtime/index.ts
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
export {
|
||||||
|
getComputeJobConcurrency,
|
||||||
|
getAvailableCpuCores,
|
||||||
|
getOnnxThreadsPerJob,
|
||||||
|
} from './cpu-budget';
|
||||||
|
|
||||||
|
export {
|
||||||
|
getComputeTimeoutConfig,
|
||||||
|
getWorkerClientWaitTimeoutMs,
|
||||||
|
withTimeout,
|
||||||
|
withIdleTimeoutAndHardCap,
|
||||||
|
type ComputeTimeoutConfig,
|
||||||
|
type ComputeOperationKind,
|
||||||
|
type IdleTimeoutAndHardCapInput,
|
||||||
|
} from './timeout-config';
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import { getComputeJobConcurrency } from '@/lib/server/compute/cpu-budget';
|
import { getComputeJobConcurrency } from '@openreader/compute-core/runtime';
|
||||||
|
|
||||||
export class ConcurrencyLimiter {
|
export class ConcurrencyLimiter {
|
||||||
private readonly maxInFlight: number;
|
private readonly maxInFlight: number;
|
||||||
|
|
|
||||||
|
|
@ -1,28 +0,0 @@
|
||||||
import os from 'node:os';
|
|
||||||
|
|
||||||
function readPositiveInt(name: string, fallback: number): number {
|
|
||||||
const raw = process.env[name]?.trim();
|
|
||||||
if (!raw) return fallback;
|
|
||||||
const parsed = Number(raw);
|
|
||||||
if (!Number.isFinite(parsed) || parsed <= 0) return fallback;
|
|
||||||
return Math.floor(parsed);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getComputeJobConcurrency(): number {
|
|
||||||
return readPositiveInt('COMPUTE_JOB_CONCURRENCY', 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getAvailableCpuCores(): number {
|
|
||||||
if (typeof os.availableParallelism === 'function') {
|
|
||||||
const value = os.availableParallelism();
|
|
||||||
if (Number.isFinite(value) && value >= 1) return Math.floor(value);
|
|
||||||
}
|
|
||||||
const fallback = os.cpus().length;
|
|
||||||
return Number.isFinite(fallback) && fallback >= 1 ? Math.floor(fallback) : 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getOnnxThreadsPerJob(): number {
|
|
||||||
const concurrency = getComputeJobConcurrency();
|
|
||||||
const usableCores = Math.max(1, getAvailableCpuCores() - 1);
|
|
||||||
return Math.max(1, Math.floor(usableCores / concurrency));
|
|
||||||
}
|
|
||||||
|
|
@ -8,7 +8,7 @@ import { Tokenizer } from '@huggingface/tokenizers';
|
||||||
import JSZip from 'jszip';
|
import JSZip from 'jszip';
|
||||||
import type { TTSAudioBuffer, TTSAudioBytes, TTSSentenceAlignment } from '@/types/tts';
|
import type { TTSAudioBuffer, TTSAudioBytes, TTSSentenceAlignment } from '@/types/tts';
|
||||||
import { getFFmpegPath } from '@/lib/server/audiobooks/ffmpeg-bin';
|
import { getFFmpegPath } from '@/lib/server/audiobooks/ffmpeg-bin';
|
||||||
import { getOnnxThreadsPerJob } from '@/lib/server/compute/cpu-budget';
|
import { getOnnxThreadsPerJob } from '@openreader/compute-core/runtime';
|
||||||
import {
|
import {
|
||||||
mapWordsToSentenceOffsets,
|
mapWordsToSentenceOffsets,
|
||||||
type WhisperWord,
|
type WhisperWord,
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@
|
||||||
"@openreader/compute-core": ["./compute/core/src/index.ts"],
|
"@openreader/compute-core": ["./compute/core/src/index.ts"],
|
||||||
"@openreader/compute-core/contracts": ["./compute/core/src/contracts.ts"],
|
"@openreader/compute-core/contracts": ["./compute/core/src/contracts.ts"],
|
||||||
"@openreader/compute-core/local-runtime": ["./compute/core/src/local-runtime.ts"],
|
"@openreader/compute-core/local-runtime": ["./compute/core/src/local-runtime.ts"],
|
||||||
|
"@openreader/compute-core/runtime": ["./compute/core/src/runtime/index.ts"],
|
||||||
"@openreader/compute-core/runtime/timeout-config": ["./compute/core/src/runtime/timeout-config.ts"],
|
"@openreader/compute-core/runtime/timeout-config": ["./compute/core/src/runtime/timeout-config.ts"],
|
||||||
"@openreader/compute-core/pdf-layout/*": ["./compute/core/src/pdf-layout/*"]
|
"@openreader/compute-core/pdf-layout/*": ["./compute/core/src/pdf-layout/*"]
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue