1
1
mirror of https://github.com/theoludwig/programming-challenges.git synced 2025-05-18 12:02:53 +02:00

fix(cli): add timeout for running solutions

This commit is contained in:
Divlo
2021-12-07 11:35:47 +01:00
parent 938702c23f
commit 0b59c19885
10 changed files with 638 additions and 284 deletions

View File

@ -1,9 +1,12 @@
import execa from 'execa'
import ora from 'ora'
import ms from 'ms'
class Docker {
export class Docker {
static CONTAINER_TAG = 'programming-challenges'
static SIGSEGV_EXIT_CODE = 139
static MAXIMUM_TIMEOUT = '1 minute'
static MAXIMUM_TIMEOUT_MILLISECONDS = ms(Docker.MAXIMUM_TIMEOUT)
public async build (): Promise<void> {
const loader = ora('Building the Docker image').start()
@ -23,13 +26,22 @@ class Docker {
input
}
)
let isValid = true
const timeout = setTimeout(() => {
subprocess.kill()
isValid = false
}, Docker.MAXIMUM_TIMEOUT_MILLISECONDS)
try {
const { stdout, stderr } = await subprocess
if (stderr.length !== 0) {
throw new Error(stderr)
}
clearTimeout(timeout)
return stdout
} catch (error: any) {
if (!isValid) {
throw new Error(`Timeout: time limit exceeded (${Docker.MAXIMUM_TIMEOUT}), try to optimize your solution.`)
}
if (error.exitCode === Docker.SIGSEGV_EXIT_CODE) {
throw new Error('Docker run failed: SIGSEGV indicates a segmentation fault (attempts to access a memory location that it\'s not allowed to access).')
}