Combine execute, execute_interactive functions

This commit is contained in:
Omar Brikaa 2022-09-30 22:25:30 +02:00
parent f40f7702de
commit d4ffccb32b
2 changed files with 30 additions and 54 deletions

View File

@ -218,7 +218,7 @@ router.ws('/connect', async (ws, req) => {
}) })
); );
await job.execute_interactive(event_bus); await job.execute(event_bus);
await job.cleanup(); await job.cleanup();
ws.close(4999, 'Job Completed'); ws.close(4999, 'Job Completed');

View File

@ -245,7 +245,7 @@ class Job {
}); });
} }
async execute() { async execute(event_bus = null) {
if (this.state !== job_states.PRIMED) { if (this.state !== job_states.PRIMED) {
throw new Error( throw new Error(
'Job must be in primed state, current state: ' + 'Job must be in primed state, current state: ' +
@ -256,32 +256,54 @@ class Job {
this.logger.info(`Executing job runtime=${this.runtime.toString()}`); this.logger.info(`Executing job runtime=${this.runtime.toString()}`);
const code_files = this.files.filter(file => file.encoding == 'utf8'); const code_files = this.files.filter(file => file.encoding == 'utf8');
this.logger.debug('Compiling');
let compile; let compile;
let compile_errored = false; let compile_errored = false;
const { emit_event_bus_result, emit_event_bus_stage } =
event_bus === null
? {
emit_event_bus_result: () => {},
emit_event_bus_stage: () => {},
}
: {
emit_event_bus_result: (stage, result, event_bus) => {
const { error, code, signal } = result;
event_bus.emit('exit', stage, {
error,
code,
signal,
});
},
emit_event_bus_stage: (stage, event_bus) => {
event_bus.emit('stage', stage);
},
};
if (this.runtime.compiled) { if (this.runtime.compiled) {
this.logger.debug('Compiling');
emit_event_bus_stage('compile', event_bus);
compile = await this.safe_call( compile = await this.safe_call(
this.runtime.compile, this.runtime.compile,
code_files.map(x => x.name), code_files.map(x => x.name),
this.timeouts.compile, this.timeouts.compile,
this.memory_limits.compile this.memory_limits.compile,
event_bus
); );
emit_event_bus_result('compile', compile, event_bus);
compile_errored = compile.code !== 0; compile_errored = compile.code !== 0;
} }
let run; let run;
if (!compile_errored) { if (!compile_errored) {
this.logger.debug('Running'); this.logger.debug('Running');
emit_event_bus_stage('run', event_bus);
run = await this.safe_call( run = await this.safe_call(
this.runtime.run, this.runtime.run,
[code_files[0].name, ...this.args], [code_files[0].name, ...this.args],
this.timeouts.run, this.timeouts.run,
this.memory_limits.run this.memory_limits.run,
event_bus
); );
emit_event_bus_result('run', run, event_bus);
} }
this.state = job_states.EXECUTED; this.state = job_states.EXECUTED;
@ -294,52 +316,6 @@ class Job {
}; };
} }
async execute_interactive(event_bus) {
if (this.state !== job_states.PRIMED) {
throw new Error(
'Job must be in primed state, current state: ' +
this.state.toString()
);
}
this.logger.info(
`Interactively executing job runtime=${this.runtime.toString()}`
);
const code_files = this.files.filter(file => file.encoding == 'utf8');
let compile_errored = false;
if (this.runtime.compiled) {
event_bus.emit('stage', 'compile');
const { error, code, signal } = await this.safe_call(
this.runtime.compile,
code_files.map(x => x.name),
this.timeouts.compile,
this.memory_limits.compile,
event_bus
);
event_bus.emit('exit', 'compile', { error, code, signal });
compile_errored = code !== 0;
}
if (!compile_errored) {
this.logger.debug('Running');
event_bus.emit('stage', 'run');
const { error, code, signal } = await this.safe_call(
this.runtime.run,
[code_files[0].name, ...this.args],
this.timeouts.run,
this.memory_limits.run,
event_bus
);
event_bus.emit('exit', 'run', { error, code, signal });
}
this.state = job_states.EXECUTED;
}
cleanup_processes(dont_wait = []) { cleanup_processes(dont_wait = []) {
let processes = [1]; let processes = [1];
const to_wait = []; const to_wait = [];