|
|
@ -1,6 +1,8 @@ |
|
|
|
import { ITorrent } from "@autoplex-api/torrent"; |
|
|
|
import { ITorrentLink } from "@autoplex-api/torrent-search"; |
|
|
|
import { MovieTicket, MovieTorrent } from "@autoplex/database"; |
|
|
|
import { InternalService, Microservice } from "@autoplex/microservice"; |
|
|
|
import { sleep } from "@autoplex/utils"; |
|
|
|
import diskusage from "diskusage"; |
|
|
|
import { readdir } from "fs/promises"; |
|
|
|
import Supervisor from "./Supervisor"; |
|
|
@ -46,6 +48,16 @@ export default class TorrentManager extends InternalService |
|
|
|
*/ |
|
|
|
protected torrentIpc!: TorrentIpc; |
|
|
|
|
|
|
|
/** |
|
|
|
* Cache torrents for quick reference |
|
|
|
*/ |
|
|
|
private __cachedTorrents: { [id: string]: ITorrent} = {}; |
|
|
|
|
|
|
|
/** |
|
|
|
* Indicate if the service is running |
|
|
|
*/ |
|
|
|
private __isRunning: boolean = false; |
|
|
|
|
|
|
|
/** |
|
|
|
* Boot the Torrent Manager service |
|
|
|
*/ |
|
|
@ -67,6 +79,16 @@ export default class TorrentManager extends InternalService |
|
|
|
*/ |
|
|
|
public override start() { |
|
|
|
this.torrentIpc.on("connected", this.onConnect.bind(this)); |
|
|
|
this.__isRunning = true; |
|
|
|
this.updateCacheLoop(); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Shutdown the service |
|
|
|
*/ |
|
|
|
public override shutdown() { |
|
|
|
this.__isRunning = false; |
|
|
|
return super.shutdown(); |
|
|
|
} |
|
|
|
|
|
|
|
// Interface methods ---------------------------------------------------------------------------
|
|
|
@ -153,6 +175,36 @@ export default class TorrentManager extends InternalService |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
// Torrent Management --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/** |
|
|
|
* Update the torrent cache as long as the service is running |
|
|
|
*/ |
|
|
|
protected async updateCacheLoop() { |
|
|
|
const THROTTLE = 500; |
|
|
|
let lastUpdate = 0; |
|
|
|
while (this.__isRunning) { |
|
|
|
await sleep(THROTTLE - (Date.now() - lastUpdate)); |
|
|
|
lastUpdate = Date.now(); |
|
|
|
await this.updateTorrentCache(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Update the torrent cache |
|
|
|
*/ |
|
|
|
protected async updateTorrentCache() { |
|
|
|
try { |
|
|
|
let torrents = await this.torrentIpc.list(); |
|
|
|
this.__cachedTorrents = {}; |
|
|
|
for (let torrent of torrents) { |
|
|
|
this.__cachedTorrents[torrent.infoHash] = torrent; |
|
|
|
} |
|
|
|
} catch(e) { |
|
|
|
console.log("Failed to update torrent cache"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// Event Handling ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/** |
|
|
@ -175,4 +227,13 @@ export default class TorrentManager extends InternalService |
|
|
|
let details = (await this.torrentIpc.details([infoHash]))[0]; |
|
|
|
this.app.service<Supervisor>("Supervisor").onMovieTorrentFinished(torrent, details); |
|
|
|
} |
|
|
|
|
|
|
|
// Accessors -----------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/** |
|
|
|
* Get the list of cached torrents |
|
|
|
*/ |
|
|
|
public get cachedTorrents() { |
|
|
|
return this.__cachedTorrents; |
|
|
|
} |
|
|
|
} |