From b04dc040d13e11e015b2b0bd11d82dad83cabfaa Mon Sep 17 00:00:00 2001 From: qwerzl Date: Wed, 18 Sep 2024 10:26:58 +0800 Subject: [PATCH] fix(ci): use circleci to run `update-club-info` --- .circleci/config.yml | 33 +++++++++++++++++++ .../functions/update-club-info-background.mts | 33 ------------------- utils/update-club-info.ts | 7 +++- 3 files changed, 39 insertions(+), 34 deletions(-) create mode 100644 .circleci/config.yml delete mode 100644 netlify/functions/update-club-info-background.mts diff --git a/.circleci/config.yml b/.circleci/config.yml new file mode 100644 index 00000000..a30f18da --- /dev/null +++ b/.circleci/config.yml @@ -0,0 +1,33 @@ +version: 2.1 + +jobs: + update-club-info: + docker: + - image: node:18 + resource_class: large + parallelism: 10 + + steps: + - checkout + - restore_cache: + name: Restore pnpm Package Cache + keys: + - pnpm-packages-{{ checksum "pnpm-lock.yaml" }} + - run: + name: Install pnpm package manager + command: | + corepack enable + corepack prepare pnpm@latest-9 --activate + pnpm config set store-dir .pnpm-store + - run: + name: Install Dependencies + command: | + pnpm install + - save_cache: + name: Save pnpm Package Cache + key: pnpm-packages-{{ checksum "pnpm-lock.yaml" }} + paths: + - .pnpm-store + - run: + name: Update Club Info + command: pnpm run update-club-info diff --git a/netlify/functions/update-club-info-background.mts b/netlify/functions/update-club-info-background.mts deleted file mode 100644 index 52a81b70..00000000 --- a/netlify/functions/update-club-info-background.mts +++ /dev/null @@ -1,33 +0,0 @@ -import type { Config, Handler } from '@netlify/functions' -import { getStore } from '@netlify/blobs' -import { schedule } from '@netlify/functions' -import { wrap } from '@netlify/integrations' - -import { withSentry } from '@netlify/sentry' -import type { Clubs } from '~/types/clubs' -import updateClubInfo from '~/utils/update-club-info' - -async function myHandler() { - const store = getStore('enspire') - const clubs: Clubs = await updateClubInfo() - await store.setJSON('clubs', clubs) - - return new Response('Done') -} - -const withIntegrations = wrap(withSentry) - -const config: Config = { - sentry: { - cronMonitoring: { - enable: true, - monitorId: 'update-club-info', - }, - }, -} - -const handlerWithIntegrations = withIntegrations(myHandler, config) as Handler - -const handler = schedule('@hourly', handlerWithIntegrations) - -export { handler } diff --git a/utils/update-club-info.ts b/utils/update-club-info.ts index 10ba5a48..d412efee 100644 --- a/utils/update-club-info.ts +++ b/utils/update-club-info.ts @@ -1,10 +1,10 @@ import type { ClubMemberRole } from '@prisma/client' +import { getStore } from '@netlify/blobs' import { PrismaClient } from '@prisma/client' import type { Clubs } from '~/types/clubs' import crawler from './crawler' const prisma = new PrismaClient() -const clubs: Clubs = await crawler() as Clubs interface ClubMembership { tsimsStudentId: number @@ -16,6 +16,8 @@ interface ClubMembership { const categories: (keyof Clubs)[] = ['Sports', 'Service', 'Arts', 'Life', 'Academic'] export default async function main() { + const clubs = await crawler() as Clubs + const runSequence = [] for (const category of categories) { const categoryClubs = clubs[category] @@ -128,6 +130,9 @@ export default async function main() { console.log(`start transaction with length ${runSequence.length}`) await prisma.$transaction(runSequence) + const store = getStore('enspire') + await store.setJSON('clubs', clubs) + return clubs }