@metacall/faas
Version:
Reimplementation of MetaCall FaaS platform written in TypeScript.
68 lines (67 loc) • 2.9 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.deployProcess = void 0;
const child_process_1 = require("child_process");
const path_1 = __importDefault(require("path"));
const app_1 = require("../app");
const protocol_1 = require("../worker/protocol");
const invoke_1 = require("./invoke");
const logger_1 = require("./logger");
const deployProcess = async (resource) => {
// Spawn a new process
const desiredPath = path_1.default.join(path_1.default.resolve(__dirname, '..'), 'worker', 'index.js');
const proc = child_process_1.spawn('metacall', [desiredPath], {
stdio: ['pipe', 'pipe', 'pipe', 'ipc']
});
// Send load message with the deploy information
proc.send({
type: protocol_1.WorkerMessageType.Load,
data: resource
});
// Pipe the stdout and stderr to the logger
logger_1.logProcessOutput(proc, resource.id);
// Wait for load result
let deployResolve;
let deployReject;
const promise = new Promise((resolve, reject) => {
deployResolve = resolve;
deployReject = reject;
});
proc.on('message', (payload) => {
switch (payload.type) {
case protocol_1.WorkerMessageType.MetaData: {
// Get the deploy data and store the process and app into our tables
const application = app_1.Applications[resource.id];
const deployment = payload.data;
application.proc = proc;
application.deployment = deployment;
deployResolve();
break;
}
case protocol_1.WorkerMessageType.InvokeResult: {
const invokeResult = payload.data;
// Get the invocation id in order to retrieve the callbacks
// for resolving the call, this deletes the invocation object
const invoke = invoke_1.invokeQueue.get(invokeResult.id);
invoke.resolve(JSON.stringify(invokeResult.result));
break;
}
default:
break;
}
});
proc.on('exit', code => {
// The application may have been ended unexpectedly,
// probably segmentation fault (exit code 139 in Linux)
deployReject(new Error(`Deployment '${resource.id}' process exited with code: ${code || 'unknown'}`));
// TODO: How to implement the exit properly? We cannot reject easily
// the promise from the call if the process exits during the call.
// Also if exits during the call it will try to call deployReject
// which is completely out of scope and the promise was fullfilled already
});
return promise;
};
exports.deployProcess = deployProcess;