Prevent concurrent tracker calls during manga migration

This commit is contained in:
schroda
2026-05-13 02:12:05 +02:00
parent 0bd5896cee
commit 679df11aad
2 changed files with 28 additions and 6 deletions

View File

@@ -25,6 +25,9 @@ import type {
} from '@/features/chapter/Chapter.types.ts';
import type { GqlMetaHolder } from '@/features/metadata/Metadata.types.ts';
import { getMetadataServerSettings } from '@/features/settings/services/ServerSettingsMetadata.ts';
import { Queue } from '@/lib/Queue.ts';
import type { TrackerIdInfo } from '@/features/tracker/Tracker.types.ts';
import { assertIsDefined } from '@/base/Asserts.ts';
type MangaToMigrate = NonNullable<GetMangaToMigrateQuery['manga']>;
type MangaToMigrateTo = NonNullable<GetMangaToMigrateToFetchMutation['fetchManga']>['manga'];
@@ -43,6 +46,20 @@ const performMigrationAction = async (migrateAction: keyof MigrateAction, ...act
Promise.all(actions.flatMap((action) => action[migrateAction]()));
export class MangaMigration {
private static trackerQueue = new Map<TrackerIdInfo['id'], Queue>();
private static getOrCreateQueue(trackerId: TrackerIdInfo['id']): Queue {
if (!MangaMigration.trackerQueue.has(trackerId)) {
MangaMigration.trackerQueue.set(trackerId, new Queue(1));
}
const queue = MangaMigration.trackerQueue.get(trackerId);
assertIsDefined(queue);
return queue;
}
static async migrate(
mangaToMigrate: MangaToMigrate | null | undefined,
mangaToMigrateTo: MangaToMigrateTo | null | undefined,
@@ -281,12 +298,16 @@ export class MangaMigration {
copy: () =>
trackBindingsToAdd.map(
(trackRecord) =>
requestManager.bindTracker(
mangaToMigrateTo.id,
trackRecord.trackerId,
trackRecord.remoteId,
trackRecord.private,
).response,
MangaMigration.getOrCreateQueue(trackRecord.trackerId).enqueue(
String(mangaToMigrate.id),
() =>
requestManager.bindTracker(
mangaToMigrateTo.id,
trackRecord.trackerId,
trackRecord.remoteId,
trackRecord.private,
).response,
).promise,
),
cleanup: () =>
mode === 'migrate'