refactor(logs): move to core package (#4586)
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import type { LogLevel } from "@homarr/log/constants";
|
||||
import type { LogLevel } from "@homarr/core/infrastructure/logs/constants";
|
||||
|
||||
import { createListChannel, createQueueChannel, createSubPubChannel } from "./lib/channel";
|
||||
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
import { randomUUID } from "crypto";
|
||||
|
||||
import type { MaybePromise } from "@homarr/common/types";
|
||||
import { logger } from "@homarr/log";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
|
||||
import { createRedisConnection } from "./connection";
|
||||
|
||||
const logger = createLogger({ module: "channelSubscriptionTracker" });
|
||||
|
||||
type SubscriptionCallback = (message: string) => MaybePromise<void>;
|
||||
|
||||
/**
|
||||
@@ -26,7 +28,7 @@ export class ChannelSubscriptionTracker {
|
||||
* @returns a function to unsubscribe from the channel
|
||||
*/
|
||||
public static subscribe(channelName: string, callback: SubscriptionCallback) {
|
||||
logger.debug(`Adding redis channel callback channel='${channelName}'`);
|
||||
logger.debug("Adding redis channel callback", { channel: channelName });
|
||||
|
||||
// We only want to activate the listener once
|
||||
if (!this.listenerActive) {
|
||||
@@ -39,18 +41,18 @@ export class ChannelSubscriptionTracker {
|
||||
|
||||
// If there are no subscriptions to the channel, subscribe to it
|
||||
if (channelSubscriptions.size === 0) {
|
||||
logger.debug(`Subscribing to redis channel channel='${channelName}'`);
|
||||
logger.debug("Subscribing to redis channel", { channel: channelName });
|
||||
void this.redis.subscribe(channelName);
|
||||
}
|
||||
|
||||
logger.debug(`Adding redis channel callback channel='${channelName}' id='${id}'`);
|
||||
logger.debug("Adding redis channel callback", { channel: channelName, id });
|
||||
channelSubscriptions.set(id, callback);
|
||||
|
||||
this.subscriptions.set(channelName, channelSubscriptions);
|
||||
|
||||
// Return a function to unsubscribe
|
||||
return () => {
|
||||
logger.debug(`Removing redis channel callback channel='${channelName}' id='${id}'`);
|
||||
logger.debug("Removing redis channel callback", { channel: channelName, id });
|
||||
|
||||
const channelSubscriptions = this.subscriptions.get(channelName);
|
||||
if (!channelSubscriptions) return;
|
||||
@@ -62,7 +64,7 @@ export class ChannelSubscriptionTracker {
|
||||
return;
|
||||
}
|
||||
|
||||
logger.debug(`Unsubscribing from redis channel channel='${channelName}'`);
|
||||
logger.debug("Unsubscribing from redis channel", { channel: channelName });
|
||||
void this.redis.unsubscribe(channelName);
|
||||
this.subscriptions.delete(channelName);
|
||||
};
|
||||
@@ -76,14 +78,14 @@ export class ChannelSubscriptionTracker {
|
||||
this.redis.on("message", (channel, message) => {
|
||||
const channelSubscriptions = this.subscriptions.get(channel);
|
||||
if (!channelSubscriptions) {
|
||||
logger.warn(`Received message on unknown channel channel='${channel}'`);
|
||||
logger.warn("Received message on unknown channel", { channel });
|
||||
return;
|
||||
}
|
||||
|
||||
for (const [id, callback] of channelSubscriptions.entries()) {
|
||||
// Don't log messages from the logging channel as it would create an infinite loop
|
||||
if (channel !== "pubSub:logging") {
|
||||
logger.debug(`Calling subscription callback channel='${channel}' id='${id}'`);
|
||||
logger.debug("Calling subscription callback", { channel, id });
|
||||
}
|
||||
void callback(message);
|
||||
}
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
import superjson from "superjson";
|
||||
|
||||
import { createId, hashObjectBase64 } from "@homarr/common";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
import type { WidgetKind } from "@homarr/definitions";
|
||||
import { logger } from "@homarr/log";
|
||||
|
||||
import { ChannelSubscriptionTracker } from "./channel-subscription-tracker";
|
||||
import { createRedisConnection } from "./connection";
|
||||
|
||||
const logger = createLogger({ module: "redisChannel" });
|
||||
|
||||
const publisher = createRedisConnection();
|
||||
const lastDataClient = createRedisConnection();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user