fix: ping urls are not reset when restarting (#924)

This commit is contained in:
Meier Lukas
2024-08-08 20:36:51 +02:00
committed by GitHub
parent f9e2df085b
commit 365e267b8d
3 changed files with 20 additions and 1 deletions

View File

@@ -16,6 +16,7 @@ export interface CreateCronJobCreatorOptions<TAllowedNames extends string> {
interface CreateCronJobOptions {
runOnStart?: boolean;
beforeStart?: () => MaybePromise<void>;
}
const createCallback = <TAllowedNames extends string, TName extends TAllowedNames>(
@@ -62,6 +63,11 @@ const createCallback = <TAllowedNames extends string, TName extends TAllowedName
cronExpression,
scheduledTask,
async onStartAsync() {
if (options.beforeStart) {
creatorOptions.logger.logDebug(`Running beforeStart for job: ${name}`);
await options.beforeStart();
}
if (!options.runOnStart) return;
creatorOptions.logger.logDebug(`The cron job '${name}' is running because runOnStart is set to true`);

View File

@@ -5,7 +5,14 @@ import { pingChannel, pingUrlChannel } from "@homarr/redis";
import { createCronJob } from "../lib";
export const pingJob = createCronJob("ping", EVERY_MINUTE).withCallback(async () => {
const resetPreviousUrlsAsync = async () => {
await pingUrlChannel.clearAsync();
logger.info("Cleared previous ping urls");
};
export const pingJob = createCronJob("ping", EVERY_MINUTE, {
beforeStart: resetPreviousUrlsAsync,
}).withCallback(async () => {
const urls = await pingUrlChannel.getAllAsync();
for (const url of new Set(urls)) {

View File

@@ -77,6 +77,12 @@ export const createListChannel = <TItem>(name: string) => {
removeAsync: async (item: TItem) => {
await getSetClient.lrem(listChannelName, 0, superjson.stringify(item));
},
/**
* Clear all items from the channels list
*/
clearAsync: async () => {
await getSetClient.del(listChannelName);
},
/**
* Add an item to the channels list
* @param item item to add