From 35ed26965dfa8fa52fd10d005417e72817ccf851 Mon Sep 17 00:00:00 2001 From: Ryan Goetz Date: Thu, 15 Feb 2024 06:06:47 -1000 Subject: [PATCH] Hook up the background transpiler. *Triggers the transpilation process upon server startup and at regular intervals (every 5 minutes). --- sequencing-server/src/app.ts | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/sequencing-server/src/app.ts b/sequencing-server/src/app.ts index dd1370f406..ff070d2c74 100644 --- a/sequencing-server/src/app.ts +++ b/sequencing-server/src/app.ts @@ -27,6 +27,7 @@ import { getHasuraSession, canUserPerformAction, ENDPOINTS_WHITELIST } from './u import type { Result } from '@nasa-jpl/aerie-ts-user-code-runner/build/utils/monads'; import type { CacheItem, UserCodeError } from '@nasa-jpl/aerie-ts-user-code-runner'; import { PromiseThrottler } from './utils/PromiseThrottler.js'; +import { backgroundTranspiler } from './backgroundTranspiler.js'; const logger = getLogger('app'); @@ -39,7 +40,9 @@ app.use(bodyParser.json({ limit: '100mb' })); DbExpansion.init(); export const db = DbExpansion.getDb(); - +export let graphqlClient = new GraphQLClient(getEnv().MERLIN_GRAPHQL_URL, { + headers: { 'x-hasura-admin-secret': getEnv().HASURA_GRAPHQL_ADMIN_SECRET }, +}); export const piscina = new Piscina({ filename: new URL('worker.js', import.meta.url).pathname, minThreads: parseInt(getEnv().SEQUENCING_WORKER_NUM), @@ -62,10 +65,6 @@ export type Context = { }; app.use(async (req: Request, res: Response, next: NextFunction) => { - const graphqlClient = new GraphQLClient(getEnv().MERLIN_GRAPHQL_URL, { - headers: { 'x-hasura-admin-secret': getEnv().HASURA_GRAPHQL_ADMIN_SECRET }, - }); - // Check and make sure the user making the request has the required permissions. if ( !ENDPOINTS_WHITELIST.has(req.url) && @@ -248,3 +247,19 @@ app.listen(PORT, () => { Total workers started: ${piscina.threads.length}, Heap Size per Worker: ${getEnv().SEQUENCING_MAX_WORKER_HEAP_MB} MB`); }); + +if (getEnv().TRANSPILER_ENABLED === 'true') { + //log that the tranpiler is on + logger.info(`Background Transpiler is 'on'`); + // Immediately call the background transpiler + await backgroundTranspiler(); + // Then continue calling it every 5 minutes + let transpilerInProgress: boolean = false; + setInterval(async () => { + if (!transpilerInProgress) { + transpilerInProgress = true; + await backgroundTranspiler(); + transpilerInProgress = false; + } + }, 60 * 5 * 1000); +}