feat: attempt to fix pm2

This commit is contained in:
Eli Bosley
2024-11-05 09:27:06 -05:00
parent cb6534d9d9
commit a32374a3ac
6 changed files with 51 additions and 46 deletions

View File

@@ -35,6 +35,8 @@ EXPOSE 4000
FROM development AS builder
ENV NODE_ENV=production
COPY . .
CMD ["npm", "run", "build-and-pack"]

View File

@@ -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": [

View File

@@ -7,8 +7,9 @@
"author": "Lime Technology, Inc. <unraid.net>",
"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'",

View File

@@ -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<Result> => {
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,
};
};

View File

@@ -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',
});

View File

@@ -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);
}
};