mirror of
https://github.com/chrisbenincasa/tunarr.git
synced 2026-04-18 09:03:35 -04:00
This commit includes a huge amount of changes, including support for adding Jellyfin servers as media sources and streaming content from them. These are breaking changes and touch almost every corner of the code, but also pave the way for a lot more flexibility on the backend for addinng different sources. The commit also includes performance improvements to the inline modal, lots of code cleanup, and a few bug fixes I found along the way. Fixes #24
62 lines
1.3 KiB
TypeScript
62 lines
1.3 KiB
TypeScript
import PQueue, { Options, Queue, QueueAddOptions } from 'p-queue';
|
|
import { Task } from './Task.js';
|
|
import { Maybe } from '../types/util.js';
|
|
import { Logger, LoggerFactory } from '../util/logging/LoggerFactory.js';
|
|
|
|
export class TaskQueue {
|
|
#logger: Logger;
|
|
#queue: PQueue;
|
|
|
|
constructor(
|
|
name: string,
|
|
opts: Options<
|
|
Queue<() => Promise<unknown>, QueueAddOptions>,
|
|
QueueAddOptions
|
|
> = {
|
|
concurrency: 2,
|
|
},
|
|
) {
|
|
this.#logger = LoggerFactory.child({ caller: import.meta, queue: name });
|
|
this.#queue = new PQueue({ ...opts });
|
|
}
|
|
|
|
async add<Out = unknown>(task: Task<Out>): Promise<Maybe<Out>> {
|
|
try {
|
|
this.#logger.trace('Adding task %s to queue', task.taskName);
|
|
return await this.#queue.add(
|
|
() => {
|
|
return task.run();
|
|
},
|
|
{ throwOnTimeout: true },
|
|
);
|
|
} catch (e) {
|
|
this.#logger.error(e);
|
|
return;
|
|
}
|
|
}
|
|
|
|
set concurrency(c: number) {
|
|
this.#queue.concurrency = c;
|
|
}
|
|
|
|
pause() {
|
|
this.#queue.pause();
|
|
}
|
|
|
|
resume() {
|
|
this.#queue.start();
|
|
}
|
|
}
|
|
|
|
export const PlexTaskQueue = new TaskQueue('PlexTaskQueue', {
|
|
concurrency: 2,
|
|
intervalCap: 5,
|
|
interval: 2000,
|
|
});
|
|
|
|
export const JellyfinTaskQueue = new TaskQueue('JellyfinTaskQueue', {
|
|
concurrency: 2,
|
|
intervalCap: 5,
|
|
interval: 2000,
|
|
});
|