diff --git a/services/request/src/server/services/Ipc/IpcClient.ts b/services/request/src/server/services/Ipc/IpcClient.ts deleted file mode 100644 index f258578..0000000 --- a/services/request/src/server/services/Ipc/IpcClient.ts +++ /dev/null @@ -1,156 +0,0 @@ -import { Socket } from "net"; -import { InternalService } from "@autoplex/microservice"; -import RawIPC = require("node-ipc"); -import Application from "@server/Application"; - -export interface IIpcResponse { - response?: any, - error?: string | Error -} - -/** - * IPC connection error type - */ -export class IpcConnectionError extends Error { - constructor(...args: any[]) { - super(...args); - Object.setPrototypeOf(this, IpcConnectionError.prototype); - } -} - -export default class IpcClient extends InternalService -{ - /** - * Indicate if there is an active connection to the IPC - */ - private __isConnected: boolean; - - /** - * The IPC ID to connect to - */ - private __targetIpc: string; - - /** - * The IPC client name - */ - protected readonly IPC_NAME: string; - - /** - * HOLY @#$@% WHOEVER MADE THE TYPES FOR node-ipc SHOULDB BE HANGED - */ - protected ipc; - - /** - * The active IPC socket - */ - protected socket!: Socket; - - /** - * Create a new IPC client for the Seeker - */ - constructor(name: string, app: Application, ipcId: string, targetIpc: string) { - super(app); - this.IPC_NAME = name; - this.ipc = new RawIPC.IPC(); - this.ipc.config.id = ipcId; - this.ipc.config.retry = 1500; - this.ipc.config.silent = true; - this.__targetIpc = targetIpc; - this.__isConnected = false; - } - - /** - * The name of the service - */ - public get name() { return this.IPC_NAME } - - /** - * Boot the seeker client IPC service - */ - public boot() { - return new Promise((resolve, reject) => { - this.ipc.connectTo(this.__targetIpc, process.env["SEEKER_IPC_SOCKET"], () => { - this.socket = this.ipc.of[this.__targetIpc]; - this.installSocketEventHandlers(this.socket); - this.installSocketMessageHandlers(this.socket); - resolve(); - }); - }); - } - - /** - * Shutdown the seeker IPC service - */ - public async shutdown() { - this.ipc.disconnect(this.__targetIpc); - } - - /** - * Install the event handlers for the IPC socket - */ - protected installSocketEventHandlers(socket: Socket) { - socket.on("connect", () => this.onConnect()); - socket.on("error", (error: any) => this.onError(error)); - socket.on("disconnect", () => this.onDisconnect()); - socket.on("destroy", () => this.onDestroy()); - } - - /** - * Install the event handlers for receiving on the IPC socket - */ - protected installSocketMessageHandlers(socket: Socket) {} - - // Socket Event Handlers ----------------------------------------------------------------------- - - protected onConnect() { - this.log("IPC: Connection established"); - this.__isConnected = true; - } - - protected onError(error: string | Error) { - if (this.__isConnected) { - this.log("IPC: Error occurred:", error); - } - } - - protected onDisconnect() { - if (this.__isConnected) { - this.log("IPC: Disconnected"); - } - this.__isConnected = false; - } - - protected onDestroy() { - this.log("IPC: Destroyed"); - } - - // Methods ------------------------------------------------------------------------------------- - - /** - * Perform a general request - */ - protected async request(method: string, message?: any) { - return new Promise((resolve, reject) => { - if (!this.isConnected) { - reject(new IpcConnectionError("Not connected to Seeker")); - } - let respond = (response: any) => { - clearTimeout(timeout); - resolve(response); - } - // Include timeout mechanism in the off chance something breaks - let timeout = setTimeout(() => { - this.socket.off(method, respond); - reject(new IpcConnectionError("IPC request timeout")); - }, 1000); - this.socket.once(method, respond); - this.socket.emit(method, message); - }); - } - - // Accessors ----------------------------------------------------------------------------------- - - get isConnected() { - return this.__isConnected; - } -} diff --git a/services/request/src/server/services/Ipc/SeekerIpcClient.ts b/services/request/src/server/services/Ipc/SeekerIpcClient.ts index fe96aad..cd66b76 100644 --- a/services/request/src/server/services/Ipc/SeekerIpcClient.ts +++ b/services/request/src/server/services/Ipc/SeekerIpcClient.ts @@ -1,13 +1,21 @@ -import IpcClient from "./IpcClient"; import Application from "@server/Application"; +import { IpcClientService } from "@autoplex/microservice"; +import { env } from "@autoplex/utils"; -export default class SeekerIpcClient extends IpcClient +export default class SeekerIpcClient extends IpcClientService { /** - * Create a new IPC client for the Seeker + * The service name */ - constructor(app: Application) { - super("Seeker IPC", app, "request", "seeker"); + public get name() { + return "Seeker IPC" + } + + /** + * The path to the socket file + */ + public get socketPath() { + return env("SEEKER_IPC_SOCKET"); } // Methods ------------------------------------------------------------------------------------- diff --git a/services/request/src/server/services/Ipc/TorrentIpcClient.ts b/services/request/src/server/services/Ipc/TorrentIpcClient.ts deleted file mode 100644 index a7cd6b5..0000000 --- a/services/request/src/server/services/Ipc/TorrentIpcClient.ts +++ /dev/null @@ -1,63 +0,0 @@ -import { ISerializedTorrent, ITorrent } from "../../common"; -import IpcClient from "./IpcClient"; -import Application from "@server/Application"; - -export default class TorrentIpcClient extends IpcClient -{ - /** - * Create a new IPC client for the torrent client - */ - constructor(app: Application) { - super("Torrent IPC", app, "request", "torrent_client"); - } - - // Methods ------------------------------------------------------------------------------------- - - /** - * Add a torrent to the client - * @param torrent Magnet URI or file buffer - */ - public async add(torrent: string | Buffer) { - let response = await this.request("add", torrent); - if (response.error) { - throw new Error("Failed to add torrent"); - } - return response.response; - } - - /** - * Remove a torrent from the client - * @param torrent Torrent info hash - */ - public async remove(torrent: string) { - let response = await this.request("remove", torrent); - if (response.error) { - throw new Error("Failed to remove torrent"); - } - } - - /** - * Get a list of all torrents in the client - */ - public async list() { - let response = await this.request("list"); - if (response.error) { - console.error(response.error); - throw new Error("Failed to obtain torrent list"); - } - return response.response; - } - - /** - * Get full details of each of the provided torrents - * @param torrentIds Array of torrent info hashes - */ - public async details(...torrentIds: string[]) { - let response = await this.request("details", torrentIds); - if (response.error) { - console.error(response.error); - throw new Error("Failed to retrieve torrent details"); - } - return response.response; - } -} diff --git a/services/request/src/server/services/index.ts b/services/request/src/server/services/index.ts index 39d6f47..189fa70 100644 --- a/services/request/src/server/services/index.ts +++ b/services/request/src/server/services/index.ts @@ -3,7 +3,6 @@ import DiscordBot from "./DiscordBot"; import MovieSearch from "./MovieSearch"; import PlexLibrary from "./PlexLibrary"; import SeekerIpcClient from "./Ipc/SeekerIpcClient"; -import TorrentIpcClient from "./Ipc/TorrentIpcClient"; import TvDb from "./TvDb"; import WebServer from "./WebServer";