Skip to content

Commit

Permalink
Improve overall code, solved bugs and make tests work.
Browse files Browse the repository at this point in the history
  • Loading branch information
viferga committed Jun 4, 2024
1 parent 16c492f commit 7541399
Show file tree
Hide file tree
Showing 22 changed files with 380 additions and 514 deletions.
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
engine-strict=true
42 changes: 11 additions & 31 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
"url": "https://github.com/metacall/faas/issues"
},
"homepage": "https://github.com/metacall/faas#readme",
"engines": {
"npm": ">=10.0.0",
"node": ">=20.1.0 || >=v18.17.0"
},
"prettier": {
"tabWidth": 4,
"useTabs": true,
Expand Down Expand Up @@ -90,14 +94,12 @@
"colors": "^1.4.0",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"git-clone": "^0.2.0",
"unzipper": "^0.10.11"
},
"devDependencies": {
"@types/busboy": "^1.3.0",
"@types/express": "^4.17.15",
"@types/git-clone": "^0.2.0",
"@types/node": "^14.14.7",
"@types/node": "^18.17.0",
"@types/unzipper": "^0.10.5",
"@typescript-eslint/eslint-plugin": "^4.7.0",
"@typescript-eslint/parser": "^4.7.0",
Expand All @@ -109,4 +111,4 @@
"prettier": "^2.1.2",
"typescript": "^4.3.2"
}
}
}
68 changes: 3 additions & 65 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,16 @@
import { DeployStatus, MetaCallJSON } from '@metacall/protocol/deployment';
import { ChildProcess } from 'child_process';

export interface Deployment {
id: string;
type?: string;
path: string;
jsons: MetaCallJSON[];
runners?: string[];
path: string;
type?: string;
blob?: string;
}

export const deploymentMap: Record<string, Promise<Deployment>> = {};

export const createInstallDependenciesScript = (
runner: string,
path: string
): string => {
const installDependenciesScript: Record<string, string> = {
python: `cd ${path} && metacall pip3 install -r requirements.txt`,
nodejs: `cd ${path} && metacall npm i`,
csharp: `cd ${path} && metacall dotnet restore && metacall dotnet release;`,
ruby: `cd ${path} && metacall bundle install`
};
return installDependenciesScript[runner];
};

export type tpackages = Record<string, unknown>;

// TODO: Isn't this available inside protocol package? We MUST reuse it
Expand Down Expand Up @@ -63,54 +49,6 @@ export class App implements IApp {
}
}

export type IAppWithFunctions = IApp & {
funcs: string[];
};

export type IAllApps = Record<string, IAppWithFunctions>;
export type IAllApps = Record<string, IApp>;

export const allApplications: IAllApps = {};

export enum ProtocolMessageType {
Install = 'InstallDependencies',
Load = 'LoadFunctions',
MetaData = 'GetApplicationMetadata',
Invoke = 'CallFunction',
InvokeResult = 'FunctionInvokeResult'
}

export const childProcesses: { [key: string]: ChildProcess } = {};

export interface WorkerMessage<T> {
type: ProtocolMessageType;
data: T;
}

export type WorkerMessageUnknown = WorkerMessage<unknown>;

export interface InspectObject {
[key: string]: Array<{ name: string }>;
}
export interface LogMessage {
deploymentName: string;
workerPID: number;
message: string;
}

export const ANSICode: number[] = [
166, 154, 142, 118, 203, 202, 190, 215, 214, 32, 6, 4, 220, 208, 184, 172
];

export interface PIDToColorCodeMapType {
[key: string]: number;
}

export interface AssignedColorCodesType {
[key: string]: boolean;
}

// Maps a PID to a color code
export const PIDToColorCodeMap: PIDToColorCodeMapType = {};

// Tracks whether a color code is assigned
export const assignedColorCodes: AssignedColorCodesType = {};
20 changes: 10 additions & 10 deletions src/controller/call.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { NextFunction, Request, Response } from 'express';
import {
ProtocolMessageType,
WorkerMessageUnknown,
childProcesses
} from '../constants';
import AppError from '../utils/appError';
import {
Processes,
WorkerMessageType,
WorkerMessageUnknown
} from '../worker/master';

export const callFunction = (
req: Request,
Expand All @@ -22,7 +22,7 @@ export const callFunction = (
const { appName: app, name } = req.params;
const args = Object.values(req.body);

if (!(app in childProcesses)) {
if (!(app in Processes)) {
return res
.status(404)
.send(
Expand All @@ -33,18 +33,18 @@ export const callFunction = (
let responseSent = false; // Flag to track if response has been sent
let errorCame = false;

childProcesses[app].send({
type: ProtocolMessageType.Invoke,
Processes[app].send({
type: WorkerMessageType.Invoke,
data: {
name,
args
}
});

childProcesses[app].on('message', (message: WorkerMessageUnknown) => {
Processes[app].on('message', (message: WorkerMessageUnknown) => {
if (!responseSent) {
// Check if response has already been sent
if (message.type === ProtocolMessageType.InvokeResult) {
if (message.type === WorkerMessageType.InvokeResult) {
responseSent = true; // Set flag to true to indicate response has been sent
return res.send(JSON.stringify(message.data));
} else {
Expand Down
13 changes: 13 additions & 0 deletions src/controller/catch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { NextFunction, Request, RequestHandler, Response } from 'express';

export const catchAsync = (
fn: (
req: Request,
res: Response,
next: NextFunction
) => Promise<Response | void>
): RequestHandler => {
return (req: Request, res: Response, next: NextFunction) => {
return fn(req, res, next).catch(err => next(err));
};
};
20 changes: 10 additions & 10 deletions src/controller/delete.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { NextFunction, Request, Response } from 'express';

import { ChildProcess } from 'child_process';
import { rm } from 'fs/promises';
import { join } from 'path';

import { allApplications, childProcesses } from '../constants';
import { allApplications } from '../constants';
import { appsDirectory } from '../utils/config';
import { catchAsync, ensureFolderExists } from '../utils/utils';
import { ensureFolderExists } from '../utils/filesystem';
import { Processes } from '../worker/master';
import { catchAsync } from './catch';

const deleteStatusMessage = (
app: string
Expand Down Expand Up @@ -41,21 +42,20 @@ export const deployDelete = catchAsync(
// Initialize isError flag
let isError = false;

// Check if the application exists in childProcesses and allApplications objects
if (!(app in childProcesses && app in allApplications)) {
// Check if the application exists in Processes and allApplications objects
if (!(app in Processes && app in allApplications)) {
isError = true;
return res.send(deleteStatusMessage(app)['error']);
}

// Retrieve the child process associated with the application and kill it
const childProcessesInApplications: ChildProcess = childProcesses[app];
childProcessesInApplications.kill();
Processes[app].kill();

// Remove the application from childProcesses and allApplications objects
delete childProcesses[app];
// Remove the application from Processes and allApplications objects
delete Processes[app];
delete allApplications[app];

if (app in childProcesses && app in allApplications) {
if (app in Processes && app in allApplications) {
isError = true;
return res.send(deleteStatusMessage(app)['appShouldntExist']);
}
Expand Down
Loading

0 comments on commit 7541399

Please sign in to comment.