mirror of
https://github.com/immich-app/immich.git
synced 2025-01-10 13:56:47 +01:00
907a95a746
* no-misused-promises * no-floating-promises * format * revert for now * remove load function * require-await * revert a few no-floating-promises changes that would cause no-misused-promises failures * format * fix a few more * fix most remaining errors * executor-queue * executor-queue.spec * remove duplicate comments by grouping rules * upgrade sveltekit and enforce rules * oops. move await * try this * just ignore for now since it's only a test * run in parallel * Update web/src/routes/admin/jobs-status/+page.svelte Co-authored-by: Michel Heusschen <59014050+michelheusschen@users.noreply.github.com> * remove Promise.resolve call * rename function * remove unnecessary warning silencing * make handleError sync * fix new errors from recently merged PR to main * extract method * use handlePromiseError --------- Co-authored-by: Michel Heusschen <59014050+michelheusschen@users.noreply.github.com> Co-authored-by: Alex Tran <alex.tran1502@gmail.com>
73 lines
1.4 KiB
TypeScript
73 lines
1.4 KiB
TypeScript
import { handlePromiseError } from '$lib/utils';
|
|
|
|
interface Options {
|
|
concurrency: number;
|
|
}
|
|
|
|
type Runnable = () => Promise<unknown>;
|
|
|
|
export class ExecutorQueue {
|
|
private queue: Array<Runnable> = [];
|
|
private running = 0;
|
|
private _concurrency: number;
|
|
|
|
constructor(options?: Options) {
|
|
this._concurrency = options?.concurrency || 2;
|
|
}
|
|
|
|
get concurrency() {
|
|
return this._concurrency;
|
|
}
|
|
|
|
set concurrency(concurrency: number) {
|
|
if (concurrency < 1) {
|
|
return;
|
|
}
|
|
|
|
this._concurrency = concurrency;
|
|
|
|
const v = concurrency - this.running;
|
|
if (v > 0) {
|
|
for (let i = 0; i < v; i++) {
|
|
this.tryRun();
|
|
}
|
|
}
|
|
}
|
|
|
|
addTask<T>(task: () => Promise<T>): Promise<T> {
|
|
return new Promise((resolve, reject) => {
|
|
// Add a custom task that wrap the original one;
|
|
this.queue.push(async () => {
|
|
try {
|
|
this.running++;
|
|
const result = task();
|
|
resolve(await result);
|
|
} catch (error) {
|
|
reject(error);
|
|
} finally {
|
|
this.taskFinished();
|
|
}
|
|
});
|
|
// Then run it if possible !
|
|
this.tryRun();
|
|
});
|
|
}
|
|
|
|
private taskFinished(): void {
|
|
this.running--;
|
|
this.tryRun();
|
|
}
|
|
|
|
private tryRun() {
|
|
if (this.running >= this.concurrency) {
|
|
return;
|
|
}
|
|
|
|
const runnable = this.queue.shift();
|
|
if (!runnable) {
|
|
return;
|
|
}
|
|
|
|
handlePromiseError(runnable());
|
|
}
|
|
}
|