Skip to content

Commit d353a13

Browse files
committed
implemented getStatus rpc
1 parent b758ab5 commit d353a13

3 files changed

Lines changed: 33 additions & 6 deletions

File tree

src/api/v1/job_impl.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,24 @@ func (job *JobServer) NewJob(ctx context.Context, req *connect.Request[v1.NewJob
6363
return res, nil
6464
}
6565

66+
func (job *JobServer) GetStatus(_ context.Context, req *connect.Request[v1.JobLogRequest]) (*connect.Response[v1.JobLogsResponse], error) {
67+
status, logs, err := job.srv.GetJobStatusAndLogs(req.Msg.GetJobId())
68+
if err != nil {
69+
return nil, err
70+
}
71+
72+
res := connect.NewResponse(&v1.JobLogsResponse{
73+
JobInfo: status.ToProto(),
74+
Logs: logs,
75+
})
76+
return res, nil
77+
}
78+
6679
func (job *JobServer) StreamStatus(ctx context.Context, req *connect.Request[v1.JobLogRequest], stream *connect.ServerStream[v1.JobLogsResponse]) error {
6780
streamFunc := func(jobInfo *models.Job, logs string) error {
6881
return stream.Send(&v1.JobLogsResponse{
69-
JobInfo: &v1.JobStatus{
70-
JobId: jobInfo.JobId,
71-
Status: string(jobInfo.Status),
72-
StatusMessage: jobInfo.StatusMessage,
73-
},
74-
Logs: logs,
82+
JobInfo: jobInfo.ToProto(),
83+
Logs: logs,
7584
})
7685
}
7786

src/models/job.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package models
33
import (
44
"context"
55
"fmt"
6+
v1 "github.com/makeopensource/leviathan/generated/jobs/v1"
67
"github.com/rs/zerolog/log"
78
"gorm.io/gorm"
89
)
@@ -63,6 +64,14 @@ type Job struct {
6364
JobCtx context.Context `gorm:"-"`
6465
}
6566

67+
func (j *Job) ToProto() *v1.JobStatus {
68+
return &v1.JobStatus{
69+
JobId: j.JobId,
70+
Status: string(j.Status),
71+
StatusMessage: j.StatusMessage,
72+
}
73+
}
74+
6675
// ValidateForQueue checks for fields required before sending job to queue
6776
func (j *Job) ValidateForQueue() error {
6877
if j == nil {

src/service/jobs/job_service.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,15 @@ func (job *JobService) StreamJobAndLogs(
249249
}
250250
}
251251

252+
// GetJobStatusAndLogs gets the status once whatever it may be and current logs
253+
func (job *JobService) GetJobStatusAndLogs(jobUuid string) (*models.Job, string, error) {
254+
jobInfo, _, logs, err := job.checkJob(jobUuid)
255+
if err != nil {
256+
return nil, "", err
257+
}
258+
return jobInfo, logs, nil
259+
}
260+
252261
func (job *JobService) ListenToJobLogs(ctx context.Context, jobInfo *models.Job) chan string {
253262
logChannel := make(chan string, 2)
254263
go func(ctx context.Context) {

0 commit comments

Comments
 (0)