diff --git a/.gitignore b/.gitignore index eb84e7e..18cfd66 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,4 @@ node_modules *.pem .env git -log.txt +runner_stdout.txt diff --git a/README.md b/README.md index e5128e7..bb48068 100644 --- a/README.md +++ b/README.md @@ -88,8 +88,6 @@ After it's running, the logs will be to the systemd journal: `sudo journalctl -u benchbot.service` -As well as to `./log.txt`. - # Github Settings ## Permissions diff --git a/bench.js b/bench.js index 121f831..2759e01 100644 --- a/bench.js +++ b/bench.js @@ -1,3 +1,4 @@ +const fs = require("fs") const cp = require("child_process") const path = require("path") @@ -8,6 +9,8 @@ function errorResult(message, error) { let cwd = process.cwd(); console.log(`process cwd: ${cwd}`); +const runnerOutput = path.join(__dirname, "runner_stdout.txt") + const Mutex = require('async-mutex').Mutex; const mutex = new Mutex(); var shell = require('shelljs'); @@ -19,7 +22,7 @@ function BenchContext(app, config) { self.app = app; self.config = config; - self.runTask = async function(cmd, { title, shouldLogOutput } = {}) { + self.runTask = function(cmd, { title, shouldLogOutput } = {}) { if (title) { app.log(title) } @@ -29,41 +32,21 @@ function BenchContext(app, config) { try { if (shouldLogOutput) { console.log(`<=== Start command output (cwd: ${process.cwd()})`) + cp.execFileSync("/bin/dash", ["-c", `${cmd} | tee ${runnerOutput}`], { stdio: "inherit" }) + stdout = fs.readFileSync(runnerOutput).toString() + } else { + stdout = cp.execSync(cmd, { stdio: "pipe", shell: true }).toString() } - - await new Promise(function (resolve) { - const proc = cp.spawn("/bin/bash", ["-c", cmd], { stdio: "pipe" }) - - proc.stdout.on("data", function (data) { - data = data.toString() - - if (data && shouldLogOutput) { - console.log(data.trim()) - } - - stdout += data - }) - - proc.stderr.on("data", function (data) { - data = data.toString() - - if (data && shouldLogOutput) { - console.log(data.trim()) - } - - stderr += data - }) - - proc.on("close", function (code) { - error = !!code - resolve() - }) - }) } catch (err) { error = true if (err.code) { + app.log(`Command ${cmd} failed with error code ${error.code}`); stdout = err.stdout.toString() + stderr = err.stderr.toString() + if (stderr) { + app.log(`stderr: ${stderr.trim()}`); + } } else { app.log.error("Caught exception in command execution") app.log.error(err) @@ -125,33 +108,33 @@ const prepareBranch = async function( const repositoryPath = path.join(gitDirectory, repo) var { url } = await getPushDomain() - await benchContext.runTask(`git clone ${url}/${owner}/${repo} ${repositoryPath}`); + benchContext.runTask(`git clone ${url}/${owner}/${repo} ${repositoryPath}`); shell.cd(repositoryPath) - var { error } = await benchContext.runTask(`git add . && git reset --hard HEAD`); + var { error } = benchContext.runTask(`git add . && git reset --hard HEAD`); if (error) return errorResult(stderr); - var { error, stdout } = await benchContext.runTask("git rev-parse HEAD"); + var { error, stdout } = benchContext.runTask("git rev-parse HEAD"); if (error) return errorResult(stderr); const detachedHead = stdout.trim() // Check out to the detached head so that any branch can be deleted - var { error, stderr } = await benchContext.runTask(`git checkout ${detachedHead}`); + var { error, stderr } = benchContext.runTask(`git checkout ${detachedHead}`); if (error) return errorResult(stderr); // Recreate PR remote - await benchContext.runTask(`git remote remove pr`); + benchContext.runTask(`git remote remove pr`); var { url } = await getPushDomain() - var { error, stderr } = await benchContext.runTask(`git remote add pr ${url}/${contributor}/${repo}.git`); + var { error, stderr } = benchContext.runTask(`git remote add pr ${url}/${contributor}/${repo}.git`); if (error) return errorResult(`Failed to add remote reference to ${owner}/${repo}`); // Fetch and recreate the PR's branch - await benchContext.runTask(`git branch -D ${branch}`); - var { error, stderr } = await benchContext.runTask(`git fetch pr ${branch} && git checkout --track pr/${branch}`, `Checking out ${branch}...`); + benchContext.runTask(`git branch -D ${branch}`); + var { error, stderr } = benchContext.runTask(`git fetch pr ${branch} && git checkout --track pr/${branch}`, `Checking out ${branch}...`); if (error) return errorResult(stderr); // Fetch and merge master - var { error, stderr } = await benchContext.runTask(`git pull origin ${baseBranch}`, `Merging branch ${baseBranch}`); + var { error, stderr } = benchContext.runTask(`git pull origin ${baseBranch}`, `Merging branch ${baseBranch}`); if (error) return errorResult(stderr); } @@ -178,7 +161,7 @@ function benchBranch(app, config) { var error = await prepareBranch(config, { benchContext }) if (error) return error - var { stderr, error, stdout } = await benchContext.runTask(benchCommand, { + var { stderr, error, stdout } = benchContext.runTask(benchCommand, { title: `Benching branch ${config.branch}...`, shouldLogOutput: true }); @@ -408,7 +391,7 @@ function benchmarkRuntime(app, config) { var error = await prepareBranch(config, { benchContext }) if (error) return error - var { stdout, stderr } = await benchContext.runTask(benchCommand, { + var { stdout, stderr } = benchContext.runTask(benchCommand, { title: `Benching runtime in branch ${config.branch}...`, shouldLogOutput: true }); @@ -418,15 +401,15 @@ function benchmarkRuntime(app, config) { if (benchCommand.includes("--output")) { const regex = /--output(?:=|\s+)(".+?"|\S+)/; const path = benchCommand.match(regex)[1]; - await benchContext.runTask(`git add ${path}`); - await benchContext.runTask(`git commit -m "${benchCommand}"`); + benchContext.runTask(`git add ${path}`); + benchContext.runTask(`git commit -m "${benchCommand}"`); const target = `${config.contributor}/${config.repo}` const { url, token } = await config.getPushDomain() try { - await benchContext.runTask(`git remote set-url pr ${url}/${target}.git`, "Setting up remote for PR"); - await benchContext.runTask(`git push pr HEAD`); + benchContext.runTask(`git remote set-url pr ${url}/${target}.git`, "Setting up remote for PR"); + benchContext.runTask(`git push pr HEAD`); } catch (error) { extraInfo = `NOTE: Failed to push commits to repository: ${error.toString().replace(token, "{secret}", "g")}` } diff --git a/run b/run index 9a83af6..3eb06ae 100755 --- a/run +++ b/run @@ -139,7 +139,7 @@ main() { source ~/.cargo/env && \ cd "$install_location" && \ yarn && \ - yarn start 2>&1 | tee -a log.txt + yarn start 2>&1 } follow_service_logs() {