Skip to content
This repository was archived by the owner on Feb 3, 2025. It is now read-only.

Commit 130c339

Browse files
committed
feat: cancel job if exists
1 parent bce4832 commit 130c339

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

src/server/queue/checkChapters.ts

+24-4
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const checkChapters = async (manga: MangaWithLibrary) => {
4444
opts: {
4545
jobId: `${sanitizer(manga.title)}_${chapterIndex - 1}_download`,
4646
},
47-
name: `${sanitizer(manga.title)}_${chapterIndex - 1}_download`,
47+
name: `${sanitizer(manga.title)}_chapter#${chapterIndex - 1}_download`,
4848
data: {
4949
chapterIndex: chapterIndex - 1,
5050
source: manga.source,
@@ -78,19 +78,39 @@ export const checkChaptersWorker = new Worker(
7878
},
7979
);
8080

81+
export const getJobIdFromTitle = (title: string) => `check_${sanitizer(title)}_chapters`;
82+
83+
export const removeJob = async (title: string) => {
84+
const jobId = getJobIdFromTitle(title);
85+
const jobs = await checkChaptersQueue.getJobs('delayed');
86+
await Promise.all(
87+
jobs
88+
.filter((job) => job.opts.repeat?.jobId === jobId)
89+
.map(async (job) => {
90+
if (job.id) {
91+
return checkChaptersQueue.remove(job.id);
92+
}
93+
return null;
94+
}),
95+
);
96+
};
97+
8198
export const schedule = async (manga: MangaWithLibrary) => {
8299
if (manga.interval === 'never') {
83100
return;
84101
}
85102

103+
await removeJob(manga.title);
104+
const jobId = getJobIdFromTitle(manga.title);
105+
86106
await checkChaptersQueue.add(
87-
`check_${manga.title}_chapters`,
107+
jobId,
88108
{
89109
manga,
90110
},
91111
{
92-
jobId: `check_${manga.libraryId}_${manga.id}_chapters`,
93-
repeatJobKey: `check_${manga.libraryId}_${manga.id}_chapters`,
112+
jobId,
113+
repeatJobKey: jobId,
94114
repeat: {
95115
pattern: cronMap[manga.interval as keyof typeof cronMap],
96116
},

src/server/trpc/router/manga.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { TRPCError } from '@trpc/server';
22
import path from 'path';
33
import { z } from 'zod';
44
import { sanitizer } from '../../../utils/sanitize';
5-
import { schedule } from '../../queue/checkChapters';
5+
import { removeJob, schedule } from '../../queue/checkChapters';
66
import { getAvailableSources, getMangaDetail, removeManga, search } from '../../utils/mangal';
77
import { t } from '../trpc';
88

@@ -68,7 +68,7 @@ export const mangaRouter = t.router({
6868
});
6969
const mangaPath = path.resolve(removed.Library.path, sanitizer(removed.title));
7070
await removeManga(mangaPath);
71-
// TODO: remove jobs also
71+
await removeJob(removed.title);
7272
}),
7373
add: t.procedure
7474
.input(

0 commit comments

Comments
 (0)