From 145883b8c7c13ada4181c7881f2bde5edfe14869 Mon Sep 17 00:00:00 2001 From: David Ludwig Date: Fri, 7 May 2021 14:52:57 +0000 Subject: [PATCH] Update Seeker service to use new Database module. Clean up torrent IPC --- services/seeker/src/Application.ts | 2 +- services/seeker/src/services/Database.ts | 37 -------------- services/seeker/src/services/TorrentIpc.ts | 50 +++++++++++++++++++ .../{TorrentManager => }/TorrentManager.ts | 32 ++++++++---- .../TorrentManager/TorrentClientIpc.ts | 25 ---------- .../src/services/TorrentManager/index.ts | 3 -- services/seeker/src/services/index.ts | 19 +++---- 7 files changed, 84 insertions(+), 84 deletions(-) delete mode 100644 services/seeker/src/services/Database.ts create mode 100644 services/seeker/src/services/TorrentIpc.ts rename services/seeker/src/services/{TorrentManager => }/TorrentManager.ts (82%) delete mode 100644 services/seeker/src/services/TorrentManager/TorrentClientIpc.ts delete mode 100644 services/seeker/src/services/TorrentManager/index.ts diff --git a/services/seeker/src/Application.ts b/services/seeker/src/Application.ts index ef88a10..fea5e9e 100644 --- a/services/seeker/src/Application.ts +++ b/services/seeker/src/Application.ts @@ -1,4 +1,4 @@ -import services from "./services"; +import * as services from "./services"; import { Microservice } from "@autoplex/microservice"; /** diff --git a/services/seeker/src/services/Database.ts b/services/seeker/src/services/Database.ts deleted file mode 100644 index d8967a9..0000000 --- a/services/seeker/src/services/Database.ts +++ /dev/null @@ -1,37 +0,0 @@ -import { Connection } from "typeorm"; -import connectToDatabase from "@autoplex/database"; -import { env, secret } from "@autoplex/utils"; -import { InternalService } from "@autoplex/microservice"; -import Application from "../Application"; - -export default class Database extends InternalService -{ - /** - * The active database connection - */ - protected connection!: Connection; - - /** - * The service name - */ - public readonly NAME = "Database"; - - /** - * Boot the database service - */ - public async boot() { - let host = env("DB_HOST"); - let port = parseInt(env("DB_PORT")); - let username = env("DB_USER"); - let password = await secret(env("DB_PASSWORD_FILE")); - let database = env("DB_DATABASE"); - this.connection = await connectToDatabase(host, port, username, password, database); - } - - /** - * Shutdown the database service - */ - public async shutdown() { - await this.connection.close(); - } -} diff --git a/services/seeker/src/services/TorrentIpc.ts b/services/seeker/src/services/TorrentIpc.ts new file mode 100644 index 0000000..fdfd548 --- /dev/null +++ b/services/seeker/src/services/TorrentIpc.ts @@ -0,0 +1,50 @@ +import Application from "../Application"; +import { IpcClient } from "@autoplex-api/torrent"; + +interface IResponse { + response?: any, + error?: string | Error +} + +/** + * Declare EventEmitter types + */ +interface TorrentIpcEvents { + "connected": () => void +} + +/** + * Torrent IPC events + */ +declare interface TorrentIpc { + on(event: U, listener: TorrentIpcEvents[U]): this, + emit(event: U, ...args: Parameters): boolean +} + +/** + * The torrent client IPC service + */ +class TorrentIpc extends IpcClient +{ + /** + * Install the message event handlers + */ + protected installMessageHandlers() { + this.addListener("torrent_finished", this.onTorrentFinished); + } + + /** + * Invoked when a torrent has finished downloading + */ + protected async onTorrentFinished(infoHash: string) {} + + /** + * Emit an event when connected + */ + protected onConnect() { + super.onConnect(); + this.emit("connected"); + } +} + +export default TorrentIpc; diff --git a/services/seeker/src/services/TorrentManager/TorrentManager.ts b/services/seeker/src/services/TorrentManager.ts similarity index 82% rename from services/seeker/src/services/TorrentManager/TorrentManager.ts rename to services/seeker/src/services/TorrentManager.ts index f9130f9..09fcd5f 100644 --- a/services/seeker/src/services/TorrentManager/TorrentManager.ts +++ b/services/seeker/src/services/TorrentManager.ts @@ -1,8 +1,10 @@ import diskusage from "diskusage"; import { readdir } from "fs/promises"; import { MovieTicket, MovieTorrent } from "@autoplex/database"; -import Supervisor from "../Supervisor"; -import TorrentClientIpc from "./TorrentClientIpc" +import Supervisor from "./Supervisor"; +import { InternalService } from "@autoplex/microservice"; +import Application from "../Application"; +import TorrentIpc from "./TorrentIpc"; interface IPendingMovieTorrent { link: string, @@ -17,8 +19,13 @@ interface IDiskMap { // tvshows: string[] } -export default class TorrentManager extends TorrentClientIpc +export default class TorrentManager extends InternalService { + /** + * The service name + */ + public readonly NAME = "Torrent Manager"; + /** * The queue of movies to add to the client */ @@ -35,9 +42,9 @@ export default class TorrentManager extends TorrentClientIpc protected disks!: IDiskMap; /** - * The service name + * The torrent IPC client */ - public readonly NAME = "Torrent Manager"; + protected torrentIpc!: TorrentIpc; /** * Boot the Torrent Manager service @@ -48,6 +55,14 @@ export default class TorrentManager extends TorrentClientIpc await this.loadDisks(); } + /** + * Start the service + */ + public start() { + this.torrentIpc = this.app.service("Torrent"); + this.torrentIpc.on("connected", this.onConnect.bind(this)); + } + // Interface methods --------------------------------------------------------------------------- /** @@ -92,7 +107,7 @@ export default class TorrentManager extends TorrentClientIpc * Add the movies in the queue to the torrent client */ protected async addMovies() { - if (this.isAddingMovies || !this.isConnected || this.pendingMovies.length == 0) { + if (this.isAddingMovies || !this.torrentIpc.isConnected || this.pendingMovies.length == 0) { return; } this.isAddingMovies = true; @@ -115,7 +130,7 @@ export default class TorrentManager extends TorrentClientIpc */ public async addMovie(movie: MovieTicket, link: string, diskName: string) { try { - let infoHash = await this.add(link, `/mnt/movies/${diskName}/Downloads`); + let infoHash = await this.torrentIpc.add(link, `/mnt/movies/${diskName}/Downloads`); let torrent = new MovieTorrent(); torrent.infoHash = infoHash; torrent.diskName = diskName; @@ -134,7 +149,6 @@ export default class TorrentManager extends TorrentClientIpc * Invoked when the connection to the torrent client is established/re-established */ protected onConnect() { - super.onConnect(); this.addMovies(); } @@ -148,7 +162,7 @@ export default class TorrentManager extends TorrentClientIpc if (torrent === undefined || torrent.movieTicket.isFulfilled || torrent.movieTicket.isCanceled) { return; } - let details = (await this.details([infoHash]))[0]; + let details = (await this.torrentIpc.details([infoHash]))[0]; this.app.service("Supervisor").onMovieTorrentFinished(torrent, details); } } diff --git a/services/seeker/src/services/TorrentManager/TorrentClientIpc.ts b/services/seeker/src/services/TorrentManager/TorrentClientIpc.ts deleted file mode 100644 index c473a25..0000000 --- a/services/seeker/src/services/TorrentManager/TorrentClientIpc.ts +++ /dev/null @@ -1,25 +0,0 @@ -import Application from "../../Application"; -import { IpcClient } from "@autoplex-api/torrent"; - -interface IResponse { - response?: any, - error?: string | Error -} - -/** - * The torrent client IPC service - */ -export default abstract class TorrentClientIpc extends IpcClient -{ - /** - * Install the message event handlers - */ - protected installMessageHandlers() { - this.addListener("torrent_finished", this.onTorrentFinished); - } - - /** - * Invoked when a torrent has finished downloading - */ - protected async onTorrentFinished(infoHash: string) {} -} diff --git a/services/seeker/src/services/TorrentManager/index.ts b/services/seeker/src/services/TorrentManager/index.ts deleted file mode 100644 index b0931ce..0000000 --- a/services/seeker/src/services/TorrentManager/index.ts +++ /dev/null @@ -1,3 +0,0 @@ -import TorrentManager from "./TorrentManager"; - -export default TorrentManager; diff --git a/services/seeker/src/services/index.ts b/services/seeker/src/services/index.ts index 95653c4..3882b68 100644 --- a/services/seeker/src/services/index.ts +++ b/services/seeker/src/services/index.ts @@ -1,15 +1,16 @@ -import Database from "./Database"; -import IpcInterface from "./IpcInterface"; -import MovieSearch from "./MovieSearch"; -import PostProcessor from "./PostProcessor"; -import Supervisor from "./Supervisor"; -import TorrentManager from "./TorrentManager"; +export { DatabaseService } from "@autoplex/database"; +import IpcInterface from "./IpcInterface"; +import MovieSearch from "./MovieSearch"; +import PostProcessor from "./PostProcessor"; +import Supervisor from "./Supervisor"; +import TorrentIpc from "./TorrentIpc"; +import TorrentManager from "./TorrentManager"; -export default { - Database, +export { IpcInterface, MovieSearch, PostProcessor, Supervisor, - TorrentManager, + TorrentIpc, + TorrentManager }