@eleven-am/transcoder
Version:
High-performance HLS transcoding library with hardware acceleration, intelligent client management, and distributed processing support for Node.js
252 lines • 8.92 kB
JavaScript
"use strict";
/*
* @eleven-am/transcoder
* Copyright (C) 2025 Roy OSSAI
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.JobProcessor = void 0;
const os = __importStar(require("os"));
const fp_1 = require("@eleven-am/fp");
const types_1 = require("./types");
const utils_1 = require("./utils");
/**
* JobProcessor - Manages transcoding job execution with concurrency control
*
* This class provides a queue-based system for managing FFmpeg transcoding jobs,
* ensuring system resources aren't overwhelmed by limiting concurrent executions.
*/
class JobProcessor extends utils_1.ExtendedEventEmitter {
constructor(maxConcurrentJobs = Math.max(1, os.cpus().length - 1), maxQueueSize = 1000) {
super();
this.maxConcurrentJobs = maxConcurrentJobs;
this.maxQueueSize = maxQueueSize;
this.queue = [];
this.activeJobs = 0;
this.disposed = false;
this.lastCpuInfo = os.cpus();
this.lastMeasureTime = Date.now();
}
/**
* Submit a job for processing
* @param job The transcode job to process
* @returns A promise that resolves when the job completes
*/
submitJob(job) {
if (this.disposed) {
return fp_1.TaskEither.error((0, fp_1.createBadRequestError)('JobProcessor has been disposed'));
}
if (this.queue.length >= this.maxQueueSize) {
this.emit('queue:full', { size: this.queue.length,
maxSize: this.maxQueueSize });
return fp_1.TaskEither.error((0, fp_1.createBadRequestError)('Job queue is full'));
}
return fp_1.TaskEither.tryCatch(() => new Promise((resolve, reject) => {
this.queue.push({ job,
resolve,
reject });
this.processNextJob();
}), 'Failed to submit job');
}
/**
* Get current queue status
*/
getStatus() {
return {
queueLength: this.queue.length,
activeJobs: this.activeJobs,
maxConcurrentJobs: this.maxConcurrentJobs,
isProcessing: this.activeJobs > 0,
};
}
/**
* Update the maximum number of concurrent jobs
* @param newMax New maximum concurrent jobs
*/
setMaxConcurrentJobs(newMax) {
const oldMax = this.maxConcurrentJobs;
this.maxConcurrentJobs = Math.max(1, newMax);
if (this.maxConcurrentJobs > oldMax) {
// Process more jobs if we increased the limit
for (let i = oldMax; i < this.maxConcurrentJobs && i < this.queue.length; i++) {
this.processNextJob();
}
}
this.emit('concurrency:changed', { current: this.activeJobs,
max: this.maxConcurrentJobs });
}
/**
* Clear all pending jobs from the queue
*/
clearQueue() {
const clearedJobs = this.queue.splice(0);
clearedJobs.forEach(({ reject }) => {
reject(new Error('Queue cleared'));
});
}
/**
* Dispose of the job processor
*/
dispose() {
this.disposed = true;
this.clearQueue();
}
/**
* Process the next job in the queue if possible
*/
processNextJob() {
if (this.disposed || this.activeJobs >= this.maxConcurrentJobs || this.queue.length === 0) {
return;
}
const item = this.queue.shift();
if (!item) {
return;
}
this.activeJobs++;
this.executeJob(item);
}
/**
* Execute a single job
*/
executeJob(item) {
const { job, resolve, reject } = item;
this.emit('job:started', job);
job.status = types_1.TranscodeStatus.PROCESSING;
const _startTime = Date.now();
// Define cleanup function first
let onEnd;
let onError;
const cleanup = () => {
job.process.removeListener('end', onEnd);
job.process.removeListener('error', onError);
};
// Set up event handlers for the FFmpeg process
onEnd = () => {
cleanup();
job.status = types_1.TranscodeStatus.PROCESSED;
this.emit('job:completed', job);
resolve();
this.jobCompleted();
};
onError = (error) => {
cleanup();
job.status = types_1.TranscodeStatus.ERROR;
this.emit('job:failed', { job,
error });
reject(error);
this.jobCompleted();
};
// Attach handlers and run the command
job.process.once('end', onEnd);
job.process.once('error', onError);
try {
job.process.run();
}
catch (error) {
onError(error);
}
}
/**
* Handle job completion and process next job
*/
jobCompleted() {
this.activeJobs--;
this.processNextJob();
}
/**
* Get a system load for dynamic concurrency adjustment
* @returns System load percentage (0-100)
*/
getSystemLoad() {
const currentCpus = os.cpus();
const currentTime = Date.now();
if (currentTime - this.lastMeasureTime < 100) {
return 0;
}
let totalDiff = 0;
let idleDiff = 0;
for (let i = 0; i < currentCpus.length; i++) {
const lastCpu = this.lastCpuInfo[i];
const currentCpu = currentCpus[i];
const lastTotal = Object.values(lastCpu.times).reduce((a, b) => a + b, 0);
const currentTotal = Object.values(currentCpu.times).reduce((a, b) => a + b, 0);
totalDiff += currentTotal - lastTotal;
idleDiff += currentCpu.times.idle - lastCpu.times.idle;
}
this.lastCpuInfo = currentCpus;
this.lastMeasureTime = currentTime;
const usage = totalDiff > 0 ? (1 - idleDiff / totalDiff) * 100 : 0;
return Math.min(100, Math.max(0, usage));
}
/**
* Dynamically adjust concurrency based on system load
* Call this periodically if you want automatic adjustment
*/
adjustConcurrencyBasedOnLoad() {
const load = this.getSystemLoad();
const cpuCount = os.cpus().length;
let targetConcurrency;
if (load > 80) {
targetConcurrency = Math.max(1, Math.floor(cpuCount * 0.25));
}
else if (load > 60) {
targetConcurrency = Math.max(1, Math.floor(cpuCount * 0.5));
}
else if (load > 40) {
targetConcurrency = Math.max(1, Math.floor(cpuCount * 0.75));
}
else {
targetConcurrency = Math.max(1, cpuCount - 1);
}
if (targetConcurrency !== this.maxConcurrentJobs) {
this.setMaxConcurrentJobs(targetConcurrency);
}
}
}
exports.JobProcessor = JobProcessor;
//# sourceMappingURL=jobProcessor.js.map