From 14f121cbfaf595b275a15bf8562b2bd0f9ca17ae Mon Sep 17 00:00:00 2001 From: Tycho Bokdam Date: Wed, 11 Nov 2020 16:40:03 +0100 Subject: [PATCH] feat(scraper): Added status endpoint --- apps/api/src/status/status.service.ts | 4 +-- .../src/routes/status/status.controller.ts | 30 +++++++++++++++++++ .../src/routes/status/status.module.ts | 11 +++++++ apps/scraper/src/scraper.module.ts | 3 ++ 4 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 apps/scraper/src/routes/status/status.controller.ts create mode 100755 apps/scraper/src/routes/status/status.module.ts diff --git a/apps/api/src/status/status.service.ts b/apps/api/src/status/status.service.ts index 55ccfd7..15b307a 100755 --- a/apps/api/src/status/status.service.ts +++ b/apps/api/src/status/status.service.ts @@ -2,7 +2,7 @@ import { HttpService, Inject, Injectable } from '@nestjs/common' import { InjectModel } from '@nestjs/mongoose' import { checkSync } from 'diskusage' import * as getFolderSize from 'get-folder-size' -import { formatBytes, formatMsToRemaining } from '@pct-org/torrent/utils' +import { formatBytes } from '@pct-org/torrent/utils' import { MovieModel, ShowModel, EpisodeModel } from '@pct-org/mongo-models' import { Status } from './status.object-type' @@ -66,7 +66,7 @@ export class StatusService { status: response.data.status, updated: response.data.updated, nextUpdate: response.data.nextUpdate, - uptime: formatMsToRemaining(response.data.uptime * 1000) + uptime: response.data.uptime, } } catch (e) { diff --git a/apps/scraper/src/routes/status/status.controller.ts b/apps/scraper/src/routes/status/status.controller.ts new file mode 100644 index 0000000..8ec990e --- /dev/null +++ b/apps/scraper/src/routes/status/status.controller.ts @@ -0,0 +1,30 @@ +import { Controller, Get, Inject, OnApplicationBootstrap } from '@nestjs/common' +import { SchedulerRegistry } from '@nestjs/schedule' +import { formatMsToRemaining } from '@pct-org/torrent/utils' + +@Controller() +export class StatusController implements OnApplicationBootstrap { + + @Inject() + private schedulerRegistry: SchedulerRegistry + + private bootedSince: number + + public onApplicationBootstrap(): void { + this.bootedSince = Date.now() + } + + @Get('status') + public watch() { + const cron = this.schedulerRegistry.getCronJob('scraper') + + return { + status: 'ok', + version: 'beta', + updated: cron.lastDate() || 'never', + nextUpdate: cron.nextDates(), + uptime: formatMsToRemaining(Date.now() - this.bootedSince) + } + } + +} diff --git a/apps/scraper/src/routes/status/status.module.ts b/apps/scraper/src/routes/status/status.module.ts new file mode 100755 index 0000000..3528fe9 --- /dev/null +++ b/apps/scraper/src/routes/status/status.module.ts @@ -0,0 +1,11 @@ +import { Module } from '@nestjs/common' + +import { StatusController } from './status.controller' + +@Module({ + controllers: [ + StatusController + ] +}) +export class StatusModule { +} diff --git a/apps/scraper/src/scraper.module.ts b/apps/scraper/src/scraper.module.ts index 5f79272..3ebc8d0 100644 --- a/apps/scraper/src/scraper.module.ts +++ b/apps/scraper/src/scraper.module.ts @@ -10,12 +10,15 @@ import { ConfigService } from './shared/config/config.service' import { ProvidersModule } from './providers/providers.module' import { ProvidersService } from './providers/providers.service' +import { StatusModule } from './routes/status/status.module' + @Module({ imports: [ ConfigModule, ModelsModule, ProvidersModule, + StatusModule, // Enable Mongoose MongooseModule.forRootAsync({