11import { execute } from '/common/util' ;
22import Commander from '/languages/Commander' ;
3- import { maxTracers , maxTraces } from '/common/config' ;
3+ import { maxTracers , maxTraces , memoryLimit , timeLimit } from '/common/config' ;
4+ import uuid from 'uuid' ;
45
56class Executer extends Commander {
67 constructor ( { name, compileCommand, runCommand } ) {
@@ -12,17 +13,30 @@ class Executer extends Commander {
1213 }
1314
1415 execute ( tempPath , command ) {
15- // TODO: memory limit + time limit + space limit?
16+ const containerName = uuid . v4 ( ) ;
17+ let timer = setTimeout ( ( ) => {
18+ timer = null ;
19+ execute ( `docker kill ${ containerName } ` , this . cwd ) ;
20+ } , timeLimit ) ;
1621 return execute ( [
1722 `docker run --rm` ,
23+ `--name=${ containerName } ` ,
1824 '-w=/usr/judge' ,
1925 `-v=$PWD/tracers:/usr/bin/tracers:ro` ,
2026 `-v=${ tempPath } :/usr/judge:rw` ,
27+ `-m=${ memoryLimit } m --memory-swap=${ memoryLimit } m` , // TODO: needs to be tested on linux
2128 `-e MAX_TRACES=${ maxTraces } -e MAX_TRACERS=${ maxTracers } ` ,
2229 this . executerImageTag ,
2330 '/bin/bash -c' ,
2431 `"${ command } "` ,
25- ] . join ( ' ' ) , this . cwd ) ;
32+ ] . join ( ' ' ) , this . cwd ) . catch ( error => {
33+ if ( timer ) {
34+ clearTimeout ( timer ) ;
35+ } else {
36+ console . error ( 'Time Limit Exceeded' ) ;
37+ }
38+ throw error ;
39+ } ) ;
2640 }
2741
2842 compile ( tempPath ) {
0 commit comments