-
Notifications
You must be signed in to change notification settings - Fork 0
/
bqload_job_check_api.go
105 lines (96 loc) · 3.12 KB
/
bqload_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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"github.com/gcpug/ds2bq/bigquery"
)
type BQLoadJobCheckRequest struct {
DS2BQJobID string
BQLoadProjectID string
BQLoadKind string
BigQueryLoadJobID string
}
func HandleBQLoadJobCheckAPI(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
}
var form BQLoadJobCheckRequest
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))
bqloadJobStore, err := NewBQLoadJobStore(ctx, DatastoreClient)
if err != nil {
msg := fmt.Sprintf("failed NewBQLoadJobStore.err=%+v", err)
log.Println(msg)
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(http.StatusInternalServerError)
_, err := w.Write([]byte(msg))
if err != nil {
log.Println(err)
}
return
}
res, err := bigquery.CheckJobStatus(ctx, form.BQLoadProjectID, form.BigQueryLoadJobID)
if err != nil {
msg := fmt.Sprintf("failed bigquery.CheckJobStatus.ProjectID=%v,JobID=%v,err=%+v", form.BQLoadProjectID, form.BigQueryLoadJobID, err)
log.Println(msg)
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(http.StatusInternalServerError)
_, err := w.Write([]byte(msg))
if err != nil {
log.Println(err)
}
return
}
switch res.Status {
case bigquery.Running:
_, err := bqloadJobStore.IncrementJobStatusCheckCount(ctx, form.DS2BQJobID, form.BQLoadKind)
if err != nil {
log.Printf("failed DSExportJobStore.IncrementJobStatusCheckCount. DS2BQJobID=%v,BQLoadKind=%v,err=%v\n", form.DS2BQJobID, form.BQLoadKind, err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusConflict)
case bigquery.Fail:
_, err := bqloadJobStore.FinishExportJob(ctx, form.DS2BQJobID, form.BQLoadKind, BQLoadJobStatusFailed, fmt.Sprintf("MSG=%v", res.ErrMessage))
if err != nil {
log.Printf("failed BQLOadJobStore.FinishExportJob. DS2BQJobID=%v,BQLoadKind=%v,err=%v\n", form.DS2BQJobID, form.BQLoadKind, err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
case bigquery.Done:
_, err := bqloadJobStore.FinishExportJob(ctx, form.DS2BQJobID, form.BQLoadKind, BQLoadJobStatusDone, "")
if err != nil {
log.Printf("failed BQLOadJobStore.FinishExportJob. DS2BQJobID=%v,BQLoadKind=%v,err=%v\n", form.DS2BQJobID, form.BQLoadKind, err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
default:
log.Printf("%v is Unsupported Status\n", res.Status)
w.WriteHeader(http.StatusInternalServerError)
}
}