From 9661d9a261d3b215d590bdc3524a77bb5a55d115 Mon Sep 17 00:00:00 2001 From: David Ludwig Date: Tue, 4 May 2021 10:28:44 -0500 Subject: [PATCH] Use new IPC package and add torrent client IPC --- services/seeker/.env.example | 3 - services/seeker/package.json | 1 + services/seeker/src/services/IpcInterface.ts | 2 +- .../services/PostProcessor/PostProcessor.ts | 2 +- services/seeker/src/services/Supervisor.ts | 2 +- .../TorrentManager/TorrentClientIpc.ts | 118 +----------------- .../services/TorrentManager/TorrentManager.ts | 5 +- 7 files changed, 7 insertions(+), 126 deletions(-) diff --git a/services/seeker/.env.example b/services/seeker/.env.example index 28d01df..6be17f7 100644 --- a/services/seeker/.env.example +++ b/services/seeker/.env.example @@ -9,6 +9,3 @@ DB_PORT = 3306 DB_USER = root DB_PASSWORD_FILE = /run/secrets/mysql_root_password DB_DATABASE = autoplex_request - -# Torrent client IPC socket path -TORRENT_CLIENT_IPC_SOCKET = /var/autoplex/ipc/torrent_client.sock diff --git a/services/seeker/package.json b/services/seeker/package.json index d49fe7a..c3d91d6 100644 --- a/services/seeker/package.json +++ b/services/seeker/package.json @@ -21,6 +21,7 @@ "typescript": "^4.2.4" }, "dependencies": { + "@autoplex-api/torrent": "^0.0.0", "@autoplex/database": "^0.0.0", "@autoplex/microservice": "^0.0.0", "@autoplex/utils": "^0.0.0", diff --git a/services/seeker/src/services/IpcInterface.ts b/services/seeker/src/services/IpcInterface.ts index f0b6681..4b0f329 100644 --- a/services/seeker/src/services/IpcInterface.ts +++ b/services/seeker/src/services/IpcInterface.ts @@ -1,4 +1,4 @@ -import { IpcServerService } from "@autoplex/microservice"; +import { IpcServerService } from "@autoplex/ipc"; import Application from "../Application"; import Supervisor from "./Supervisor"; import { MovieTicket } from "@autoplex/database"; diff --git a/services/seeker/src/services/PostProcessor/PostProcessor.ts b/services/seeker/src/services/PostProcessor/PostProcessor.ts index 1711408..37ccbdd 100644 --- a/services/seeker/src/services/PostProcessor/PostProcessor.ts +++ b/services/seeker/src/services/PostProcessor/PostProcessor.ts @@ -2,7 +2,7 @@ import { link, mkdir } from "fs/promises"; import { dirname, extname } from "path"; import Application from "../../Application"; import { MovieTicket, MovieTorrent } from "@autoplex/database"; -import { ISerializedTorrent } from "../TorrentManager/TorrentClientIpc"; +import { ISerializedTorrent } from "@autoplex-api/torrent"; import { safeTitleFileName } from "../../utils"; import Supervisor from "../Supervisor"; import { InternalService } from "@autoplex/microservice"; diff --git a/services/seeker/src/services/Supervisor.ts b/services/seeker/src/services/Supervisor.ts index df94a5f..54f1f3d 100644 --- a/services/seeker/src/services/Supervisor.ts +++ b/services/seeker/src/services/Supervisor.ts @@ -4,7 +4,7 @@ import MovieSearch from "./MovieSearch"; import PostProcessor from "./PostProcessor"; import { InternalService } from "@autoplex/microservice"; import TorrentManager from "./TorrentManager"; -import { ISerializedTorrent } from "./TorrentManager/TorrentClientIpc"; +import { ISerializedTorrent } from "@autoplex-api/torrent"; export default class Supervisor extends InternalService { diff --git a/services/seeker/src/services/TorrentManager/TorrentClientIpc.ts b/services/seeker/src/services/TorrentManager/TorrentClientIpc.ts index e8f569f..c473a25 100644 --- a/services/seeker/src/services/TorrentManager/TorrentClientIpc.ts +++ b/services/seeker/src/services/TorrentManager/TorrentClientIpc.ts @@ -1,80 +1,16 @@ -import { Socket } from "net"; import Application from "../../Application"; -import { IpcClientService } from "@autoplex/microservice"; -import { env } from "@autoplex/utils"; +import { IpcClient } from "@autoplex-api/torrent"; interface IResponse { response?: any, error?: string | Error } -export interface ITorrent { - name: string, - infoHash: string, - progress: number, - state: TorrentState -} - -export interface ISerializedFile { - path : string; - size : number; - downloaded: number; - progress : number; - selected : boolean; -} - -export interface ISerializedTorrent { - name : string; - infoHash : string; - downloaded : number; - uploaded : number; - ratio : number; - size : number; - downloadSpeed: number; - uploadSpeed : number; - numPeers : number; - progress : number; - path : string; - state : TorrentState; - files : ISerializedFile[]; -} - -export enum TorrentState { - Ready = 0x1, - Paused = 0x2, - Done = 0x4 -} - -/** - * A custom error type for torrent client connection errors - */ -export class TorrentClientConnectionError extends Error { - constructor(...args: any[]) { - super(...args); - Object.setPrototypeOf(this, TorrentClientConnectionError.prototype); - } -} - /** * The torrent client IPC service */ -export default abstract class TorrentClientIpc extends IpcClientService +export default abstract class TorrentClientIpc extends IpcClient { - /** - * The path to the socket file - */ - public readonly SOCKET_PATH = env("TORRENT_CLIENT_IPC_SOCKET"); - - /** - * 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 message event handlers */ @@ -86,54 +22,4 @@ export default abstract class TorrentClientIpc extends IpcClientServiceresponse.data; - } - - /** - * Remove a torrent from the client - * @param torrent Torrent info hash - */ - protected 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 - */ - protected 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.data; - } - - /** - * Get full details of each of the provided torrents - * @param torrentIds Array of torrent info hashes - */ - protected 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.data; - } } diff --git a/services/seeker/src/services/TorrentManager/TorrentManager.ts b/services/seeker/src/services/TorrentManager/TorrentManager.ts index 6935576..13e70c9 100644 --- a/services/seeker/src/services/TorrentManager/TorrentManager.ts +++ b/services/seeker/src/services/TorrentManager/TorrentManager.ts @@ -2,7 +2,7 @@ import diskusage from "diskusage"; import { readdir } from "fs/promises"; import { MovieTicket, MovieTorrent } from "@autoplex/database"; import Supervisor from "../Supervisor"; -import TorrentClientIpc, { TorrentClientConnectionError } from "./TorrentClientIpc" +import TorrentClientIpc from "./TorrentClientIpc" interface IPendingMovieTorrent { link: string, @@ -122,9 +122,6 @@ export default class TorrentManager extends TorrentClientIpc torrent.movieTicket = movie; await torrent.save(); } catch(e) { - if (e instanceof TorrentClientConnectionError) { - throw e; - } console.log("Failed download the torrent"); return false; }