From b567e205b0d5e5ee66c04430ac97ae5ed5ed33c4 Mon Sep 17 00:00:00 2001 From: Max Isom Date: Sun, 29 Oct 2023 21:07:51 -0700 Subject: [PATCH] Don't throw if Banner services are temporarily down --- src/tasks/scrape-instructors.ts | 11 ++++++++++- src/tasks/scrape-section-details.ts | 5 +++++ src/tasks/scrape-sections.ts | 14 ++++++++++++-- src/tasks/scrape-transfer-courses.ts | 10 +++++++++- 4 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/tasks/scrape-instructors.ts b/src/tasks/scrape-instructors.ts index ffcad08..b574938 100644 --- a/src/tasks/scrape-instructors.ts +++ b/src/tasks/scrape-instructors.ts @@ -2,6 +2,7 @@ import {Injectable, Logger} from '@nestjs/common'; import {Task, TaskHandler} from 'nestjs-graphile-worker'; import * as db from 'zapatos/db'; import {FetcherService} from 'src/fetcher/fetcher.service'; +import type {IFaculty} from '@mtucourses/scraper'; import {PoolService} from '~/pool/pool.service'; import {updateDeletedAtUpdatedAtForUpsert} from '~/lib/db-utils'; @@ -14,7 +15,15 @@ export class ScrapeInstructorsTask { @TaskHandler() async handler() { - const faculty = await this.fetcher.getAllFaculty(); + let faculty: IFaculty[] = []; + try { + faculty = await this.fetcher.getAllFaculty(); + } catch (error: unknown) { + if (error instanceof Error && error.message === 'Banner services are currently down.') { + this.logger.warn('Banner services are currently down. Skipping scraping instructors.'); + return; + } + } this.logger.log('Finished scraping website'); diff --git a/src/tasks/scrape-section-details.ts b/src/tasks/scrape-section-details.ts index 8dd150b..314414c 100644 --- a/src/tasks/scrape-section-details.ts +++ b/src/tasks/scrape-section-details.ts @@ -105,6 +105,11 @@ export class ScrapeSectionDetailsTask { section }; } catch (error: unknown) { + if (error instanceof Error && error.message === 'Banner services are currently down.') { + this.logger.warn('Banner services are currently down. Skipping scraping instructors.'); + return; + } + if ((error as Error).message === 'Course not found') { this.logger.log(`Did not find ${section.course.id}: ${JSON.stringify(section.course)}`); this.logger.log('It should be cleaned up automatically on the next course scrape.'); diff --git a/src/tasks/scrape-sections.ts b/src/tasks/scrape-sections.ts index 0618887..1d5c4fc 100644 --- a/src/tasks/scrape-sections.ts +++ b/src/tasks/scrape-sections.ts @@ -1,4 +1,4 @@ -import {Injectable} from '@nestjs/common'; +import {Injectable, Logger} from '@nestjs/common'; import pThrottle from 'p-throttle'; import type {ICourseOverview, ISection} from '@mtucourses/scraper'; import type {IRuleOptions} from 'src/lib/rschedule'; @@ -15,6 +15,8 @@ import {updateDeletedAtUpdatedAtForUpsert} from '~/lib/db-utils'; @Injectable() @Task('scrape-sections') export class ScrapeSectionsTask { + private readonly logger = new Logger(ScrapeSectionsTask.name); + constructor(private readonly pool: PoolService, private readonly fetcher: FetcherService) {} @TaskHandler() @@ -28,7 +30,15 @@ export class ScrapeSectionsTask { private async processTerm(term: Date) { const {semester, year} = dateToTerm(term); - const extCourses = await this.fetcher.getAllSections(term); + let extCourses: ICourseOverview[] = []; + try { + extCourses = await this.fetcher.getAllSections(term); + } catch (error: unknown) { + if (error instanceof Error && error.message === 'Banner services are currently down.') { + this.logger.warn('Banner services are currently down. Skipping scraping instructors.'); + return; + } + } await db.serializable(this.pool, async trx => { // Mark courses and sections in term as deleted by default diff --git a/src/tasks/scrape-transfer-courses.ts b/src/tasks/scrape-transfer-courses.ts index 5098e36..60b1084 100644 --- a/src/tasks/scrape-transfer-courses.ts +++ b/src/tasks/scrape-transfer-courses.ts @@ -15,7 +15,15 @@ export class ScrapeTransferCoursesTask { @TaskHandler() async handler() { - const extTransferCourses = await this.fetcher.getAllTransferCourses(); + let extTransferCourses: ITransferCourse[] = []; + try { + extTransferCourses = await this.fetcher.getAllTransferCourses(); + } catch (error: unknown) { + if (error instanceof Error && error.message === 'Banner services are currently down.') { + this.logger.warn('Banner services are currently down. Skipping scraping instructors.'); + return; + } + } // Filter out duplicates // todo: this should be handled by the scraper