diff --git a/backend/state-manager/pkg/connect/connect_handler.go b/backend/state-manager/pkg/connect/connect_handler.go index 7fa85b41..ad6e1f55 100644 --- a/backend/state-manager/pkg/connect/connect_handler.go +++ b/backend/state-manager/pkg/connect/connect_handler.go @@ -120,6 +120,7 @@ func (s *StateManagerServer) UpdateStopState( connect.CodeUnknown, errors.New("db error"), ) + slog.Default().Error("db connection error", err) return nil, err } mqtt_handler.NotifyStateUpdate("stop", req.Msg.State.Id, req.Msg.State.State.String()) diff --git a/backend/state-manager/pkg/db/db.go b/backend/state-manager/pkg/db/db.go index 5736ee75..03995226 100644 --- a/backend/state-manager/pkg/db/db.go +++ b/backend/state-manager/pkg/db/db.go @@ -7,6 +7,7 @@ package db import ( "context" "log" + "log/slog" "os" statev1 "github.com/ueckoken/plarail2023/backend/spec/state/v1" @@ -21,24 +22,29 @@ var client *mongo.Client func Open() { var err error - //log.Println("Connecting to MongoDB...") + slog.Default().Debug("Connecting to MongoDB...") uri := os.Getenv("MONGODB_URI") if uri == "" { log.Fatal("No MONGODB_URI set") } + // TODO: Open関数がctxを受けるようにして、そのctxの子contextをDBのコネクションに使う client, err = mongo.Connect(context.TODO(), options.Client().ApplyURI(uri)) if err != nil { + // TODO: return err, do not panic! + slog.Default().Error("database connection failed", slog.Any("err", err)) panic(err) } - //log.Println("Connected to DB!") + slog.Default().Debug("connected to DB") } func C() { - //log.Println("Closing connection to DB...") + slog.Default().Debug("Closing connection to DB...") + // TODO: contextを受けて、その子contextをDBクライアントに渡す if err := client.Disconnect(context.TODO()); err != nil { + slog.Default().Error("DB Connection Closing failed") log.Println(err) } - //log.Println("Connection closed!") + slog.Default().Debug("DB Connection is successfully closed") } /* @@ -81,6 +87,7 @@ func GetPoints() []*statev1.PointAndState { collection := client.Database("state-manager").Collection("points") cursor, err := collection.Find(context.Background(), bson.M{}) if err != nil { + slog.Default().Warn("Get Points failed", slog.Any("err", err)) panic(err) } var result []*statev1.PointAndState diff --git a/go.mod b/go.mod index 82f01763..fff44c56 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,6 @@ require ( github.com/go-chi/httplog/v2 v2.0.7 github.com/joho/godotenv v1.5.1 go.mongodb.org/mongo-driver v1.13.0 - golang.org/x/exp v0.0.0-20231108232855-2478ac86f678 golang.org/x/net v0.18.0 golang.org/x/sync v0.5.0 google.golang.org/protobuf v1.31.0 @@ -52,6 +51,7 @@ require ( golang.org/x/term v0.14.0 // indirect golang.org/x/text v0.14.0 // indirect golang.org/x/time v0.3.0 // indirect + golang.org/x/tools v0.15.0 // indirect google.golang.org/appengine v1.6.7 // indirect gopkg.in/inf.v0 v0.9.1 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect diff --git a/go.sum b/go.sum index f7108caa..d9151842 100644 --- a/go.sum +++ b/go.sum @@ -113,8 +113,6 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5y golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.15.0 h1:frVn1TEaCEaZcn3Tmd7Y2b5KKPaZ+I32Q2OA3kYp5TA= golang.org/x/crypto v0.15.0/go.mod h1:4ChreQoLWfG3xLDer1WdlH5NdlQ3+mwnQq1YTKY+72g= -golang.org/x/exp v0.0.0-20231108232855-2478ac86f678 h1:mchzmB1XO2pMaKFRqk/+MV3mgGG96aqaPXaMifQU47w= -golang.org/x/exp v0.0.0-20231108232855-2478ac86f678/go.mod h1:zk2irFbV9DP96SEBUUAy67IdHUaZuSnrz1n472HUCLE= golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=