|
|
@ -1,18 +1,16 @@ |
|
|
|
import Application from "../Application"; |
|
|
|
import { IpcClient as TorrentSearchIpc, ITorrentLink } from "@autoplex-api/torrent-search"; |
|
|
|
import { ISerializedTorrent } from "@autoplex-api/torrent"; |
|
|
|
import { MovieTicket, MovieTorrent } from "@autoplex/database"; |
|
|
|
import MovieSearch from "./MovieSearch"; |
|
|
|
import { InternalService, Microservice } from "@autoplex/microservice"; |
|
|
|
import PostProcessor from "./PostProcessor"; |
|
|
|
import { InternalService } from "@autoplex/microservice"; |
|
|
|
import TorrentManager from "./TorrentManager"; |
|
|
|
import { ISerializedTorrent } from "@autoplex-api/torrent"; |
|
|
|
|
|
|
|
export default class Supervisor extends InternalService<Application> |
|
|
|
export default class Supervisor extends InternalService |
|
|
|
{ |
|
|
|
|
|
|
|
/** |
|
|
|
* The movie search service instance |
|
|
|
*/ |
|
|
|
protected movieSearch!: MovieSearch; |
|
|
|
protected torrentSearch!: TorrentSearchIpc; |
|
|
|
|
|
|
|
/** |
|
|
|
* The torrent client IPC service instance |
|
|
@ -25,24 +23,23 @@ export default class Supervisor extends InternalService<Application> |
|
|
|
protected postProcessor!: PostProcessor; |
|
|
|
|
|
|
|
/** |
|
|
|
* Create a new supervisor service instance |
|
|
|
* The service name |
|
|
|
*/ |
|
|
|
public constructor(app: Application) { |
|
|
|
super(app); |
|
|
|
} |
|
|
|
public readonly NAME = "Supervisor"; |
|
|
|
|
|
|
|
/** |
|
|
|
* The service name |
|
|
|
* Link to other internal services |
|
|
|
*/ |
|
|
|
public readonly NAME = "Supervisor"; |
|
|
|
public link(app: Microservice) { |
|
|
|
this.torrentSearch = app.service<TorrentSearchIpc>("Torrent Search"); |
|
|
|
this.torrentManager = app.service<TorrentManager>("Torrent Manager"); |
|
|
|
this.postProcessor = app.service<PostProcessor>("Post Processor"); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* All services are booted and ready |
|
|
|
*/ |
|
|
|
public start() { |
|
|
|
this.movieSearch = this.app.service<MovieSearch>("Movie Search"); |
|
|
|
this.torrentManager = this.app.service<TorrentManager>("Torrent Manager"); |
|
|
|
this.postProcessor = this.app.service<PostProcessor>("Post Processor"); |
|
|
|
this.searchMovies(); |
|
|
|
} |
|
|
|
|
|
|
@ -52,15 +49,19 @@ export default class Supervisor extends InternalService<Application> |
|
|
|
* 1. A movie ticket has been added/torrent has gone stale, so |
|
|
|
* dispatch a search task for a torrent |
|
|
|
*/ |
|
|
|
public onMovieTicketNeedsTorrent(ticket: MovieTicket) { |
|
|
|
this.log("A movie needs a torrent:", ticket.title); |
|
|
|
this.movieSearch.enqueueMovie(ticket); |
|
|
|
public async onMovieTicketNeedsTorrent(ticket: MovieTicket) { |
|
|
|
let link = await this.torrentSearch.searchMovie(ticket.title, ticket.imdbId, ticket.year); |
|
|
|
if (link === null) { |
|
|
|
this.log("Could not find a torrent for:", ticket.title); |
|
|
|
return; |
|
|
|
} |
|
|
|
this.onMovieTorrentFound(ticket, link); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 2. A movie torrent has been found, so add it to the client |
|
|
|
*/ |
|
|
|
public onMovieTorrentFound(ticket: MovieTicket, link: string) { |
|
|
|
public onMovieTorrentFound(ticket: MovieTicket, link: ITorrentLink) { |
|
|
|
this.log("A torrent was found for movie:", ticket.title); |
|
|
|
this.torrentManager.enqueueMovie(ticket, link); |
|
|
|
} |
|
|
@ -84,18 +85,20 @@ export default class Supervisor extends InternalService<Application> |
|
|
|
// Tasks ---------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/** |
|
|
|
* @TODO Performing a promise-all instead of waiting between each movie may be much faster |
|
|
|
* Search available movies in the database |
|
|
|
*/ |
|
|
|
public async searchMovies() { |
|
|
|
let movies = await MovieTicket.find({where: {isFulfilled: false}, relations: [ "torrents" ]}); |
|
|
|
for (let movie of movies) { |
|
|
|
let tickets = await MovieTicket.find({ |
|
|
|
where: {isFulfilled: false, isCanceled: false}, |
|
|
|
relations: [ "torrents" ] |
|
|
|
}); |
|
|
|
for (let ticket of tickets) { |
|
|
|
// Skip already-resolved non-stale torrents
|
|
|
|
if (movie.torrents.length > 0 && !movie.isStale) { |
|
|
|
if (ticket.torrents.length > 0 && !ticket.isStale) { |
|
|
|
this.log("Skipping already satisfied ticket") |
|
|
|
continue; |
|
|
|
} |
|
|
|
this.onMovieTicketNeedsTorrent(movie); |
|
|
|
this.onMovieTicketNeedsTorrent(ticket); |
|
|
|
} |
|
|
|
} |
|
|
|
} |