Skip to content

Commit

Permalink
Clean up old executions and code run endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
jay-m-dev committed Sep 13, 2023
1 parent bdd400b commit bef0c62
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 82 deletions.
28 changes: 14 additions & 14 deletions lab/routes/execapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ const {
} = require('../execapiutils');

/**
* Route for submitting a new code execution.
*
* @param {Object} req - HTTP request object containing request data.
* @property {string} req.body.src_code - The source code to be executed.
* @property {string} [req.body.dataset_file_id] - The dataset file ID (optional).
* @property {string} [req.body.dataset_id] - The dataset ID (optional).
* The dataset_file_id OR the dataset_id are used to load the dataset into a
* Pandas DataFrame as variable 'df'
* @property {string} [req.body.experiment_id] - The experiment ID (optional).
* The experiment_id is used to load the experiment model as variable 'model'
* @param {Object} res - HTTP response object for sending the response back to the client.
* @param {Function} next - Function to proceed to the next middleware step.
*
* The only responsibility this function will have is to:
* 1. Check required and optional parameters
* 2. Create and save Executions
Expand Down Expand Up @@ -106,20 +119,7 @@ router.post('/executions', async (req, res, next) => {
}
});

/**
* Route for submitting a new code execution.
*
* @param {Object} req - HTTP request object containing request data.
* @property {string} req.body.src_code - The source code to be executed.
* @property {string} [req.body.dataset_file_id] - The dataset file ID (optional).
* @property {string} [req.body.dataset_id] - The dataset ID (optional).
* The dataset_file_id OR the dataset_id are used to load the dataset into a
* Pandas DataFrame as variable 'df'
* @property {string} [req.body.experiment_id] - The experiment ID (optional).
* The experiment_id is used to load the experiment model as variable 'model'
* @param {Object} res - HTTP response object for sending the response back to the client.
* @param {Function} next - Function to proceed to the next middleware step.
*/

router.post('/executions_old', async (req, res, next) => {
if (req.body.src_code == null) {
return res.status(400).json({ message: 'No src_code provided' });
Expand Down
87 changes: 21 additions & 66 deletions machine/machine.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,68 +217,6 @@ app.get("/projects", (req, res) => {
res.send(projects);
});

app.post('/code/run', jsonParser, (req, res) => {

// Create the temp path to store the code execuction generated files
let tmppath = path.join(process.env.CODE_RUN_PATH, req.body._id.toString());
if (!fs.existsSync(tmppath)) fs.mkdirSync(tmppath, { recursive: true });

let executionId = req.body._id;
let args = [
"machine/pyutils/run_code.py",
"--code",
req.body.src_code,
"--execution_id",
executionId,
];

if (req.body._dataset_file_id !== undefined) {
args.push("--dataset_file_id");
args.push(req.body._dataset_file_id);
}

if (req.body._experiment_id !== undefined) {
args.push("--experiment_id");
args.push(req.body._experiment_id);
}

let pyProc = spawn("python", args, { cwd: process.env.PROJECT_ROOT });

let result = req.body;
result.exec_results = {};

pyProc.stdout.on("data", (data) => {
result.exec_results = JSON.parse(data.toString());
});

pyProc.stderr.on("data", (data) => {
// console.log(`stderr: ${data}`);
try {
result.exec_results = JSON.parse(data.toString());
} catch (e) {
console.log(e);
}
});

pyProc.on("close", async (code) => {
console.log(`child process exited with code ${code}`);
const files = await machine_utils.sendExecFiles(executionId, tmppath);
result.exec_results.files = files;

// delete tmp folder
fs.rmdir(tmppath, { recursive: true }, (err) => {
if (err) {
console.error(err);
} else {
console.log(tmppath + ' folder deleted');
}
});

res.send(result);

});
});

// Starts experiment
app.post("/projects/:id", jsonParser, (req, res) => {
var experimentId = req.body._id;
Expand Down Expand Up @@ -574,14 +512,19 @@ app.post("/code/run/install", jsonParser, (req, res) => {
});

// run code execution
app.post("/code/run_old", jsonParser, (req, res) => {
app.post('/code/run', jsonParser, (req, res) => {

// Create the temp path to store the code execuction generated files
let tmppath = path.join(process.env.CODE_RUN_PATH, req.body._id.toString());
if (!fs.existsSync(tmppath)) fs.mkdirSync(tmppath, { recursive: true });

let executionId = req.body._id;
let args = [
"machine/pyutils/run_code.py",
"--code",
req.body.src_code,
"--execution_id",
req.body._id,
executionId,
];

if (req.body._dataset_file_id !== undefined) {
Expand Down Expand Up @@ -612,10 +555,22 @@ app.post("/code/run_old", jsonParser, (req, res) => {
}
});

pyProc.on("close", (code) => {
// result.code = code;
pyProc.on("close", async (code) => {
console.log(`child process exited with code ${code}`);
const files = await machine_utils.sendExecFiles(executionId, tmppath);
result.exec_results.files = files;

// delete tmp folder
fs.rmdir(tmppath, { recursive: true }, (err) => {
if (err) {
console.error(err);
} else {
console.log(tmppath + ' folder deleted');
}
});

res.send(result);

});
});

Expand Down
3 changes: 1 addition & 2 deletions machine/machine_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ const path = require("path");
const fetch = require('isomorphic-fetch');
const fs = require('fs');
const FormData = require('form-data');

// const { isConstructorDeclaration } = require('typescript');
const mime = require('mime-types');

//Generate a list of projects based on machine_config.json
var getProjects = function(algorithms) {
var project_list = [];
Expand Down

0 comments on commit bef0c62

Please sign in to comment.