Skip to content

Commit

Permalink
fix: pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jahabeebs committed Nov 6, 2024
1 parent 1b1aab9 commit e281b9e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 31 deletions.
2 changes: 1 addition & 1 deletion apps/agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const main = async (): Promise<void> => {

logger.debug("Initializing notifier...");

const notifier = new DiscordNotifier(config.DISCORD_WEBHOOK);
const notifier = new DiscordNotifier(config.DISCORD_WEBHOOK, logger);

logger.debug("Notifier initialized...");

Expand Down
12 changes: 7 additions & 5 deletions packages/automated-dispute/src/services/discordNotifier.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isNativeError } from "util/types";
import type { APIEmbed, JSONEncodable } from "discord.js";
import { Logger, stringify } from "@ebo-agent/shared";
import { ILogger, stringify } from "@ebo-agent/shared";
import { WebhookClient, WebhookMessageCreateOptions } from "discord.js";

import { NotificationFailureException } from "../exceptions/index.js";
Expand All @@ -15,19 +15,21 @@ export type WebhookMessage = WebhookMessageCreateOptions & {
*/
export class DiscordNotifier implements NotificationService {
private client: WebhookClient;
private defaultAvatarUrl?: string;
private logger: Logger;

/**
* Creates an instance of DiscordNotifier.
*
* @param {string} url - The Discord webhook URL.
* @param {ILogger} logger - An injected logger instance implementing ILogger.
* @param {string} [defaultAvatarUrl] - The default avatar URL to use if none is provided.
*/
constructor(url: string, defaultAvatarUrl?: string) {
constructor(
url: string,
private readonly logger: ILogger,
private readonly defaultAvatarUrl?: string,
) {
this.client = new WebhookClient({ url });
this.defaultAvatarUrl = defaultAvatarUrl;
this.logger = Logger.getInstance();
}

/**
Expand Down
18 changes: 9 additions & 9 deletions packages/automated-dispute/src/services/eboProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ export class EboProcessor {
lastBlock: Block<bigint, boolean, "finalized">,
) {
const firstEvent = events[0];
const actor = this.getOrCreateActor(requestId, firstEvent);
const actor = await this.getOrCreateActor(requestId, firstEvent);

if (!actor) {
this.logger.warn(droppingUnhandledEventsWarning(requestId));
Expand Down Expand Up @@ -299,7 +299,7 @@ export class EboProcessor {
* @param firstEvent an event to create an actor if it does not exist
* @returns the actor handling the specified request
*/
private getOrCreateActor(requestId: RequestId, firstEvent?: EboEvent<EboEventName>) {
private async getOrCreateActor(requestId: RequestId, firstEvent?: EboEvent<EboEventName>) {
const actor = this.actorsManager.getActor(requestId);

if (actor) return actor;
Expand All @@ -322,7 +322,7 @@ export class EboProcessor {
} else {
this.logger.warn(`Chain ${chainId} not supported by the agent. Skipping...`);

this.notifier.sendError(
await this.notifier.sendError(
`Chain ${chainId} not supported by the agent. Skipping...`,
{ chainId, requestId },
new Error("Unsupported chain"),
Expand Down Expand Up @@ -354,14 +354,14 @@ export class EboProcessor {
return actor;
}

private onActorError(requestId: RequestId, error: Error) {
private async onActorError(requestId: RequestId, error: Error) {
this.logger.error(
`Critical error. Actor event handling request ${requestId} ` +
`threw a non-recoverable error: ${error.message}\n\n` +
`The request ${requestId} will stop being tracked by the system.`,
);

this.notifier.sendError(`Actor error for request ${requestId}`, { requestId }, error);
await this.notifier.sendError(`Actor error for request ${requestId}`, { requestId }, error);

this.terminateActor(requestId);
}
Expand Down Expand Up @@ -432,7 +432,7 @@ export class EboProcessor {
`Could not create a request for epoch ${epoch} and chain ${chain}.`,
);

this.notifier.sendError(
await this.notifier.sendError(
`Could not create a request for epoch ${epoch} and chain ${chain}.`,
{ epoch, chain },
err,
Expand All @@ -450,7 +450,7 @@ export class EboProcessor {
this.logger.error(`Requests creation failed: ${err}`);
}

this.notifier.sendError("Error creating missing requests", { epoch }, err);
await this.notifier.sendError("Error creating missing requests", { epoch }, err);
}
}

Expand All @@ -459,7 +459,7 @@ export class EboProcessor {
*
* @param requestId the ID of the request the actor is handling
*/
private terminateActor(requestId: RequestId) {
private async terminateActor(requestId: RequestId) {
this.logger.info(`Terminating actor handling request ${requestId}...`);

const deletedActor = this.actorsManager.deleteActor(requestId);
Expand All @@ -469,7 +469,7 @@ export class EboProcessor {
} else {
this.logger.warn(alreadyDeletedActorWarning(requestId));

this.notifier.sendError(
await this.notifier.sendError(
`Actor handling request ${requestId} was already terminated.`,
{ requestId },
new Error("Actor already deleted"),
Expand Down
24 changes: 8 additions & 16 deletions packages/automated-dispute/tests/services/discordNotifier.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { stringify } from "@ebo-agent/shared";
import { ILogger, stringify } from "@ebo-agent/shared";
import { WebhookClient } from "discord.js";
import { beforeEach, describe, expect, it, vi } from "vitest";

Expand All @@ -18,27 +18,19 @@ vi.mock("discord.js", () => {
};
});

vi.mock("../../src/Logger", () => {
const mockLogger = {
error: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
debug: vi.fn(),
};
return {
Logger: {
getInstance: () => mockLogger,
},
};
});

describe("DiscordNotifier", () => {
let notifier: DiscordNotifier;
const webhookUrl = "https://discord.com/api/webhooks/TEST/TEST";

beforeEach(() => {
vi.clearAllMocks();
notifier = new DiscordNotifier(webhookUrl);
const mockLogger: ILogger = {
error: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
debug: vi.fn(),
};
notifier = new DiscordNotifier(webhookUrl, mockLogger);
});

describe("send", () => {
Expand Down

0 comments on commit e281b9e

Please sign in to comment.