Files
tunarr/server/src/tasks/TaskQueue.ts
Christian Benincasa f52df44ef0 feat!: add support for Jellyfin media (#633)
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
2024-08-22 07:41:33 -04:00

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,
});