Browse Source

Convert Request service to use the new generic IPC services from the microservice package

dev
David Ludwig 4 years ago
parent
commit
59f7cddbf8
4 changed files with 13 additions and 225 deletions
  1. +0
    -156
      services/request/src/server/services/Ipc/IpcClient.ts
  2. +13
    -5
      services/request/src/server/services/Ipc/SeekerIpcClient.ts
  3. +0
    -63
      services/request/src/server/services/Ipc/TorrentIpcClient.ts
  4. +0
    -1
      services/request/src/server/services/index.ts

+ 0
- 156
services/request/src/server/services/Ipc/IpcClient.ts View File

@ -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<Application>
{
/**
* 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<void>((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<IIpcResponse>((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;
}
}

+ 13
- 5
services/request/src/server/services/Ipc/SeekerIpcClient.ts View File

@ -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<Application>
{
/**
* 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 -------------------------------------------------------------------------------------


+ 0
- 63
services/request/src/server/services/Ipc/TorrentIpcClient.ts View File

@ -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 <string>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 <ITorrent[]>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 <ISerializedTorrent[]>response.response;
}
}

+ 0
- 1
services/request/src/server/services/index.ts View File

@ -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";


Loading…
Cancel
Save