-
Notifications
You must be signed in to change notification settings - Fork 0
/
dsexport_job_store.go
198 lines (175 loc) · 5.72 KB
/
dsexport_job_store.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
//go:generate qbg -usedatastorewrapper -output model_query.go .
package main
import (
"context"
"fmt"
"time"
"github.com/google/uuid"
"github.com/morikuni/failure"
"go.mercari.io/datastore"
)
type DSExportJobStore struct {
ds datastore.Client
}
func NewDSExportJobStore(ctx context.Context, client datastore.Client) (*DSExportJobStore, error) {
return &DSExportJobStore{
ds: client,
}, nil
}
type DSExportJobStatus int
const (
DSExportJobStatusDefault DSExportJobStatus = iota
DSExportJobStatusRunning
DSExportJobStatusFailed
DSExportJobStatusDone
)
type DSExportJob struct {
ID string `datastore:"-"`
DSExportJobIDs []string
JobRequestBody string `datastore:",noindex"`
ExportProjectID string
ExportNamespaceIDs []string `datastore:",noindex"`
ExportKinds []string `datastore:",noindex"`
StatusCheckCount int
Status DSExportJobStatus
MaxRetryCount int
RetryCount int
ChangeStatusAt time.Time
DSExportResponseMessages []string `datastore:",noindex"` // DatastoreExportJobID-_-ResponseMessagesが格納される
CreatedAt time.Time
UpdatedAt time.Time
SchemaVersion int
}
var _ datastore.PropertyLoadSaver = &DSExportJob{}
var _ datastore.KeyLoader = &DSExportJob{}
// LoadKey is Entity Load時にKeyを設定する
func (e *DSExportJob) LoadKey(ctx context.Context, k datastore.Key) error {
e.ID = k.Name()
return nil
}
// Load is Entity Load時に呼ばれる
func (e *DSExportJob) Load(ctx context.Context, ps []datastore.Property) error {
err := datastore.LoadStruct(ctx, e, ps)
if err != nil {
return err
}
return nil
}
// Save is Entity Save時に呼ばれる
func (e *DSExportJob) Save(ctx context.Context) ([]datastore.Property, error) {
if e.CreatedAt.IsZero() {
e.CreatedAt = time.Now()
}
e.UpdatedAt = time.Now()
e.SchemaVersion = 2
return datastore.SaveStruct(ctx, e)
}
// NewJobID is JobIDを生成する
// JobIDは一度のDatastore Export, BQ Loadで一つ発行され、複数KindのExportが全て終わっているかを確認するためのID
func (store *DSExportJobStore) NewDS2BQJobID(ctx context.Context) string {
return uuid.New().String()
}
func (store *DSExportJobStore) NewKey(ctx context.Context, ds2bqJobID string) datastore.Key {
return store.ds.NameKey("DSExportJob", ds2bqJobID, nil)
}
func (store *DSExportJobStore) Create(ctx context.Context, ds2bqJobID string, body string, exportProjectID string, namespaceIDs []string, kinds []string, maxRetryCount int) (*DSExportJob, error) {
e := DSExportJob{
ID: ds2bqJobID,
DSExportJobIDs: []string{},
Status: DSExportJobStatusDefault,
JobRequestBody: body,
ExportProjectID: exportProjectID,
ExportNamespaceIDs: namespaceIDs,
ExportKinds: kinds,
ChangeStatusAt: time.Now(),
DSExportResponseMessages: []string{},
MaxRetryCount: maxRetryCount,
}
_, err := store.ds.Put(ctx, store.NewKey(ctx, ds2bqJobID), &e)
if err != nil {
return nil, failure.Wrap(err)
}
return &e, nil
}
func (store *DSExportJobStore) Get(ctx context.Context, ds2bqJobID string) (*DSExportJob, error) {
var e DSExportJob
err := store.ds.Get(ctx, store.NewKey(ctx, ds2bqJobID), &e)
if err != nil {
if err == datastore.ErrNoSuchEntity {
return nil, err
}
return nil, failure.Wrap(err, failure.Messagef("failed datastore.Get() ds2bqJobID=%v", ds2bqJobID))
}
return &e, nil
}
func (store *DSExportJobStore) StartExportJob(ctx context.Context, ds2bqJobID string, dsExportJobID string, retryCount int) (*DSExportJob, error) {
key := store.NewKey(ctx, ds2bqJobID)
var e DSExportJob
_, err := store.ds.RunInTransaction(ctx, func(tx datastore.Transaction) error {
if err := tx.Get(key, &e); err != nil {
return err
}
e.DSExportJobIDs = append(e.DSExportJobIDs, dsExportJobID)
e.Status = DSExportJobStatusRunning
e.ChangeStatusAt = time.Now()
e.RetryCount = retryCount
_, err := tx.Put(key, &e)
if err != nil {
return err
}
return nil
})
if err != nil {
if err == datastore.ErrNoSuchEntity {
return nil, err
}
return nil, failure.Wrap(err, failure.Messagef("failed datastore.RunInTx() ds2bqJobID=%v", ds2bqJobID))
}
return &e, nil
}
func (store *DSExportJobStore) IncrementJobStatusCheckCount(ctx context.Context, ds2bqJobID string) (*DSExportJob, error) {
key := store.NewKey(ctx, ds2bqJobID)
var e DSExportJob
_, err := store.ds.RunInTransaction(ctx, func(tx datastore.Transaction) error {
if err := tx.Get(key, &e); err != nil {
return err
}
e.StatusCheckCount++
_, err := tx.Put(key, &e)
if err != nil {
return err
}
return nil
})
if err != nil {
if err == datastore.ErrNoSuchEntity {
return nil, err
}
return nil, failure.Wrap(err, failure.Messagef("failed datastore.RunInTx() ds2bqJobID=%v", ds2bqJobID))
}
return &e, nil
}
func (store *DSExportJobStore) FinishExportJob(ctx context.Context, ds2bqJobID string, status DSExportJobStatus, dsExportJobID string, message string) (*DSExportJob, error) {
key := store.NewKey(ctx, ds2bqJobID)
var e DSExportJob
_, err := store.ds.RunInTransaction(ctx, func(tx datastore.Transaction) error {
if err := tx.Get(key, &e); err != nil {
return err
}
e.Status = status
e.ChangeStatusAt = time.Now()
e.DSExportResponseMessages = append(e.DSExportResponseMessages, fmt.Sprintf("%s-_-%s", dsExportJobID, message))
_, err := tx.Put(key, &e)
if err != nil {
return err
}
return nil
})
if err != nil {
if err == datastore.ErrNoSuchEntity {
return nil, err
}
return nil, failure.Wrap(err, failure.Messagef("failed datastore.RunInTx() ds2bqJobID=%v", ds2bqJobID))
}
return &e, nil
}