Skip to content

Commit

Permalink
connect_handlerにロガーを仕込む。
Browse files Browse the repository at this point in the history
  • Loading branch information
Azuki-bar committed Nov 12, 2023
1 parent a0be491 commit 1442a66
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 7 deletions.
1 change: 1 addition & 0 deletions backend/state-manager/pkg/connect/connect_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
15 changes: 11 additions & 4 deletions backend/state-manager/pkg/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package db
import (
"context"
"log"
"log/slog"
"os"

statev1 "github.com/ueckoken/plarail2023/backend/spec/state/v1"
Expand All @@ -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")
}

/*
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 1442a66

Please sign in to comment.