From a32374a3ac62d4d12c18ca7c37836e5322784dcb Mon Sep 17 00:00:00 2001 From: Eli Bosley Date: Tue, 5 Nov 2024 09:27:06 -0500 Subject: [PATCH] feat: attempt to fix pm2 --- api/Dockerfile | 2 + api/ecosystem.config.json | 4 +- api/package.json | 3 +- api/src/core/modules/get-services.ts | 66 +++++++++---------- api/src/dotenv.ts | 4 +- .../remote-graphql/remote-query.ts | 18 +++-- 6 files changed, 51 insertions(+), 46 deletions(-) diff --git a/api/Dockerfile b/api/Dockerfile index fa2198d32..2c61fa75b 100644 --- a/api/Dockerfile +++ b/api/Dockerfile @@ -35,6 +35,8 @@ EXPOSE 4000 FROM development AS builder +ENV NODE_ENV=production + COPY . . CMD ["npm", "run", "build-and-pack"] \ No newline at end of file diff --git a/api/ecosystem.config.json b/api/ecosystem.config.json index e387acfba..e11200482 100644 --- a/api/ecosystem.config.json +++ b/api/ecosystem.config.json @@ -2,8 +2,8 @@ "apps": [ { "name": "unraid-api", - "script": "node", - "args": "dist/main.js", + "script": "npm", + "args": "start", "log": "/var/log/unraid-api/unraid-api.log", "exec_mode": "fork", "ignore_watch": [ diff --git a/api/package.json b/api/package.json index e97692cff..ed2d9d01a 100644 --- a/api/package.json +++ b/api/package.json @@ -7,8 +7,9 @@ "author": "Lime Technology, Inc. ", "license": "UNLICENSED", "scripts": { + "start": "node dist/main.js", "build:docker": "./scripts/dc.sh run --rm builder", - "build": "vite build", + "build": "vite build --mode=production", "postbuild": "chmod +x dist/main.js && chmod +x dist/cli.js", "build-and-pack": "./scripts/build.mjs", "codegen": "MOTHERSHIP_GRAPHQL_LINK='https://staging.mothership.unraid.net/ws' graphql-codegen --config codegen.yml -r dotenv/config './.env.staging'", diff --git a/api/src/core/modules/get-services.ts b/api/src/core/modules/get-services.ts index 91be99724..fd296e70e 100644 --- a/api/src/core/modules/get-services.ts +++ b/api/src/core/modules/get-services.ts @@ -4,27 +4,22 @@ import type { CoreResult, CoreContext } from '@app/core/types'; import { getUnraidApiService } from '@app/core/modules/services/get-unraid-api'; import { NODE_ENV } from '@app/environment'; -const devNames = [ - 'emhttpd', - 'rest-api', -]; +const devNames = ['emhttpd', 'rest-api']; -const coreNames = [ - 'unraid-api', -]; +const coreNames = ['unraid-api']; interface Service { - online: boolean; - uptime: string; - version: string; + online: boolean; + uptime: string; + version: string; } interface ServiceResult extends CoreResult { - json: Service; + json: Service; } interface ServiceWithName extends Service { - name: string; + name: string; } /** @@ -33,39 +28,40 @@ interface ServiceWithName extends Service { * @param services * @param names */ -const addNameToService = (services: ServiceResult[], names: string[]): ServiceWithName[] => services.map((service, index) => ({ - name: names[index], - ...service.json, -})); +const addNameToService = (services: ServiceResult[], names: string[]): ServiceWithName[] => + services.map((service, index) => ({ + name: names[index], + ...service.json, + })); interface Result extends CoreResult { - json: ServiceWithName[]; + json: ServiceWithName[]; } /** * Get all services. */ export const getServices = async (context: CoreContext): Promise => { - const logErrorAndReturnEmptyArray = (error: Error) => { - logger.error(error); - return []; - }; + const logErrorAndReturnEmptyArray = (error: Error) => { + logger.error(error); + return []; + }; - const devServices: ServiceResult[] = NODE_ENV === 'development' ? await Promise.all([ - getEmhttpdService(context), - ]).catch(logErrorAndReturnEmptyArray) as ServiceResult[] : []; + const devServices: ServiceResult[] = (await Promise.all([getEmhttpdService(context)]).catch( + logErrorAndReturnEmptyArray + )) as ServiceResult[]; - const coreServices: ServiceResult[] = await Promise.all([ - getUnraidApiService(context), - ]).catch(logErrorAndReturnEmptyArray) as ServiceResult[]; + const coreServices: ServiceResult[] = (await Promise.all([getUnraidApiService(context)]).catch( + logErrorAndReturnEmptyArray + )) as ServiceResult[]; - const result = [ - ...addNameToService(devServices, devNames), - ...addNameToService(coreServices, coreNames), - ]; + const result = [ + ...addNameToService(devServices, devNames), + ...addNameToService(coreServices, coreNames), + ]; - return { - text: `Services: ${JSON.stringify(result, null, 2)}`, - json: result, - }; + return { + text: `Services: ${JSON.stringify(result, null, 2)}`, + json: result, + }; }; diff --git a/api/src/dotenv.ts b/api/src/dotenv.ts index 39c2a6325..a3590ba10 100644 --- a/api/src/dotenv.ts +++ b/api/src/dotenv.ts @@ -1,8 +1,10 @@ +import { logger } from '@app/core/log'; import { config } from 'dotenv'; export const env = process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test' - ? config({ debug: true, path: `./.env.${process.env.NODE_ENV}` }) + ? config({ debug: true, path: `./.env.${process.env.NODE_ENV}`, encoding: 'utf-8' }) : config({ path: '/usr/local/unraid-api/.env', + encoding: 'utf-8', }); diff --git a/api/src/graphql/resolvers/subscription/remote-graphql/remote-query.ts b/api/src/graphql/resolvers/subscription/remote-graphql/remote-query.ts index a2d97e989..49cf225ef 100644 --- a/api/src/graphql/resolvers/subscription/remote-graphql/remote-query.ts +++ b/api/src/graphql/resolvers/subscription/remote-graphql/remote-query.ts @@ -1,5 +1,4 @@ import { remoteQueryLogger } from '@app/core/log'; -import { ENVIRONMENT } from '@app/environment'; import { getApiApolloClient } from '@app/graphql/client/api/get-api-client'; import { RemoteGraphQLEventType, @@ -20,17 +19,19 @@ export const executeRemoteGraphQLQuery = async ( try { const parsedQuery = parseGraphQLQuery(originalBody); const localClient = getApiApolloClient({ - upcApiKey: apiKey + upcApiKey: apiKey, }); - if (ENVIRONMENT === 'development') { - remoteQueryLogger.debug({ query: parsedQuery.query }, '[DEVONLY] Running query'); - } + remoteQueryLogger.trace({ query: parsedQuery.query }, '[DEVONLY] Running query'); const localResult = await localClient.query({ query: parsedQuery.query, variables: parsedQuery.variables, }); if (localResult.data) { - remoteQueryLogger.trace({ data: localResult.data }, 'Got data from remoteQuery request', data.sha256); + remoteQueryLogger.trace( + { data: localResult.data }, + 'Got data from remoteQuery request', + data.sha256 + ); await client?.mutate({ mutation: SEND_REMOTE_QUERY_RESPONSE, @@ -71,7 +72,10 @@ export const executeRemoteGraphQLQuery = async ( } catch (error) { remoteQueryLogger.warn('Could not respond %o', error); } - remoteQueryLogger.error('Error executing remote query %s', err instanceof Error ? err.message: 'Unknown Error'); + remoteQueryLogger.error( + 'Error executing remote query %s', + err instanceof Error ? err.message : 'Unknown Error' + ); remoteQueryLogger.trace(err); } };