feat: add job on start option for icons updater (#442)

This commit is contained in:
Meier Lukas
2024-05-05 22:20:38 +02:00
committed by GitHub
parent 4b80b16b53
commit 7b31684c84
2 changed files with 79 additions and 70 deletions

View File

@@ -8,9 +8,10 @@ import { logger } from "@homarr/log";
import { EVERY_WEEK } from "~/lib/cron-job/constants"; import { EVERY_WEEK } from "~/lib/cron-job/constants";
import { createCronJob } from "~/lib/cron-job/creator"; import { createCronJob } from "~/lib/cron-job/creator";
export const iconsUpdaterJob = createCronJob(EVERY_WEEK).withCallback( export const iconsUpdaterJob = createCronJob(EVERY_WEEK, {
async () => { runOnStart: true,
logger.info(`Updating icon repository cache...`); }).withCallback(async () => {
logger.info("Updating icon repository cache...");
const stopWatch = new Stopwatch(); const stopWatch = new Stopwatch();
const repositoryIconGroups = await fetchIconsAsync(); const repositoryIconGroups = await fetchIconsAsync();
const countIcons = repositoryIconGroups const countIcons = repositoryIconGroups
@@ -30,7 +31,7 @@ export const iconsUpdaterJob = createCronJob(EVERY_WEEK).withCallback(
let countDeleted = 0; let countDeleted = 0;
let countInserted = 0; let countInserted = 0;
logger.info(`Updating icons in database...`); logger.info("Updating icons in database...");
stopWatch.reset(); stopWatch.reset();
await db.transaction(async (transaction) => { await db.transaction(async (transaction) => {
@@ -54,7 +55,7 @@ export const iconsUpdaterJob = createCronJob(EVERY_WEEK).withCallback(
if ( if (
databaseIconGroups databaseIconGroups
.flatMap((group) => group.icons) .flatMap((group) => group.icons)
.some((dbIcon) => dbIcon.checksum == icon.checksum) .some((dbIcon) => dbIcon.checksum === icon.checksum)
) { ) {
skippedChecksums.push(icon.checksum); skippedChecksums.push(icon.checksum);
continue; continue;
@@ -76,9 +77,7 @@ export const iconsUpdaterJob = createCronJob(EVERY_WEEK).withCallback(
.filter((icon) => !skippedChecksums.includes(icon.checksum)); .filter((icon) => !skippedChecksums.includes(icon.checksum));
for (const icon of deadIcons) { for (const icon of deadIcons) {
await transaction await transaction.delete(icons).where(eq(icons.checksum, icon.checksum));
.delete(icons)
.where(eq(icons.checksum, icon.checksum));
countDeleted++; countDeleted++;
} }
}); });
@@ -86,5 +85,4 @@ export const iconsUpdaterJob = createCronJob(EVERY_WEEK).withCallback(
logger.info( logger.info(
`Updated database within ${stopWatch.getElapsedInHumanWords()} (-${countDeleted}, +${countInserted})`, `Updated database within ${stopWatch.getElapsedInHumanWords()} (-${countDeleted}, +${countInserted})`,
); );
}, });
);

View File

@@ -2,9 +2,20 @@ import cron from "node-cron";
import type { MaybePromise } from "@homarr/common/types"; import type { MaybePromise } from "@homarr/common/types";
export const createCronJob = (cronExpression: string) => { interface CreateCronJobOptions {
runOnStart?: boolean;
}
export const createCronJob = (
cronExpression: string,
options: CreateCronJobOptions = { runOnStart: false },
) => {
return { return {
withCallback: (callback: () => MaybePromise<void>) => { withCallback: (callback: () => MaybePromise<void>) => {
if (options.runOnStart) {
void callback();
}
const task = cron.schedule(cronExpression, () => void callback(), { const task = cron.schedule(cronExpression, () => void callback(), {
scheduled: false, scheduled: false,
}); });