-
Notifications
You must be signed in to change notification settings - Fork 8
/
collection.go
208 lines (167 loc) · 6.02 KB
/
collection.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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package collection
import (
"context"
"database/sql"
"fmt"
"net/http"
"time"
goacollection "github.com/artefactual-labs/enduro/internal/api/gen/collection"
"github.com/artefactual-labs/enduro/internal/pipeline"
"github.com/go-logr/logr"
"github.com/jmoiron/sqlx"
cadenceclient "go.uber.org/cadence/client"
goahttp "goa.design/goa/v3/http"
)
//go:generate mockgen -destination=./fake/mock_collection.go -package=fake github.com/artefactual-labs/enduro/internal/collection Service
type Service interface {
// Goa returns an implementation of the goacollection Service.
Goa() goacollection.Service
Create(context.Context, *Collection) error
UpdateWorkflowStatus(ctx context.Context, ID uint, name string, workflowID, runID, transferID, aipID, pipelineID string, status Status, storedAt time.Time) error
SetStatus(ctx context.Context, ID uint, status Status) error
SetStatusInProgress(ctx context.Context, ID uint, startedAt time.Time) error
SetStatusPending(ctx context.Context, ID uint, taskToken []byte) error
SetOriginalID(ctx context.Context, ID uint, originalID string) error
// HTTPDownload returns a HTTP handler that serves the package over HTTP.
//
// TODO: this service is meant to be agnostic to protocols. But I haven't
// found a way in goagen to have my service write directly to the HTTP
// response writer. Ideally, our goacollection.Service would have a new
// method that takes a io.Writer (e.g. http.ResponseWriter).
HTTPDownload(mux goahttp.Muxer, dec func(r *http.Request) goahttp.Decoder) http.HandlerFunc
}
type collectionImpl struct {
logger logr.Logger
db *sqlx.DB
cc cadenceclient.Client
registry *pipeline.Registry
// downloadProxy generates a reverse proxy on each download.
downloadProxy *downloadReverseProxy
}
var _ Service = (*collectionImpl)(nil)
func NewService(logger logr.Logger, db *sql.DB, cc cadenceclient.Client, registry *pipeline.Registry) *collectionImpl {
return &collectionImpl{
logger: logger,
db: sqlx.NewDb(db, "mysql"),
cc: cc,
registry: registry,
downloadProxy: newDownloadReverseProxy(logger),
}
}
func (svc *collectionImpl) Goa() goacollection.Service {
return &goaWrapper{
collectionImpl: svc,
}
}
func (svc *collectionImpl) Create(ctx context.Context, col *Collection) error {
var query = `INSERT INTO collection (name, workflow_id, run_id, transfer_id, aip_id, original_id, pipeline_id, decision_token, status) VALUES ((?), (?), (?), (?), (?), (?), (?), (?), (?))`
var args = []interface{}{
col.Name,
col.WorkflowID,
col.RunID,
col.TransferID,
col.AIPID,
col.OriginalID,
col.PipelineID,
col.DecisionToken,
col.Status,
}
query = svc.db.Rebind(query)
res, err := svc.db.ExecContext(ctx, query, args...)
if err != nil {
return fmt.Errorf("error inserting collection: %w", err)
}
var id int64
if id, err = res.LastInsertId(); err != nil {
return fmt.Errorf("error retrieving insert ID: %w", err)
}
col.ID = uint(id)
return err
}
func (svc *collectionImpl) UpdateWorkflowStatus(ctx context.Context, ID uint, name string, workflowID, runID, transferID, aipID, pipelineID string, status Status, storedAt time.Time) error {
// Ensure that storedAt is reset during retries.
var completedAt = &storedAt
if status == StatusInProgress {
completedAt = nil
}
if completedAt != nil && completedAt.IsZero() {
completedAt = nil
}
var query = `UPDATE collection SET name = (?), workflow_id = (?), run_id = (?), transfer_id = (?), aip_id = (?), pipeline_id = (?), status = (?), completed_at = (?) WHERE id = (?)`
var args = []interface{}{
name,
workflowID,
runID,
transferID,
aipID,
pipelineID,
status,
completedAt,
ID,
}
_, err := svc.updateRow(ctx, query, args)
return err
}
func (svc *collectionImpl) SetStatus(ctx context.Context, ID uint, status Status) error {
var query = `UPDATE collection SET status = (?) WHERE id = (?)`
var args = []interface{}{
status,
ID,
}
_, err := svc.updateRow(ctx, query, args)
return err
}
func (svc *collectionImpl) SetStatusInProgress(ctx context.Context, ID uint, startedAt time.Time) error {
var query string
var args = []interface{}{StatusInProgress}
if !startedAt.IsZero() {
query = `UPDATE collection SET status = (?), started_at = (?) WHERE id = (?)`
args = append(args, startedAt, ID)
} else {
query = `UPDATE collection SET status = (?) WHERE id = (?)`
args = append(args, ID)
}
_, err := svc.updateRow(ctx, query, args)
return err
}
func (svc *collectionImpl) SetStatusPending(ctx context.Context, ID uint, taskToken []byte) error {
var query = `UPDATE collection SET status = (?), decision_token = (?) WHERE id = (?)`
var args = []interface{}{
StatusPending,
taskToken,
ID,
}
_, err := svc.updateRow(ctx, query, args)
return err
}
func (svc *collectionImpl) SetOriginalID(ctx context.Context, ID uint, originalID string) error {
var query = `UPDATE collection SET original_id = (?) WHERE id = (?)`
var args = []interface{}{
originalID,
ID,
}
_, err := svc.updateRow(ctx, query, args)
return err
}
func (svc *collectionImpl) updateRow(ctx context.Context, query string, args []interface{}) (int64, error) {
query = svc.db.Rebind(query)
res, err := svc.db.ExecContext(ctx, query, args...)
if err != nil {
return 0, fmt.Errorf("error updating collection: %v", err)
}
n, err := res.RowsAffected()
if err != nil {
return 0, fmt.Errorf("error retrieving rows affected: %v", err)
}
return n, nil
}
func (svc *collectionImpl) read(ctx context.Context, ID uint) (*Collection, error) {
var query = "SELECT id, name, workflow_id, run_id, transfer_id, aip_id, original_id, pipeline_id, decision_token, status, CONVERT_TZ(created_at, @@session.time_zone, '+00:00') AS created_at, CONVERT_TZ(started_at, @@session.time_zone, '+00:00') AS started_at, CONVERT_TZ(completed_at, @@session.time_zone, '+00:00') AS completed_at FROM collection WHERE id = (?)"
var args = []interface{}{ID}
var c = Collection{}
query = svc.db.Rebind(query)
if err := svc.db.GetContext(ctx, &c, query, args...); err != nil {
return nil, err
}
return &c, nil
}