import "dotenv/config"; import * as admin from "firebase-admin"; import { listPlaysExpired, terminatePlay } from "../src/stores"; import * as akashicModule from "../src/modules/akashic"; import { AkashicError } from "../src/akashic"; import { app as config } from "../src/config/default"; if (process.env.NODE_ENV != "production") { admin.initializeApp(); } else { if (process.env.GOOGLE_APPLICATION_CREDENTIALS == null) { throw new Error("credential not found"); } const serviceAccount = require(process.env.GOOGLE_APPLICATION_CREDENTIALS!); admin.initializeApp({ credential: admin.credential.cert(serviceAccount), }); } async function test() { const now = new Date(); const firestore = admin.firestore(); const akashicClient = akashicModule.createWrapper({ mode: config.akashic.mode, baseUrl: config.akashic.baseUrl, apiKey: config.akashic.apiKey, refreshToken: config.akashic.refreshToken, refreshTokenUrl: config.akashic.refreshTokenUrl, firestore: firestore, }); const plays = await listPlaysExpired(firestore, now); if (plays.length === 0) return; console.info(`期限切れのPlayが ${plays.length} 件見つかりました。`, { task: "CheckExpiredPlays", }); for (const play of plays) { if (play.akashicPlayId != null) { try { await akashicClient.stopInstance(play.akashicPlayId); } catch (error) { const apiError = error as AkashicError; if (apiError.response.status === 409) { console.info(`playId: ${play.akashicPlayId} のインスタンスはすでに終了しています`, { task: "CheckExpiredPlays", }); } else { throw apiError; } } } try { await terminatePlay(firestore, play.id); } catch (error) { const apiError = error as AkashicError; if (apiError.response.status === 409) { console.info(`playId: ${play.akashicPlayId} のプレイはすでに終了しています`, { task: "CheckExpiredPlays", }); } else { throw apiError; } } } } test();