Skip to content

Commit

Permalink
Merge pull request #613 from pablo-tx/master
Browse files Browse the repository at this point in the history
Backport #519 parallel requests fix
  • Loading branch information
HexF authored Jul 16, 2023
2 parents 919076e + 11841b3 commit 89e0dd4
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 105 deletions.
29 changes: 14 additions & 15 deletions api/src/api/v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@ router.use((req, res, next) => {

router.ws('/connect', async (ws, req) => {
let job = null;
let eventBus = new events.EventEmitter();
let event_bus = new events.EventEmitter();

eventBus.on('stdout', data =>
event_bus.on('stdout', data =>
ws.send(
JSON.stringify({
type: 'data',
Expand All @@ -185,7 +185,7 @@ router.ws('/connect', async (ws, req) => {
})
)
);
eventBus.on('stderr', data =>
event_bus.on('stderr', data =>
ws.send(
JSON.stringify({
type: 'data',
Expand All @@ -194,10 +194,10 @@ router.ws('/connect', async (ws, req) => {
})
)
);
eventBus.on('stage', stage =>
event_bus.on('stage', stage =>
ws.send(JSON.stringify({ type: 'stage', stage }))
);
eventBus.on('exit', (stage, status) =>
event_bus.on('exit', (stage, status) =>
ws.send(JSON.stringify({ type: 'exit', stage, ...status }))
);

Expand All @@ -220,7 +220,8 @@ router.ws('/connect', async (ws, req) => {
})
);

await job.execute_interactive(eventBus);
await job.execute(event_bus);
await job.cleanup();

ws.close(4999, 'Job Completed');
} else {
Expand All @@ -230,7 +231,7 @@ router.ws('/connect', async (ws, req) => {
case 'data':
if (job !== null) {
if (msg.stream === 'stdin') {
eventBus.emit('stdin', msg.data);
event_bus.emit('stdin', msg.data);
} else {
ws.close(4004, 'Can only write to stdin');
}
Expand All @@ -241,7 +242,7 @@ router.ws('/connect', async (ws, req) => {
case 'signal':
if (job !== null) {
if (SIGNALS.includes(msg.signal)) {
eventBus.emit('signal', msg.signal);
event_bus.emit('signal', msg.signal);
} else {
ws.close(4005, 'Invalid signal');
}
Expand All @@ -257,12 +258,6 @@ router.ws('/connect', async (ws, req) => {
}
});

ws.on('close', async () => {
if (job !== null) {
await job.cleanup();
}
});

setTimeout(() => {
//Terminate the socket after 1 second, if not initialized.
if (job === null) ws.close(4001, 'Initialization Timeout');
Expand All @@ -275,7 +270,11 @@ router.post('/execute', async (req, res) => {

await job.prime();

const result = await job.execute();
let result = await job.execute();
// Backward compatibility when the run stage is not started
if (result.run === undefined) {
result.run = result.compile;
}

await job.cleanup();

Expand Down
Loading

0 comments on commit 89e0dd4

Please sign in to comment.