-
Notifications
You must be signed in to change notification settings - Fork 0
/
datastore_export_job_check_api.go
169 lines (143 loc) · 6.22 KB
/
datastore_export_job_check_api.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
package main
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"github.com/gcpug/ds2bq/datastore"
"github.com/morikuni/failure"
)
type DatastoreExportJobCheckRequest struct {
DS2BQJobID string
DatastoreExportJobID string
}
type DatastoreExportJobCheckAPI struct {
DatastoreExportJobCheckQueue *DatastoreExportJobCheckQueue
DSExportJobStore *DSExportJobStore
BQLoadJobStore *BQLoadJobStore
BQLoadJobCheckQueue *BQLoadJobCheckQueue
}
func NewDatastoreExportJobCheckAPI(queue *DatastoreExportJobCheckQueue, dseJS *DSExportJobStore, bqlJS *BQLoadJobStore, bqjcQ *BQLoadJobCheckQueue) *DatastoreExportJobCheckAPI {
return &DatastoreExportJobCheckAPI{
queue, dseJS, bqlJS, bqjcQ,
}
}
func HandleDatastoreExportJobCheckAPI(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
b, err := ioutil.ReadAll(r.Body)
if err != nil {
msg := fmt.Sprintf("failed ioutil.Read(request.Body).err=%+v", err)
log.Println(msg)
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(http.StatusBadRequest)
_, err := w.Write([]byte(msg))
if err != nil {
log.Println(err)
}
return
}
form := &DatastoreExportJobCheckRequest{}
if err := json.Unmarshal(b, form); err != nil {
msg := fmt.Sprintf("failed json.Unmarshal(request.Body).err=%+v", err)
log.Println(msg)
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(http.StatusBadRequest)
_, err := w.Write([]byte(msg))
if err != nil {
log.Println(err)
}
return
}
log.Printf("%s\n", string(b))
queue, err := NewDatastoreExportJobCheckQueue(r.Host, TasksClient)
if err != nil {
WriteError(w, http.StatusBadRequest, "failed NewDatastoreExportJobCheckQueue", err)
return
}
dsexportJobStore, err := NewDSExportJobStore(ctx, DatastoreClient)
if err != nil {
WriteError(w, http.StatusInternalServerError, fmt.Sprintf("failed NewDSExportJobStore() form=%+v", form), err)
return
}
bqloadJobStore, err := NewBQLoadJobStore(ctx, DatastoreClient)
if err != nil {
WriteError(w, http.StatusInternalServerError, fmt.Sprintf("failed NewBQLoadJobStore() form=%+v", form), err)
return
}
bqljcQ, err := NewBQLoadJobCheckQueue(r.Host, TasksClient)
if err != nil {
WriteError(w, http.StatusInternalServerError, fmt.Sprintf("failed NewBQLoadJobCheckQueue() form=%+v", form), err)
return
}
api := NewDatastoreExportJobCheckAPI(queue, dsexportJobStore, bqloadJobStore, bqljcQ)
if err := api.Check(ctx, form); err != nil {
log.Println(err.Error())
w.WriteHeader(http.StatusInternalServerError)
}
w.WriteHeader(http.StatusOK)
}
func (api *DatastoreExportJobCheckAPI) Check(ctx context.Context, form *DatastoreExportJobCheckRequest) error {
res, err := datastore.CheckJobStatus(ctx, form.DatastoreExportJobID)
if err != nil {
return failure.New(StatusInternalServerError, failure.Messagef("failed Datastore.CheckJobStatus.err=%+v", err))
}
switch res.Status {
case datastore.Running:
log.Printf("%s is Running...\n", form.DatastoreExportJobID)
_, err := api.DSExportJobStore.IncrementJobStatusCheckCount(ctx, form.DS2BQJobID)
if err != nil {
return failure.New(StatusInternalServerError, failure.Messagef("failed DSExportJobStore.IncrementJobStatusCheckCount. DS2BQJobID=%v,err=%v\n", form.DS2BQJobID, err))
}
return failure.New(StatusConflict)
case datastore.Fail:
log.Printf("%s is Fail. ErrCode=%v,ErrMessage=%v\n", form.DatastoreExportJobID, res.ErrCode, res.ErrMessage)
_, err := api.DSExportJobStore.FinishExportJob(ctx, form.DS2BQJobID, DSExportJobStatusFailed, form.DatastoreExportJobID, fmt.Sprintf("Code=%v,MSG=%v,META=%+v", res.ErrCode, res.ErrMessage, res.Metadata))
if err != nil {
return failure.New(StatusInternalServerError, failure.Messagef("failed DSExportJobStore.FinishExportJob. DS2BQJobID=%v,err=%v\n", form.DS2BQJobID, err))
}
job, err := api.DSExportJobStore.Get(ctx, form.DS2BQJobID)
if err != nil {
return failure.New(StatusInternalServerError, failure.Messagef("failed DSExportJobStore.Get. DS2BQJobID=%v,err=%v\n", form.DS2BQJobID, err))
}
job.RetryCount++
if job.RetryCount > job.MaxRetryCount {
return nil
}
efs, err := BuildEntityFilter(ctx, job.ExportNamespaceIDs, job.ExportKinds, len(job.ExportKinds))
if err != nil {
return failure.New(StatusInternalServerError, failure.Messagef("failed BuildEntityFilter. DS2BQJobID=%v,err=%v\n", form.DS2BQJobID, err))
}
ds2bqJobID := api.DSExportJobStore.NewDS2BQJobID(ctx)
dseAPI := NewDatastoreExportAPI(api.DatastoreExportJobCheckQueue, api.DSExportJobStore, api.BQLoadJobStore)
var dseForm DatastoreExportRequest
if err := json.Unmarshal([]byte(job.JobRequestBody), &dseForm); err != nil {
return failure.New(StatusInternalServerError, failure.Messagef("failed json.Unmarshal.ds2bqJobID=%v,err=%v\n", form.DS2BQJobID, err))
}
_, err = dseAPI.CreateDatastoreExportJob(ctx, ds2bqJobID, job.ExportProjectID, dseForm.OutputGCSFilePath, efs[0], job.RetryCount)
if err != nil {
return failure.New(StatusInternalServerError, failure.Messagef("failed CreateDatastoreExportJob.ds2bqJobID=%v,err=%v\n", form.DS2BQJobID, err))
}
return nil
case datastore.Done:
log.Printf("%s is Done...\n", form.DatastoreExportJobID)
_, err := api.DSExportJobStore.FinishExportJob(ctx, form.DS2BQJobID, DSExportJobStatusDone, form.DatastoreExportJobID, "")
if err != nil {
return failure.New(StatusInternalServerError, failure.Messagef("failed DSExportJobStore.FinishExportJob. DS2BQJobID=%v,err=%v\n", form.DS2BQJobID, err))
}
if err := api.InsertBQLoadJobs(ctx, form.DS2BQJobID, res.Metadata.OutputURLPrefix); err != nil {
return failure.New(StatusInternalServerError, failure.Messagef("failed InsertBQLoadJobs. DS2BQJobID=%v,err=%v\n", form.DS2BQJobID, err))
}
return nil
default:
return failure.New(StatusInternalServerError, failure.Messagef("%v is Unspported Status", res.Status))
}
}
func (api *DatastoreExportJobCheckAPI) InsertBQLoadJobs(ctx context.Context, ds2bqJobID string, outputURLPrefix string) error {
ls := NewBQLoadService(api.BQLoadJobStore, api.BQLoadJobCheckQueue)
if err := ls.InsertBigQueryLoadJob(ctx, ds2bqJobID, outputURLPrefix); err != nil {
return failure.Wrap(err, failure.Message("failed BQLoadService.InsertBigQueryLoadJob"))
}
return nil
}