Skip to content

Commit

Permalink
ログをひとまとめにするやつを適当に入れてみた refs #10
Browse files Browse the repository at this point in the history
  • Loading branch information
sinmetal committed Jul 9, 2019
1 parent 404cff7 commit 7fff845
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
22 changes: 13 additions & 9 deletions datastore_export_job_check_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/gcpug/ds2bq/datastore"
"github.com/morikuni/failure"
"github.com/sinmetal/silverdog/dogtime"
slog "github.com/sinmetal/slog/v2"
)

var ErrTimeout failure.StringCode = "Timeout"
Expand All @@ -24,6 +25,9 @@ type DatastoreExportJobCheckRequest struct {
}

func HandleDatastoreExportJobCheckAPI(w http.ResponseWriter, r *http.Request) {
ctx := slog.WithValue(r.Context())
defer slog.Flush(ctx)

b, err := ioutil.ReadAll(r.Body)
if err != nil {
msg := fmt.Sprintf("failed ioutil.Read(request.Body).err=%+v", err)
Expand Down Expand Up @@ -66,19 +70,19 @@ func HandleDatastoreExportJobCheckAPI(w http.ResponseWriter, r *http.Request) {
}
switch res.Status {
case datastore.Running:
log.Printf("%s is Running...\n", form.DatastoreExportJobID)
slog.Info(ctx, slog.KV{"MSG", fmt.Sprintf("%s is Running...\n", form.DatastoreExportJobID)})
w.WriteHeader(http.StatusConflict)
case datastore.Fail:
log.Printf("%s is Fail. ErrCode=%v,ErrMessage=%v\n", form.DatastoreExportJobID, res.ErrCode, res.ErrMessage)
slog.Info(ctx, slog.KV{"MSG", fmt.Sprintf("%s is Fail. ErrCode=%v,ErrMessage=%v\n", form.DatastoreExportJobID, res.ErrCode, res.ErrMessage)})
w.WriteHeader(http.StatusOK)
case datastore.Done:
log.Printf("%s is Done...\n", form.DatastoreExportJobID)
if err := ReceiveStorageChangeNotify(r.Context(), form.DS2BQJobID); err != nil {
log.Printf("failed ReceiveStorageChangeNotify. err=%v\n", err)
slog.Info(ctx, slog.KV{"MSG", fmt.Sprintf("%s is Done...\n", form.DatastoreExportJobID)})
if err := ReceiveStorageChangeNotify(ctx, form.DS2BQJobID); err != nil {
slog.Info(ctx, slog.KV{"MSG", fmt.Sprintf("failed ReceiveStorageChangeNotify. err=%v\n", err)})
}
w.WriteHeader(http.StatusOK)
default:
log.Printf("%v is Unsupported Status\n", res.Status)
slog.Info(ctx, slog.KV{"MSG", fmt.Sprintf("%v is Unsupported Status\n", res.Status)})
w.WriteHeader(http.StatusInternalServerError)
}
}
Expand All @@ -105,10 +109,10 @@ func ReceiveStorageChangeNotify(ctx context.Context, ds2bqJobID string) error {
errch := make(chan error, 1)
go func() {
if err := ls.ReceiveStorageChangeNotify(cctx, ds2bqJobID); err != nil {
log.Printf("failed ReceiveStorageChangeNotify. jobID=%s\n", ds2bqJobID)
slog.Info(ctx, slog.KV{"MSG", fmt.Sprintf("failed ReceiveStorageChangeNotify. jobID=%s\n", ds2bqJobID)})
errch <- err
}
log.Printf("finish! ReceiveStorageChangeNotify. jobID=%s\n", ds2bqJobID)
slog.Info(ctx, slog.KV{"MSG", fmt.Sprintf("finish! ReceiveStorageChangeNotify. jobID=%s\n", ds2bqJobID)})
}()

return WaitBQLoadJobStatusChecker(cctx, 60*time.Second, bljs, ds2bqJobID, errch)
Expand Down Expand Up @@ -144,7 +148,7 @@ func IsBQLoadJobStatusAllDone(ctx context.Context, bljs *BQLoadJobStore, ds2bqJo
}
allDone := true
for _, job := range jobs {
log.Printf("BQLoadJobStatusCheck. jobID=%v,kind=%v,status=%v\n", job.JobID, job.Kind, job.Status)
slog.Info(ctx, slog.KV{"MSG", fmt.Sprintf("BQLoadJobStatusCheck. jobID=%v,kind=%v,status=%v\n", job.JobID, job.Kind, job.Status)})
if job.Status != BQLoadJobStatusDone {
allDone = false
}
Expand Down
5 changes: 3 additions & 2 deletions datastore_export_job_check_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"bytes"
"context"
"encoding/json"
"github.com/morikuni/failure"
"github.com/sinmetal/silverdog/dogtime"
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/morikuni/failure"
"github.com/sinmetal/silverdog/dogtime"
)

func TestHandleDatastoreExportJobCheckAPI(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ require (
github.com/google/uuid v1.1.1
github.com/morikuni/failure v0.11.0
github.com/sinmetal/gcpmetadata v0.0.0-20190612100641-8951dfda8549
github.com/sinmetal/silverdog v0.0.0-20190703104659-dfc123220739
github.com/sinmetal/silverdog v0.0.0-20190709122325-a885443c3d17
github.com/sinmetal/slog v0.0.0-20190709124601-586365e285f5
go.mercari.io/datastore v1.4.2
google.golang.org/api v0.6.0
google.golang.org/genproto v0.0.0-20190611190212-a7e196e89fd3
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ github.com/sinmetal/gcpmetadata v0.0.0-20190612100641-8951dfda8549 h1:ei53S97TEe
github.com/sinmetal/gcpmetadata v0.0.0-20190612100641-8951dfda8549/go.mod h1:ct28oEQbWXPCAoVGGyFNoMr7rk+mIyeGcHJqR1Ax7cI=
github.com/sinmetal/silverdog v0.0.0-20190703104659-dfc123220739 h1:seJjztyt97UXFz99mb71Gwdrhr6Zcd9kkelljQOFWbk=
github.com/sinmetal/silverdog v0.0.0-20190703104659-dfc123220739/go.mod h1:dI62sSZ829OY67VatpXl2qj++RguayZd6zwHUVtrj9o=
github.com/sinmetal/silverdog v0.0.0-20190709122325-a885443c3d17 h1:tZVZfKSIV4Fsobho6vWbe8ZSVSuB2uztAZMGbPu/Go8=
github.com/sinmetal/silverdog v0.0.0-20190709122325-a885443c3d17/go.mod h1:dI62sSZ829OY67VatpXl2qj++RguayZd6zwHUVtrj9o=
github.com/sinmetal/slog v0.0.0-20190709124601-586365e285f5 h1:Ju3rtu9u9HpvF37x/viMqQAUkLCanVLdVMtUj5ex1cw=
github.com/sinmetal/slog v0.0.0-20190709124601-586365e285f5/go.mod h1:7abRLuXyxKUl/2wBVCkDmFPU8bI4nRVIoZDqOPv5W4o=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
Expand Down

0 comments on commit 7fff845

Please sign in to comment.