-
Notifications
You must be signed in to change notification settings - Fork 127
/
getjobspec.go
52 lines (46 loc) · 1.42 KB
/
getjobspec.go
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
package repository
import (
"github.com/gogo/protobuf/proto"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/armadaproject/armada/internal/common/armadacontext"
"github.com/armadaproject/armada/internal/common/compress"
"github.com/armadaproject/armada/pkg/api"
)
type GetJobSpecRepository interface {
GetJobSpec(ctx *armadacontext.Context, jobId string) (*api.Job, error)
}
type SqlGetJobSpecRepository struct {
db *pgxpool.Pool
decompressor compress.Decompressor
}
func NewSqlGetJobSpecRepository(db *pgxpool.Pool, decompressor compress.Decompressor) *SqlGetJobSpecRepository {
return &SqlGetJobSpecRepository{
db: db,
decompressor: decompressor,
}
}
func (r *SqlGetJobSpecRepository) GetJobSpec(ctx *armadacontext.Context, jobId string) (*api.Job, error) {
var rawBytes []byte
err := r.db.QueryRow(ctx, "SELECT job_spec FROM job WHERE job_id = $1", jobId).Scan(&rawBytes)
if err != nil {
if err == pgx.ErrNoRows {
return nil, errors.Errorf("job with id %s not found", jobId)
}
return nil, err
}
decompressed, err := r.decompressor.Decompress(rawBytes)
if err != nil {
log.WithError(err).Error("failed to decompress")
return nil, err
}
var job api.Job
err = proto.Unmarshal(decompressed, &job)
if err != nil {
log.WithError(err).Error("failed to unmarshal bytes")
return nil, err
}
return &job, nil
}