Skip to content

Commit

Permalink
feat: transmit errors in job status
Browse files Browse the repository at this point in the history
  • Loading branch information
angristan committed Jul 7, 2021
1 parent f6dce57 commit 4dcbb44
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
12 changes: 6 additions & 6 deletions job.go
Expand Up @@ -15,7 +15,7 @@ func (job benchJob) run(ctx context.Context, WarmVMs <-chan runningFirecracker)
err := q.setjobReceived(ctx, job)
if err != nil {
log.WithError(err).Error("Could not set job received")
q.setjobFailed(ctx, job)
q.setjobFailed(ctx, job, agentExecRes{Error: err.Error()})
return
}

Expand All @@ -39,14 +39,14 @@ func (job benchJob) run(ctx context.Context, WarmVMs <-chan runningFirecracker)
})
if err != nil {
log.WithError(err).Error("Failed to marshal JSON request")
q.setjobFailed(ctx, job)
q.setjobFailed(ctx, job, agentExecRes{Error: err.Error()})
return
}

err = q.setjobRunning(ctx, job)
if err != nil {
log.WithError(err).Error("Could not set job running")
q.setjobFailed(ctx, job)
q.setjobFailed(ctx, job, agentExecRes{Error: err.Error()})
return
}

Expand All @@ -57,7 +57,7 @@ func (job benchJob) run(ctx context.Context, WarmVMs <-chan runningFirecracker)
httpRes, err = http.Post("http://"+vm.ip.String()+":8080/run", "application/json", bytes.NewBuffer(reqJSON))
if err != nil {
log.WithError(err).Error("Failed to request execution to agent")
q.setjobFailed(ctx, job)
q.setjobFailed(ctx, job, agentExecRes{Error: err.Error()})
return
}
json.NewDecoder(httpRes.Body).Decode(&agentRes)
Expand All @@ -68,13 +68,13 @@ func (job benchJob) run(ctx context.Context, WarmVMs <-chan runningFirecracker)
"agentRes": agentRes,
"reqJSON": string(reqJSON),
}).Error("Failed to compile and run code")
q.setjobFailed(ctx, job)
q.setjobFailed(ctx, job, agentRes)
return
}

err = q.setjobResult(ctx, job, agentRes)
if err != nil {
q.setjobFailed(ctx, job)
q.setjobFailed(ctx, job, agentExecRes{Error: err.Error()})
}

}
24 changes: 15 additions & 9 deletions job_queue_rabbitmq.go
Expand Up @@ -18,6 +18,8 @@ type jobQueue struct {
type jobStatus struct {
ID string `json:"id"`
Status string `json:"status"`
Message string `json:"message"`
Error string `json:"error"`
StdErr string `json:"stderr"`
StdOut string `json:"stdout"`
ExecDuration int `json:"exec_duration"`
Expand Down Expand Up @@ -101,13 +103,15 @@ func (q jobQueue) getQueueForJob(ctx context.Context) error {
)
}

func (q jobQueue) setjobStatus(ctx context.Context, job benchJob, status string) error {
func (q jobQueue) setjobStatus(ctx context.Context, job benchJob, status string, res agentExecRes) error {
log.WithField("status", status).Info("Set job status")
jobStatus := &jobStatus{
ID: job.ID,
Status: status,
StdErr: "",
StdOut: "",
ID: job.ID,
Status: status,
Message: res.Message,
Error: res.Error,
StdErr: "",
StdOut: "",
}
b, err := json.Marshal(jobStatus)
if err != nil {
Expand All @@ -126,20 +130,22 @@ func (q jobQueue) setjobStatus(ctx context.Context, job benchJob, status string)
}

func (q jobQueue) setjobReceived(ctx context.Context, job benchJob) error {
return q.setjobStatus(ctx, job, "received")
return q.setjobStatus(ctx, job, "received", agentExecRes{})
}

func (q jobQueue) setjobRunning(ctx context.Context, job benchJob) error {
return q.setjobStatus(ctx, job, "running")
return q.setjobStatus(ctx, job, "running", agentExecRes{})
}

func (q jobQueue) setjobFailed(ctx context.Context, job benchJob) error {
return q.setjobStatus(ctx, job, "failed")
func (q jobQueue) setjobFailed(ctx context.Context, job benchJob, res agentExecRes) error {
return q.setjobStatus(ctx, job, "failed", res)
}
func (q jobQueue) setjobResult(ctx context.Context, job benchJob, res agentExecRes) error {
jobStatus := &jobStatus{
ID: job.ID,
Status: "done",
Message: res.Message,
Error: res.Error,
StdErr: res.StdErr,
StdOut: res.StdOut,
ExecDuration: res.ExecDuration,
Expand Down
2 changes: 2 additions & 0 deletions main.go
Expand Up @@ -36,6 +36,8 @@ type agentRunReq struct {
}

type agentExecRes struct {
Message string `json:"message"`
Error string `json:"error"`
StdErr string `json:"stderr"`
StdOut string `json:"stdout"`
ExecDuration int `json:"exec_duration"`
Expand Down

0 comments on commit 4dcbb44

Please sign in to comment.