-
Notifications
You must be signed in to change notification settings - Fork 127
/
getjobspec.go
69 lines (61 loc) · 1.64 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package repository
import (
"context"
"github.com/gogo/protobuf/proto"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/armadaproject/armada/internal/common/compress"
"github.com/armadaproject/armada/pkg/api"
)
type GetJobSpecRepository interface {
GetJobSpec(ctx context.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 context.Context, jobId string) (*api.Job, error) {
var rawBytes []byte
err := r.db.BeginTxFunc(ctx, pgx.TxOptions{
IsoLevel: pgx.RepeatableRead,
AccessMode: pgx.ReadOnly,
DeferrableMode: pgx.Deferrable,
}, func(tx pgx.Tx) error {
rows, err := tx.Query(ctx, "SELECT job_spec FROM job WHERE job_id = $1", jobId)
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
err := rows.Scan(&rawBytes)
if err != nil {
return err
}
return nil
}
return errors.Errorf("job with id %s not found", jobId)
})
if err != nil {
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
}