feat(weather): add periodic live updates (#4155)
This commit is contained in:
@@ -22,6 +22,7 @@ import { minecraftServerStatusJob } from "./jobs/minecraft-server-status";
|
||||
import { pingJob } from "./jobs/ping";
|
||||
import { rssFeedsJob } from "./jobs/rss-feeds";
|
||||
import { updateCheckerJob } from "./jobs/update-checker";
|
||||
import { weatherJob } from "./jobs/weather";
|
||||
import { createCronJobGroup } from "./lib";
|
||||
|
||||
export const jobGroup = createCronJobGroup({
|
||||
@@ -48,6 +49,7 @@ export const jobGroup = createCronJobGroup({
|
||||
firewallVersion: firewallVersionJob,
|
||||
firewallInterfaces: firewallInterfacesJob,
|
||||
refreshNotifications: refreshNotificationsJob,
|
||||
weather: weatherJob,
|
||||
});
|
||||
|
||||
export type JobGroupKeys = ReturnType<(typeof jobGroup)["getKeys"]>[number];
|
||||
|
||||
33
packages/cron-jobs/src/jobs/weather.ts
Normal file
33
packages/cron-jobs/src/jobs/weather.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import SuperJSON from "superjson";
|
||||
|
||||
import { EVERY_10_MINUTES } from "@homarr/cron-jobs-core/expressions";
|
||||
import { db, eq } from "@homarr/db";
|
||||
import { items } from "@homarr/db/schema";
|
||||
import { logger } from "@homarr/log";
|
||||
import { weatherRequestHandler } from "@homarr/request-handler/weather";
|
||||
|
||||
import type { WidgetComponentProps } from "../../../widgets";
|
||||
import { createCronJob } from "../lib";
|
||||
|
||||
export const weatherJob = createCronJob("weather", EVERY_10_MINUTES).withCallback(async () => {
|
||||
const weatherItems = await db.query.items.findMany({
|
||||
where: eq(items.kind, "weather"),
|
||||
});
|
||||
|
||||
const parsedItems = weatherItems.map((item) => ({
|
||||
id: item.id,
|
||||
options: SuperJSON.parse<WidgetComponentProps<"weather">["options"]>(item.options),
|
||||
}));
|
||||
|
||||
for (const item of parsedItems) {
|
||||
try {
|
||||
const innerHandler = weatherRequestHandler.handler({
|
||||
longitude: item.options.location.longitude,
|
||||
latitude: item.options.location.latitude,
|
||||
});
|
||||
await innerHandler.getCachedOrUpdatedDataAsync({ forceUpdate: true });
|
||||
} catch (error) {
|
||||
logger.error("Failed to update weather", { id: item.id, error });
|
||||
}
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user