diff options
author | Jakob Stendahl <14180120+JakobST1n@users.noreply.github.com> | 2021-10-11 20:02:04 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-11 20:02:04 +0200 |
commit | c67531161e56488166a33232f87566309ba8676e (patch) | |
tree | 846e59a020e80bea48557d5a06af5728e44961ff /src/NeoRuntimeManager/RuntimeProcess.js | |
parent | e6880cd8ccf82d993f222cb14b4860581654acb8 (diff) | |
parent | c1b6eec770b885a9829e1f62bad5cc99389ca429 (diff) | |
download | Luxcena-Neo-c67531161e56488166a33232f87566309ba8676e.tar.gz Luxcena-Neo-c67531161e56488166a33232f87566309ba8676e.zip |
Merge pull request #24 from JakobST1n/rebuild
v1.0.0
Diffstat (limited to 'src/NeoRuntimeManager/RuntimeProcess.js')
-rw-r--r-- | src/NeoRuntimeManager/RuntimeProcess.js | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/src/NeoRuntimeManager/RuntimeProcess.js b/src/NeoRuntimeManager/RuntimeProcess.js new file mode 100644 index 0000000..c5c4749 --- /dev/null +++ b/src/NeoRuntimeManager/RuntimeProcess.js @@ -0,0 +1,103 @@ +let fs = require("fs-extra"); +let spawn = require("child_process"); + +class RuntimeProcess { + + constructor(_modePath, _eventEmitter) { + this.modePath = _modePath; + this.logfile = `${this.modePath}/mode.log`; + this.errfile = `${this.modePath}/mode.error`; + + this.stdout = ""; + this.stderr = ""; + + this.fl = false; + this.proc = null; + + this.isRunning = false; + this.exitCode = null; + + this.eventEmitter = _eventEmitter; + } + + start() { + if (this.isRunning) { + console.log("PROCESS ALREADY RUNNING"); + return; + } + this.isRunning = true; + this.proc = spawn.spawn( + `${__appdir}/NeoRuntime/Runtime/venv/bin/python`, + //"python", + [ + "-u", // This makes us able to get real-time output + `${__appdir}/NeoRuntime/Runtime/neo_runtime.py`, + `--strip-config="${__configdir}/strip.ini"`, + `--mode-path="${this.modePath}"`, + `--mode-entry=script` + ] + ); + + this.proc.on('error', (err) => { + console.log(err); + }); + + fs.ensureFileSync(this.logfile); + fs.ensureFileSync(this.errfile); + this.eventEmitter.emit("proc:start"); + + this.proc.stdout.on('data', (_stdout) => { + let stdout_str = _stdout.toString(); + fs.appendFile(this.logfile, `[${timestamp()}]: ` + stdout_str); + this.eventEmitter.emit("proc:stdout", stdout_str); + }); + + this.proc.stdout.on('end', () => { + fs.appendFile(this.logfile, "\n"); + }); + + this.proc.stderr.on('data', (_stderr) => { + let stderr_str = _stderr.toString(); + fs.appendFile(this.errfile, `[${timestamp()}]: ` + stderr_str); + this.eventEmitter.emit("proc:stderr", stderr_str); + }); + + this.proc.stderr.on('end', () => { + fs.appendFile(this.logfile, "\n"); + }); + + this.proc.on('close', (code) => { + if (code) { + fs.appendFile(this.logfile, `[${timestamp()}]: ` + "Script exited with code " + code.toString()); + } + this.eventEmitter.emit("proc:exit", 0); + this.isRunning = false; + this.exitCode = code; + }); + + } + + stop(restart=false) { + try { + if (restart) { + this.proc.once("close", () => { + setTimeout(() => this.start(), 500); + }); + } + this.proc.kill("SIGINT"); + } catch (err) { + console.log(err); + } + } +} + +/** + * Creates and returns a timestamp that can be used in logfiles. + * + * @return {string} timestamp + */ +function timestamp() { + return (new Date()).toISOString(); +} + +module.exports = RuntimeProcess; |