forked from UBAutograding/leviathan
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjob_impl.go
More file actions
102 lines (87 loc) · 2.75 KB
/
job_impl.go
File metadata and controls
102 lines (87 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package v1
import (
"connectrpc.com/connect"
"context"
"fmt"
v1 "github.com/makeopensource/leviathan/generated/jobs/v1"
"github.com/makeopensource/leviathan/models"
"github.com/makeopensource/leviathan/service/jobs"
"strings"
"time"
)
type JobServer struct {
srv *jobs.JobService
}
func NewJobServer(srv *jobs.JobService) *JobServer {
return &JobServer{srv: srv}
}
func (job *JobServer) NewJob(ctx context.Context, req *connect.Request[v1.NewJobRequest]) (*connect.Response[v1.NewJobResponse], error) {
jobFiles := req.Msg.GetJobFiles()
tag := req.Msg.GetImageName()
dockerfile := req.Msg.GetDockerFile()
entryCmd := req.Msg.GetEntryCmd()
for i, v := range jobFiles {
if v == nil {
return nil, fmt.Errorf("nil file at index %d", i)
} else if v.Filename == "" {
return nil, fmt.Errorf("filename is empty")
} else if len(v.Content) == 0 {
return nil, fmt.Errorf("empty content for file %s", v.Filename)
}
}
if tag == "" {
return nil, fmt.Errorf("docker image tag is empty")
}
if entryCmd == "" {
return nil, fmt.Errorf("entry cmd is empty")
}
lab := models.Lab{
ImageTag: strings.ToLower(strings.TrimSpace(tag)),
JobEntryCmd: entryCmd,
JobTimeout: time.Second * time.Duration(req.Msg.JobTimeoutInSeconds),
JobLimits: models.MachineLimits{
PidsLimit: int64(req.Msg.GetLimits().PidLimit),
NanoCPU: int64(req.Msg.GetLimits().CPUCores),
Memory: int64(req.Msg.GetLimits().MemoryInMb),
},
}
newJob := &models.Job{LabData: &lab}
jobId, err := job.srv.NewJobFromRPC(newJob, jobFiles, dockerfile)
if err != nil {
return nil, err
}
res := connect.NewResponse(&v1.NewJobResponse{JobId: jobId})
return res, nil
}
func (job *JobServer) GetStatus(_ context.Context, req *connect.Request[v1.JobLogRequest]) (*connect.Response[v1.JobLogsResponse], error) {
status, logs, err := job.srv.GetJobStatusAndLogs(req.Msg.GetJobId())
if err != nil {
return nil, err
}
res := connect.NewResponse(&v1.JobLogsResponse{
JobInfo: status.ToProto(),
Logs: logs,
})
return res, nil
}
func (job *JobServer) StreamStatus(ctx context.Context, req *connect.Request[v1.JobLogRequest], stream *connect.ServerStream[v1.JobLogsResponse]) error {
streamFunc := func(jobInfo *models.Job, logs string) error {
return stream.Send(&v1.JobLogsResponse{
JobInfo: jobInfo.ToProto(),
Logs: logs,
})
}
err := job.srv.StreamJobAndLogs(ctx, req.Msg.GetJobId(), streamFunc)
if err != nil {
return err
}
return nil
}
func (job *JobServer) CancelJob(ctx context.Context, req *connect.Request[v1.CancelJobRequest]) (*connect.Response[v1.CancelJobResponse], error) {
msgId := req.Msg.GetJobId()
if msgId == "" {
return nil, fmt.Errorf("job Id is empty")
}
job.srv.CancelJob(msgId)
return connect.NewResponse(&v1.CancelJobResponse{}), nil
}