@@ -3,13 +3,14 @@ package cmd
3
3
import (
4
4
"context"
5
5
"io"
6
+ "strconv"
7
+
6
8
//"log"
7
9
"path/filepath"
8
10
9
11
dockertypes "github.com/docker/docker/api/types"
10
12
"github.com/docker/docker/api/types/container"
11
13
"github.com/docker/docker/client"
12
- "github.com/docker/docker/pkg/stdcopy"
13
14
"github.com/markhuang1212/code-grader/backend/types"
14
15
"github.com/pkg/errors"
15
16
)
@@ -37,33 +38,24 @@ func CompileUserCode(ctx context.Context, gr types.GradeRequest) ([]byte, error)
37
38
"CXXFLAGS=-std=c++11" ,
38
39
},
39
40
OpenStdin : true ,
40
- StdinOnce : true ,
41
41
}, & container.HostConfig {
42
42
NetworkMode : "none" ,
43
- Resources : container.Resources {
44
- Memory : CompilationMemoryLimit ,
43
+ Resources : container.Resources {
44
+ // Memory: CompilationMemoryLimit,
45
45
},
46
46
}, nil , nil , "" )
47
47
48
48
if err != nil {
49
49
return nil , errors .Wrap (ErrInternalError , "cannot create container" )
50
50
}
51
51
52
- attachInput , err := cli .ContainerAttach (ctx , resp .ID , dockertypes.ContainerAttachOptions {
53
- Stdin : true ,
54
- })
55
-
56
- if err != nil {
57
- return nil , errors .Wrap (ErrInternalError , "cannot attach container input" )
58
- }
59
-
60
- attachOutput , err := cli .ContainerAttach (ctx , resp .ID , dockertypes.ContainerAttachOptions {
61
- Stdout : true ,
62
- Stderr : true ,
52
+ hjresp , err := cli .ContainerAttach (ctx , resp .ID , dockertypes.ContainerAttachOptions {
53
+ Stdin : true ,
54
+ Stream : true ,
63
55
})
64
56
65
57
if err != nil {
66
- return nil , errors .Wrap (ErrInternalError , "cannot attach container output " )
58
+ return nil , errors .Wrap (ErrInternalError , "cannot attach container" )
67
59
}
68
60
69
61
statusCh , errCh := cli .ContainerWait (ctx , resp .ID , container .WaitConditionNextExit )
@@ -83,30 +75,37 @@ func CompileUserCode(ctx context.Context, gr types.GradeRequest) ([]byte, error)
83
75
// }
84
76
}()
85
77
86
- attachInput .Conn .Write ([]byte (gr .UserCode ))
87
- attachInput .Close ()
78
+ userCodeLength := len (gr .UserCode )
79
+ hjresp .Conn .Write ([]byte (strconv .Itoa (userCodeLength ) + "\n " ))
80
+ hjresp .Conn .Write ([]byte (gr .UserCode ))
81
+ hjresp .Close ()
88
82
89
- outR , outW := io .Pipe ()
90
- errR , errW := io .Pipe ()
91
- stdcopy .StdCopy (outW , errW , attachOutput .Conn )
92
- outW .Close ()
93
- errW .Close ()
94
- attachOutput .Conn .Close ()
83
+ if err != nil {
84
+ return nil , errors .Wrap (ErrInternalError , "cannot close attached session (output)" )
85
+ }
95
86
96
87
select {
97
88
case status := <- statusCh :
98
89
switch status .StatusCode {
99
90
case 0 :
100
- result , err := io .ReadAll (outR )
91
+ stdout , err := cli .ContainerLogs (ctx , resp .ID , dockertypes.ContainerLogsOptions {
92
+ ShowStdout : true ,
93
+ ShowStderr : false ,
94
+ })
101
95
if err != nil {
102
- return nil , errors .Wrap (ErrInternalError , "error reading outR " )
96
+ return nil , errors .Wrap (ErrInternalError , "error reading stdout " )
103
97
}
98
+ result , _ := io .ReadAll (stdout )
104
99
return result , nil
105
100
case 1 :
106
- result , err := io .ReadAll (errR )
101
+ stderr , err := cli .ContainerLogs (ctx , resp .ID , dockertypes.ContainerLogsOptions {
102
+ ShowStderr : true ,
103
+ ShowStdout : false ,
104
+ })
107
105
if err != nil {
108
- return nil , errors .Wrap (ErrInternalError , "error reading errR " )
106
+ return nil , errors .Wrap (ErrInternalError , "error reading stdout " )
109
107
}
108
+ result , _ := io .ReadAll (stderr )
110
109
return result , ErrCompilationError
111
110
case 2 :
112
111
return nil , ErrInternalError
0 commit comments