From 69c36d9fe004bc494754c1153921ed447ee8de3f Mon Sep 17 00:00:00 2001 From: tkc66 Date: Sat, 8 Oct 2022 23:18:13 +0900 Subject: [PATCH 01/18] =?UTF-8?q?add:=20=E9=80=9A=E7=9F=A5=E3=82=921?= =?UTF-8?q?=E5=9B=9E=E3=81=AE=E3=81=BF=E3=81=AB=E3=81=99=E3=82=8B=E3=81=9F?= =?UTF-8?q?=E3=82=81=E3=81=ABredis=E3=82=92=E7=94=A8=E6=84=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/server/config/config.go | 5 +++++ cmd/server/example.yaml | 2 ++ cmd/server/main.go | 2 ++ docker-compose.yml | 12 ++++++++++++ go.mod | 7 +++++++ go.sum | 8 ++++++++ pkg/dkwatcher/dkwatcher.go | 20 +++++++++++++++++--- pkg/infrastructure/db/redis.go | 33 +++++++++++++++++++++++++++++++++ pkg/model/talk.go | 18 +++++++++++++++++- pkg/model/talk_test.go | 3 ++- pkg/notifier/controller.go | 3 +-- pkg/notifier/notifier.go | 10 ++++++++++ 12 files changed, 116 insertions(+), 7 deletions(-) create mode 100644 docker-compose.yml create mode 100644 pkg/infrastructure/db/redis.go diff --git a/cmd/server/config/config.go b/cmd/server/config/config.go index 2ff7a0f..9878a4c 100644 --- a/cmd/server/config/config.go +++ b/cmd/server/config/config.go @@ -38,6 +38,7 @@ type Config struct { Debug DebugConfig `json:"debug"` WsProxyBindAddr string `json:"bindAddr" default:":20080"` Dreamkast DreamkastConfig `json:"dreamkast" validate:"required"` + Redis RedisConfig `json:"redis " validate:"required"` Tracks []TrackConfig `json:"tracks" validate:"required"` } @@ -74,3 +75,7 @@ type SlackConfig struct { BotToken string `json:"botToken" validate:"required"` ChannelId string `json:"channelId" validate:"required"` } + +type RedisConfig struct { + Host string `json:"host" validate:"required"` +} diff --git a/cmd/server/example.yaml b/cmd/server/example.yaml index c221087..24a83d0 100644 --- a/cmd/server/example.yaml +++ b/cmd/server/example.yaml @@ -5,6 +5,8 @@ dreamkast: auth0Domain: ${AUTH0_DOMAIN} auth0ClientId: ${AUTH0_CLIENT_ID} auth0ClientSecret: ${AUTH0_CLIENT_SECRET} +resids: + host: https://xxx.jp:6379 // TODO:reis作成後に設定する tracks: - dkTrackId: 29 obs: diff --git a/cmd/server/main.go b/cmd/server/main.go index 0836e0f..34be06c 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -65,6 +65,7 @@ func main() { Auth0ClientId: conf.Dreamkast.Auth0ClientId, Auth0ClientSecret: conf.Dreamkast.Auth0ClientSecret, Auth0ClientAudience: conf.Dreamkast.Auth0ClientAudience, + RedisHost: conf.Redis.Host, NotificationEventSendChan: talkStream, }) }) @@ -83,6 +84,7 @@ func main() { return notifier.Run(ctx, notifier.Config{ Development: conf.Debug.Development, Debug: conf.Debug.Debug, + RedisHost: conf.Redis.Host, Targets: targets, NotificationEventReceiveChan: talkStream, }) diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..a0b2f14 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,12 @@ +version: "3.8" +services: + redis: + image: redis:alpine3.16 + container_name: cnd_redis + networks: + - cnd + ports: + - "6379:6379" +networks: + cnd: + external: true diff --git a/go.mod b/go.mod index 6ec9b1b..316baff 100644 --- a/go.mod +++ b/go.mod @@ -22,10 +22,17 @@ require ( sigs.k8s.io/yaml v1.3.0 ) +require ( + github.com/cespare/xxhash/v2 v2.1.2 // indirect + github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect +) + require ( github.com/buger/jsonparser v1.1.1 // indirect github.com/go-playground/locales v0.14.0 // indirect github.com/go-playground/universal-translator v0.18.0 // indirect + github.com/go-redis/redis v6.15.9+incompatible + github.com/go-redis/redis/v8 v8.11.5 github.com/gorilla/websocket v1.5.0 // indirect github.com/hashicorp/logutils v1.0.0 // indirect github.com/inconshreveable/mousetrap v1.0.0 // indirect diff --git a/go.sum b/go.sum index c64af2d..63ebbef 100644 --- a/go.sum +++ b/go.sum @@ -9,6 +9,8 @@ github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZx github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs= github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= +github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE= +github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= @@ -18,6 +20,8 @@ github.com/creasty/defaults v1.6.0/go.mod h1:iGzKe6pbEHnpMPtfDXZEr0NVxWnPTjb1bbD github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= @@ -37,6 +41,10 @@ github.com/go-playground/universal-translator v0.18.0 h1:82dyy6p4OuJq4/CByFNOn/j github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl+lu/H90nyDXpg0fqeB/AQUGNTVA= github.com/go-playground/validator/v10 v10.11.1 h1:prmOlTVv+YjZjmRmNSF3VmspqJIxJWXmqUsHwfTRRkQ= github.com/go-playground/validator/v10 v10.11.1/go.mod h1:i+3WkQ1FvaUjjxh1kSvIA4dMGDBiPU55YFDl0WbKdWU= +github.com/go-redis/redis v6.15.9+incompatible h1:K0pv1D7EQUjfyoMql+r/jZqCLizCGKFlFgcHWWmHQjg= +github.com/go-redis/redis v6.15.9+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA= +github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI= +github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/go-test/deep v1.0.4 h1:u2CU3YKy9I2pmu9pX0eq50wCgjfGIt539SqR7FbHiho= github.com/go-test/deep v1.0.4/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA= diff --git a/pkg/dkwatcher/dkwatcher.go b/pkg/dkwatcher/dkwatcher.go index 2df66da..5cf8222 100644 --- a/pkg/dkwatcher/dkwatcher.go +++ b/pkg/dkwatcher/dkwatcher.go @@ -9,6 +9,7 @@ import ( "go.uber.org/zap" "golang.org/x/xerrors" + "github.com/cloudnativedaysjp/cnd-operation-server/pkg/infrastructure/db" "github.com/cloudnativedaysjp/cnd-operation-server/pkg/infrastructure/dreamkast" "github.com/cloudnativedaysjp/cnd-operation-server/pkg/infrastructure/sharedmem" "github.com/cloudnativedaysjp/cnd-operation-server/pkg/model" @@ -26,6 +27,7 @@ type Config struct { Auth0ClientId string Auth0ClientSecret string Auth0ClientAudience string + RedisHost string NotificationEventSendChan chan<- model.Talk } @@ -53,6 +55,11 @@ func Run(ctx context.Context, conf Config) error { return err } + redisClient, err := db.NewRedisClient(conf.RedisHost) + if err != nil { + return err + } + mw := sharedmem.Writer{UseStorageForTalks: true} mr := sharedmem.Reader{UseStorageForDisableAutomation: true} @@ -63,7 +70,9 @@ func Run(ctx context.Context, conf Config) error { logger.Info("context was done.") return nil case <-tick.C: - if err := procedure(ctx, dkClient, mw, mr, conf.NotificationEventSendChan); err != nil { + if err := procedure(ctx, dkClient, mw, mr, + conf.NotificationEventSendChan, redisClient, + ); err != nil { return err } } @@ -72,7 +81,7 @@ func Run(ctx context.Context, conf Config) error { func procedure(ctx context.Context, dkClient dreamkast.ClientIface, mw sharedmem.WriterIface, mr sharedmem.ReaderIface, - notificationEventSendChan chan<- model.Talk, + notificationEventSendChan chan<- model.Talk, redisClient *db.RedisClient, ) error { logger := utils.GetLogger(ctx) @@ -102,7 +111,12 @@ func procedure(ctx context.Context, logger.Error(xerrors.Errorf("message: %w", err), "mw.SetTalks was failed") continue } - if talks.WillStartNextTalkSince() { + hasNotify, err := talks.HasNotify(ctx, redisClient) + if err != nil { + logger.Error(xerrors.Errorf("message: %w", err), "talks.HasNotify() was failed") + continue + } + if !hasNotify { nextTalk, err := talks.GetNextTalk(currentTalk) if err != nil { logger.Error(xerrors.Errorf("message: %w", err), "talks.GetNextTalk was failed") diff --git a/pkg/infrastructure/db/redis.go b/pkg/infrastructure/db/redis.go new file mode 100644 index 0000000..16eb597 --- /dev/null +++ b/pkg/infrastructure/db/redis.go @@ -0,0 +1,33 @@ +package db + +import ( + "context" + "time" + + "github.com/go-redis/redis/v8" + "golang.org/x/xerrors" +) + +type RedisClient struct { + Client *redis.Client +} + +const ( + RedisExpiration = 10 * time.Minute + NextTalkNotificationKey = "nextTalkNotificationAlreadySentFlag" + NextTalkNotificationAlreadySentFlag = true +) + +func NewRedisClient(addr string) (*RedisClient, error) { + client := redis.NewClient(&redis.Options{ + Addr: addr, + Password: "", + DB: 0, + }) + if err := client.Ping(context.TODO()).Err(); err != nil { + return nil, xerrors.Errorf("fail to connect to redis. message: %w", err) + } + return &RedisClient{ + Client: client, + }, nil +} diff --git a/pkg/model/talk.go b/pkg/model/talk.go index c4a35b1..7abc56f 100644 --- a/pkg/model/talk.go +++ b/pkg/model/talk.go @@ -1,9 +1,11 @@ package model import ( + "context" "fmt" "time" + "github.com/cloudnativedaysjp/cnd-operation-server/pkg/infrastructure/db" "github.com/cloudnativedaysjp/cnd-operation-server/pkg/utils" ) @@ -44,7 +46,7 @@ func (t Talk) GetTalkType(title string, presentationMethod *string) (TalkType, e return t.convertTalkType(title, presentationMethod) } -func (ts Talks) WillStartNextTalkSince() bool { +func (ts Talks) isStartNextTalkSoon() bool { now := nowFunc() for _, talk := range ts { if now.After(talk.StartAt) { @@ -57,6 +59,20 @@ func (ts Talks) WillStartNextTalkSince() bool { return false } +func (ts Talks) HasNotify(ctx context.Context, rc *db.RedisClient) (bool, error) { + // 次のtalkがもうすぐ始まるか判定し,まだ通知が行われていない場合は通知を行う. + if ts.isStartNextTalkSoon() { + result := rc.Client.Get(ctx, db.NextTalkNotificationKey) + if result.Err() != nil { + return false, result.Err() + } + if result != nil { + return false, nil + } + } + return true, nil +} + func (ts Talks) GetCurrentTalk() (*Talk, error) { now := nowFunc() for _, talk := range ts { diff --git a/pkg/model/talk_test.go b/pkg/model/talk_test.go index e37255a..bb63166 100644 --- a/pkg/model/talk_test.go +++ b/pkg/model/talk_test.go @@ -178,7 +178,7 @@ func TestTalk_WillStartNextTalkSince(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got := tt.talks.WillStartNextTalkSince() + got := tt.talks.isStartNextTalkSoon() if got != tt.want { t.Errorf("Talk.GetTalkType() want %v", tt.want) } @@ -193,6 +193,7 @@ func TestTalk_GetCurrentTalk(t *testing.T) { tests := []struct { name string talks Talks + wantId int wantErr bool }{ { diff --git a/pkg/notifier/controller.go b/pkg/notifier/controller.go index 794e34a..6d3dfb3 100644 --- a/pkg/notifier/controller.go +++ b/pkg/notifier/controller.go @@ -14,8 +14,7 @@ import ( type Controller struct { logger logr.Logger slackClients map[int32]slack.ClientIface - - channelIds map[int32]string + channelIds map[int32]string } func NewController(logger logr.Logger, diff --git a/pkg/notifier/notifier.go b/pkg/notifier/notifier.go index bf9b255..4dd50c1 100644 --- a/pkg/notifier/notifier.go +++ b/pkg/notifier/notifier.go @@ -8,6 +8,7 @@ import ( "go.uber.org/zap" "golang.org/x/xerrors" + "github.com/cloudnativedaysjp/cnd-operation-server/pkg/infrastructure/db" "github.com/cloudnativedaysjp/cnd-operation-server/pkg/infrastructure/slack" "github.com/cloudnativedaysjp/cnd-operation-server/pkg/model" ) @@ -18,6 +19,7 @@ type Config struct { Development bool Debug bool Targets []Target + RedisHost string NotificationEventReceiveChan <-chan model.Talk } @@ -43,6 +45,11 @@ func Run(ctx context.Context, conf Config) error { slackClients := make(map[int32]slack.ClientIface) channelIds := make(map[int32]string) + + redisClient, err := db.NewRedisClient(conf.RedisHost) + if err != nil { + return err + } for _, target := range conf.Targets { slackClients[target.TrackId], err = slack.NewClient(target.SlackBotToken) if err != nil { @@ -61,6 +68,9 @@ func Run(ctx context.Context, conf Config) error { if err := c.Receive(talk); err != nil { logger.Error(xerrors.Errorf("message: %w", err), "notification failed") } + if result := redisClient.Client.Set(ctx, db.NextTalkNotificationKey, db.NextTalkNotificationAlreadySentFlag, db.RedisExpiration); result.Err() != nil { + return xerrors.Errorf("message: %w", result.Err()) + } } } } From c1729bbe4a7fa42d08d68bae2b6b476b060e996b Mon Sep 17 00:00:00 2001 From: tkc66 Date: Sat, 8 Oct 2022 23:22:49 +0900 Subject: [PATCH 02/18] =?UTF-8?q?fix:=20string=E3=81=AF=E5=B0=8F=E6=96=87?= =?UTF-8?q?=E5=AD=97=E3=81=AB=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/model/talk.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/model/talk.go b/pkg/model/talk.go index 7abc56f..11b548b 100644 --- a/pkg/model/talk.go +++ b/pkg/model/talk.go @@ -80,19 +80,19 @@ func (ts Talks) GetCurrentTalk() (*Talk, error) { return &talk, nil } } - return nil, fmt.Errorf("Current talk not found") + return nil, fmt.Errorf("current talk not found") } func (ts Talks) GetNextTalk(currentTalk *Talk) (*Talk, error) { for i, talk := range ts { if talk.Id == currentTalk.Id { if i+1 == len(ts) { - return nil, fmt.Errorf("This talk is last") + return nil, fmt.Errorf("this talk is last") } return &ts[i+1], nil } } - return nil, fmt.Errorf("Next talk not found") + return nil, fmt.Errorf("next talk not found") } func (t Talk) GetActualStartAtAndEndAt(conferenceDayDate string, startAt, endAt time.Time) (time.Time, time.Time, error) { From 53b1ab542f887051bf04feb0a2c165330e8dea1e Mon Sep 17 00:00:00 2001 From: tkc66 Date: Sat, 8 Oct 2022 23:49:27 +0900 Subject: [PATCH 03/18] fix: TestTalk_GetCurrentTalk --- pkg/model/talk_test.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/pkg/model/talk_test.go b/pkg/model/talk_test.go index bb63166..0e4c688 100644 --- a/pkg/model/talk_test.go +++ b/pkg/model/talk_test.go @@ -298,6 +298,7 @@ func TestTalk_GetNextTalk(t *testing.T) { name string args *Talk talks Talks + wantId int32 wantErr bool }{ { @@ -466,13 +467,22 @@ func TestTalk_GetNextTalk(t *testing.T) { }, }, wantErr: false, + wantId: 4, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - _, err := tt.talks.GetNextTalk(tt.args) - if err == nil && tt.wantErr { + got, err := tt.talks.GetNextTalk(tt.args) + if (err == nil && tt.wantErr) || (err != nil && !tt.wantErr) { t.Errorf("Talk.GetTalkType() wantErr %v", tt.wantErr) + return + } + if err != nil { + return + } + if got.Id != int32(tt.wantId) { + t.Errorf("Talk.GetTalkType() wantId %v", tt.wantId) + return } }) } From dfc56c28175928b8b543362fc03d5e7482cc8712 Mon Sep 17 00:00:00 2001 From: tkc66 Date: Sun, 9 Oct 2022 00:03:08 +0900 Subject: [PATCH 04/18] fix: GetActualStartAtAndEndAt --- pkg/model/talk_test.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/pkg/model/talk_test.go b/pkg/model/talk_test.go index 0e4c688..3674754 100644 --- a/pkg/model/talk_test.go +++ b/pkg/model/talk_test.go @@ -520,19 +520,18 @@ func TestTalk_GetActualStartAtAndEndAt(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - startAt, endAt, err := talk.GetActualStartAtAndEndAt(tt.conferenceDayDate, tt.startAt, tt.endAt) - if tt.wantErr { - if err == nil { - t.Errorf("Talk.GetActualStartAtAndEndAt() error = %v, wantErr %v", err, tt.wantErr) - return - } + gotStartAt, gotEndAt, err := talk.GetActualStartAtAndEndAt(tt.conferenceDayDate, tt.startAt, tt.endAt) + if (err == nil && tt.wantErr) || (err != nil && !tt.wantErr) { + t.Errorf("Talk.GetActualStartAtAndEndAt() error = %v, wantErr %v", err, tt.wantErr) return } - if err != nil || startAt.Equal(tt.startAt) || endAt.Equal(tt.endAt) { + if err != nil { + return + } + if !gotStartAt.Equal(tt.wantStartAt) || !gotEndAt.Equal(tt.wantEndAt) { t.Errorf("Talk.GetActualStartAtAndEndAt() error = %v, wantStatAt = %v, wantEndAt = %v,", err, tt.wantStartAt, tt.wantEndAt) return } - }) } } From aa3d2580688a0620c7cd8b2224a23ccac268f370 Mon Sep 17 00:00:00 2001 From: tkc66 Date: Sat, 15 Oct 2022 22:04:22 +0900 Subject: [PATCH 05/18] fix:test --- pkg/model/talk_test.go | 44 +++++++++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/pkg/model/talk_test.go b/pkg/model/talk_test.go index 3674754..5245186 100644 --- a/pkg/model/talk_test.go +++ b/pkg/model/talk_test.go @@ -1,9 +1,12 @@ package model import ( + "context" + "fmt" "testing" "time" + "github.com/cloudnativedaysjp/cnd-operation-server/pkg/infrastructure/db" "github.com/cloudnativedaysjp/cnd-operation-server/pkg/testutils" ) @@ -83,19 +86,27 @@ func setTestTime(v string) time.Time { return pt } -func TestTalk_WillStartNextTalkSince(t *testing.T) { +func TestTalk_HasNotify(t *testing.T) { nowFunc = func() time.Time { return time.Date(2022, 10, 01, 12, 27, 00, 0, time.UTC) } + ctx := context.Background() + rcClient, err := db.NewRedisClient("http://localhost:6379") + if err != nil { + fmt.Errorf("message: %w", err) + return + } tests := []struct { - name string - talks Talks - want bool + name string + talks Talks + want bool + wantErr bool }{ { - name: "no talk", - talks: Talks{}, - want: false, + name: "no talk", + talks: Talks{}, + want: false, + wantErr: true, }, { name: "not found next talk", @@ -134,7 +145,8 @@ func TestTalk_WillStartNextTalkSince(t *testing.T) { EndAt: setTestTime("2022-10-01T14:00:00"), }, }, - want: false, + want: false, + wantErr: true, }, { name: "found next talk", @@ -173,14 +185,24 @@ func TestTalk_WillStartNextTalkSince(t *testing.T) { EndAt: setTestTime("2022-10-01T12:30:00"), }, }, - want: true, + want: true, + wantErr: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got := tt.talks.isStartNextTalkSoon() + got, err := tt.talks.HasNotify(ctx, rcClient) + if (err != nil) != tt.wantErr { + t.Errorf("Talk.HasNotify() error = %v, wantErr %v", err, tt.wantErr) + return + } if got != tt.want { - t.Errorf("Talk.GetTalkType() want %v", tt.want) + t.Errorf("Talk.HasNotify() = %v, want %v", got, tt.want) + return + } + if got { + cmd := rcClient.Client.Get(ctx, db.NextTalkNotificationKey) + fmt.Println(cmd.Bool()) } }) } From f584a2e07d07c28442f893d832e0455965d82fb7 Mon Sep 17 00:00:00 2001 From: tkc66 Date: Sat, 15 Oct 2022 23:52:30 +0900 Subject: [PATCH 06/18] =?UTF-8?q?fix:main=E3=81=AE=E5=87=A6=E7=90=86?= =?UTF-8?q?=E3=81=AB=E5=90=88=E3=82=8F=E3=81=9B=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- development/README.md | 1 + development/docker-compose.without-server.yml | 7 +++ development/docker-compose.yml | 7 +++ docker-compose.yml | 12 ---- go.mod | 3 +- go.sum | 21 ++----- pkg/dkwatcher/dkwatcher.go | 6 +- pkg/model/talk.go | 58 ++++--------------- pkg/model/talk_test.go | 4 +- 9 files changed, 36 insertions(+), 83 deletions(-) delete mode 100644 docker-compose.yml diff --git a/development/README.md b/development/README.md index 3c72575..ae03c0e 100644 --- a/development/README.md +++ b/development/README.md @@ -27,6 +27,7 @@ SWITHCER01_SLACK_CHANNEL_ID=... GITHUB_ACCESS_TOKEN=... SWITCHER01_HOST=... SWITCHER01_PASSWORD=... +REDIS_HOST=... ``` * コンテナの起動 diff --git a/development/docker-compose.without-server.yml b/development/docker-compose.without-server.yml index f99e72d..0abb14f 100644 --- a/development/docker-compose.without-server.yml +++ b/development/docker-compose.without-server.yml @@ -18,6 +18,13 @@ services: - 18080:8080 - 18081:8081 + redis: + image: redis:alpine3.16 + ports: + - "6379:6379" + environment: + REDIS_HOST: localhost:6379 + dk-mock-server-init: image: curlimages/curl entrypoint: diff --git a/development/docker-compose.yml b/development/docker-compose.yml index 535631a..1d9bc42 100644 --- a/development/docker-compose.yml +++ b/development/docker-compose.yml @@ -16,6 +16,13 @@ services: SLACK_BOT_TOKEN: ${SLACK_BOT_TOKEN} SWITCHER01_SLACK_CHANNEL_ID: ${SWITCHER01_SLACK_CHANNEL_ID} + redis: + image: redis:alpine3.16 + ports: + - "6379:6379" + environment: + REDIS_HOST: ${REDIS_HOST} + seaman: image: public.ecr.aws/f5j9d0q5/seaman:0.0.9 command: --config=/mnt/seaman.yaml diff --git a/docker-compose.yml b/docker-compose.yml deleted file mode 100644 index a0b2f14..0000000 --- a/docker-compose.yml +++ /dev/null @@ -1,12 +0,0 @@ -version: "3.8" -services: - redis: - image: redis:alpine3.16 - container_name: cnd_redis - networks: - - cnd - ports: - - "6379:6379" -networks: - cnd: - external: true diff --git a/go.mod b/go.mod index 5af7384..73e4922 100644 --- a/go.mod +++ b/go.mod @@ -27,16 +27,15 @@ require ( ) require ( + github.com/beorn7/perks v1.0.1 // indirect github.com/cespare/xxhash/v2 v2.1.2 // indirect github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect ) require ( github.com/buger/jsonparser v1.1.1 // indirect - github.com/cespare/xxhash/v2 v2.1.2 // indirect github.com/go-playground/locales v0.14.0 // indirect github.com/go-playground/universal-translator v0.18.0 // indirect - github.com/go-redis/redis v6.15.9+incompatible github.com/go-redis/redis/v8 v8.11.5 github.com/gorilla/websocket v1.5.0 // indirect github.com/hashicorp/logutils v1.0.0 // indirect diff --git a/go.sum b/go.sum index 0862a78..eff7a52 100644 --- a/go.sum +++ b/go.sum @@ -51,8 +51,6 @@ github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6r github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs= github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= -github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE= -github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE= github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= @@ -60,8 +58,6 @@ github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWR github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= -github.com/cloudnativedaysjp/seaman v0.0.7 h1:a5dXzklLiVMX+GieV2s67pRbyIiajqlb/0r+g2JAuaU= -github.com/cloudnativedaysjp/seaman v0.0.7/go.mod h1:sPUguGLeztUQ0qMMJlbD9rSLVFxum96CEtNgMIzbR+w= github.com/cloudnativedaysjp/seaman v0.0.9 h1:gDESi7wOiEmLM0JO1guOe7QFfMQD3zn5VDQlP/hg7ss= github.com/cloudnativedaysjp/seaman v0.0.9/go.mod h1:sPUguGLeztUQ0qMMJlbD9rSLVFxum96CEtNgMIzbR+w= github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= @@ -78,6 +74,7 @@ github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymF github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= +github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= @@ -102,8 +99,6 @@ github.com/go-playground/universal-translator v0.18.0 h1:82dyy6p4OuJq4/CByFNOn/j github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl+lu/H90nyDXpg0fqeB/AQUGNTVA= github.com/go-playground/validator/v10 v10.11.1 h1:prmOlTVv+YjZjmRmNSF3VmspqJIxJWXmqUsHwfTRRkQ= github.com/go-playground/validator/v10 v10.11.1/go.mod h1:i+3WkQ1FvaUjjxh1kSvIA4dMGDBiPU55YFDl0WbKdWU= -github.com/go-redis/redis v6.15.9+incompatible h1:K0pv1D7EQUjfyoMql+r/jZqCLizCGKFlFgcHWWmHQjg= -github.com/go-redis/redis v6.15.9+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA= github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI= github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= @@ -177,8 +172,6 @@ github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI65Y= github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64= github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= -github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= -github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc= github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= @@ -207,12 +200,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= -github.com/labstack/echo/v4 v4.9.0 h1:wPOF1CE6gvt/kmbMR4dGzWvHMPT+sAEUJOwOTtvITVY= -github.com/labstack/echo/v4 v4.9.0/go.mod h1:xkCDAdFCIf8jsFQ5NnbK7oqaF/yU1A1X20Ltm0OvSks= github.com/labstack/echo/v4 v4.9.1 h1:GliPYSpzGKlyOhqIbG8nmHBo3i1saKWFOgh41AN3b+Y= github.com/labstack/echo/v4 v4.9.1/go.mod h1:Pop5HLc+xoc4qhTZ1ip6C0RtP7Z+4VzRLWZZFKqbbjo= -github.com/labstack/gommon v0.3.1 h1:OomWaJXm7xR6L1HmEtGyQf26TEn7V6X88mktX9kee9o= -github.com/labstack/gommon v0.3.1/go.mod h1:uW6kP17uPlLJsD3ijUYn3/M5bAxtlZhMI6m3MFxTMTM= github.com/labstack/gommon v0.4.0 h1:y7cvthEAEbU0yHOf4axH8ZG2NH8knB9iNSoTO8dyIk8= github.com/labstack/gommon v0.4.0/go.mod h1:uW6kP17uPlLJsD3ijUYn3/M5bAxtlZhMI6m3MFxTMTM= github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w= @@ -234,6 +223,9 @@ github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRW github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d h1:VhgPp6v9qf9Agr/56bj7Y/xa04UccTW04VP0Qed4vnQ= github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d/go.mod h1:YUTz3bUH2ZwIWBy3CJBeOBEugqcmXREj14T+iG/4k4U= +github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE= +github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE= +github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE= github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= @@ -277,8 +269,6 @@ github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6Mwd github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88= github.com/slack-go/slack v0.11.3 h1:GN7revxEMax4amCc3El9a+9SGnjmBvSUobs0QnO6ZO8= github.com/slack-go/slack v0.11.3/go.mod h1:hlGi5oXA+Gt+yWTPP0plCdRKmjsDxecdHxYQdlMQKOw= -github.com/spf13/cobra v1.5.0 h1:X+jTBEBqF0bHN+9cSMgmfuvv2VHJ9ezmFNf9Y/XstYU= -github.com/spf13/cobra v1.5.0/go.mod h1:dWXEIy2H428czQCjInthrTRUg7yKbok+2Qi/yBIJoUM= github.com/spf13/cobra v1.6.0 h1:42a0n6jwCot1pUmomAp4T7DeMD+20LFv4Q54pxLf2LI= github.com/spf13/cobra v1.6.0/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= @@ -582,8 +572,6 @@ google.golang.org/grpc v1.28.0/go.mod h1:rpkK4SK4GF4Ach/+MFLZUBavHOvF2JJB5uozKKa google.golang.org/grpc v1.29.1/go.mod h1:itym6AZVZYACWQqET3MqgPpjcuV5QH3BxFS3IjizoKk= google.golang.org/grpc v1.30.0/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM8pak= google.golang.org/grpc v1.31.0/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM8pak= -google.golang.org/grpc v1.50.0 h1:fPVVDxY9w++VjTZsYvXWqEf9Rqar/e+9zYfxKK+W+YU= -google.golang.org/grpc v1.50.0/go.mod h1:ZgQEeidpAuNRZ8iRrlBKXZQP1ghovWIVhdJRyCDK+GI= google.golang.org/grpc v1.50.1 h1:DS/BukOZWp8s6p4Dt/tOaJaTQyPyOoCcrjroHuCeLzY= google.golang.org/grpc v1.50.1/go.mod h1:ZgQEeidpAuNRZ8iRrlBKXZQP1ghovWIVhdJRyCDK+GI= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= @@ -607,6 +595,7 @@ gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/pkg/dkwatcher/dkwatcher.go b/pkg/dkwatcher/dkwatcher.go index fac4f4e..d881487 100644 --- a/pkg/dkwatcher/dkwatcher.go +++ b/pkg/dkwatcher/dkwatcher.go @@ -67,7 +67,7 @@ func Run(ctx context.Context, conf Config) error { mr := sharedmem.Reader{UseStorageForDisableAutomation: true} tick := time.NewTicker(syncPeriod) - if err := procedure(ctx, dkClient, mw, mr, conf.NotificationEventSendChan); err != nil { + if err := procedure(ctx, dkClient, mw, mr, conf.NotificationEventSendChan, redisClient); err != nil { return err } for { @@ -111,13 +111,13 @@ func procedure(ctx context.Context, logger.Error(xerrors.Errorf("message: %w", err), "mw.SetTrack was failed") continue } - hasNotify, err := talks.HasNotify(ctx, redisClient) + hasNotify, err := track.Talks.HasNotify(ctx, redisClient, howManyMinutesUntilNotify) if err != nil { logger.Error(xerrors.Errorf("message: %w", err), "talks.HasNotify() was failed") continue } if !hasNotify { - nextTalk, err := talks.GetNextTalk(currentTalk) + currentTalk, err := track.Talks.GetCurrentTalk() if err != nil { logger.Info("currentTalk is none") currentTalk = &model.Talk{} diff --git a/pkg/model/talk.go b/pkg/model/talk.go index 75bcd11..df9029a 100644 --- a/pkg/model/talk.go +++ b/pkg/model/talk.go @@ -6,7 +6,6 @@ import ( "time" "github.com/cloudnativedaysjp/cnd-operation-server/pkg/infrastructure/db" - "github.com/cloudnativedaysjp/cnd-operation-server/pkg/utils" ) type TalkType int32 @@ -27,30 +26,7 @@ const ( type Talks []Talk -func (t Talk) convertTalkType(title string, presentationMethod *string) (TalkType, error) { - switch { - case presentationMethod == nil: - switch title { - case "Opening": - return TalkType_Opening, nil - case "休憩": - return TalkType_Commercial, nil - case "Closing": - return TalkType_Ending, nil - } - case *presentationMethod == "オンライン登壇": - return TalkType_OnlineSession, nil - case *presentationMethod == "事前収録": - return TalkType_RecordingSession, nil - } - return 0, fmt.Errorf("model.convertTalkType not found. title: %s, presentationMethod: %s", title, *presentationMethod) -} - -func (t Talk) GetTalkType(title string, presentationMethod *string) (TalkType, error) { - return t.convertTalkType(title, presentationMethod) -} - -func (ts Talks) isStartNextTalkSoon() bool { +func (ts Talks) isStartNextTalkSoon(untilNotify time.Duration) bool { now := nowFunc() nextTalk, err := ts.GetNextTalk() if err != nil { @@ -59,23 +35,9 @@ func (ts Talks) isStartNextTalkSoon() bool { return nextTalk.StartAt.Sub(now) <= untilNotify } -func (ts Talks) HasNotify(ctx context.Context, rc *db.RedisClient) (bool, error) { - // 次のtalkがもうすぐ始まるか判定し,まだ通知が行われていない場合は通知を行う. - if ts.isStartNextTalkSoon() { - result := rc.Client.Get(ctx, db.NextTalkNotificationKey) - if result.Err() != nil { - return false, result.Err() - } - if result != nil { - return false, nil - } - } - return true, nil -} - -func (ts Talks) HasNotify(ctx context.Context, rc *db.RedisClient) (bool, error) { +func (ts Talks) HasNotify(ctx context.Context, rc *db.RedisClient, untilNotify time.Duration) (bool, error) { // 次のtalkがもうすぐ始まるか判定し,まだ通知が行われていない場合は通知を行う. - if ts.isStartNextTalkSoon() { + if ts.isStartNextTalkSoon(untilNotify) { result := rc.Client.Get(ctx, db.NextTalkNotificationKey) if result.Err() != nil { return false, result.Err() @@ -102,7 +64,7 @@ func (ts Talks) GetCurrentTalk() (*Talk, error) { func (ts Talks) GetNextTalk() (*Talk, error) { if len(ts) == 0 { - return nil, fmt.Errorf("Talks is empty") + return nil, fmt.Errorf("talks is empty") } currentTalk, err := ts.GetCurrentTalk() @@ -119,12 +81,12 @@ func (ts Talks) GetNextTalk() (*Talk, error) { for i, talk := range ts { if talk.Id == currentTalk.Id { if i+1 == len(ts) { - return nil, fmt.Errorf("Current talk is last") + return nil, fmt.Errorf("current talk is last") } return &ts[i+1], nil } } - return nil, fmt.Errorf("Something Wrong") + return nil, fmt.Errorf("something Wrong") } // @@ -143,10 +105,6 @@ type Talk struct { EndAt time.Time } -func (t Talk) GetTalkType(title string, presentationMethod *string) (TalkType, error) { - return t.convertTalkType(title, presentationMethod) -} - func (t Talk) GetTalkTypeName() string { var typeName string switch t.Type { @@ -197,6 +155,10 @@ func (t Talk) convertTalkType(title string, presentationMethod *string) (TalkTyp return 0, fmt.Errorf("model.convertTalkType not found. title: %s, presentationMethod: %s", title, *presentationMethod) } +func (t Talk) GetTalkType(title string, presentationMethod *string) (TalkType, error) { + return t.convertTalkType(title, presentationMethod) +} + // // CurrentAndNextTalk // diff --git a/pkg/model/talk_test.go b/pkg/model/talk_test.go index f4fa4b3..af64d8f 100644 --- a/pkg/model/talk_test.go +++ b/pkg/model/talk_test.go @@ -205,7 +205,7 @@ func TestTalk_HasNotify(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got, err := tt.talks.HasNotify(ctx, rcClient) + got, err := tt.talks.HasNotify(ctx, rcClient, 5*time.Minute) if (err != nil) != tt.wantErr { t.Errorf("Talk.HasNotify() error = %v, wantErr %v", err, tt.wantErr) return @@ -475,7 +475,7 @@ func TestTalk_GetNextTalk(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got, err := tt.talks.GetNextTalk(tt.args) + got, err := tt.talks.GetNextTalk() if (err == nil && tt.wantErr) || (err != nil && !tt.wantErr) { t.Errorf("Talk.GetTalkType() wantErr %v", tt.wantErr) return From 53bfe8ff07b966483806ca3a8ca6d3d0c9646dc2 Mon Sep 17 00:00:00 2001 From: tkc66 Date: Sat, 15 Oct 2022 23:54:18 +0900 Subject: [PATCH 07/18] fix: linter --- pkg/model/talk_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/model/talk_test.go b/pkg/model/talk_test.go index af64d8f..3e97f23 100644 --- a/pkg/model/talk_test.go +++ b/pkg/model/talk_test.go @@ -96,7 +96,6 @@ func TestTalk_HasNotify(t *testing.T) { ctx := context.Background() rcClient, err := db.NewRedisClient("http://localhost:6379") if err != nil { - fmt.Errorf("message: %w", err) return } tests := []struct { From 9b5c68f8cd068e658ae72acbf68ecea4e6784cce Mon Sep 17 00:00:00 2001 From: tkc66 Date: Sat, 15 Oct 2022 23:57:21 +0900 Subject: [PATCH 08/18] fix: use const --- pkg/model/talk_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/model/talk_test.go b/pkg/model/talk_test.go index 3e97f23..0859d14 100644 --- a/pkg/model/talk_test.go +++ b/pkg/model/talk_test.go @@ -204,7 +204,7 @@ func TestTalk_HasNotify(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got, err := tt.talks.HasNotify(ctx, rcClient, 5*time.Minute) + got, err := tt.talks.HasNotify(ctx, rcClient, test_howManyMinutesUntilNotify) if (err != nil) != tt.wantErr { t.Errorf("Talk.HasNotify() error = %v, wantErr %v", err, tt.wantErr) return From 4fa914b3bd47802bbe9da0c698e44683a8df49e6 Mon Sep 17 00:00:00 2001 From: tkc66 Date: Sun, 16 Oct 2022 00:41:48 +0900 Subject: [PATCH 09/18] =?UTF-8?q?fix:=20=E5=BF=85=E8=A6=81=E3=81=AE?= =?UTF-8?q?=E6=99=82=E3=81=AE=E3=81=BFnotifyer=E3=81=AB=E9=80=81=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/dkwatcher/dkwatcher.go | 25 ++++++++++++++----------- pkg/infrastructure/db/redis.go | 6 +++--- pkg/model/talk.go | 17 +++++++---------- pkg/notifier/notifier.go | 2 +- 4 files changed, 25 insertions(+), 25 deletions(-) diff --git a/pkg/dkwatcher/dkwatcher.go b/pkg/dkwatcher/dkwatcher.go index d881487..aea1cc7 100644 --- a/pkg/dkwatcher/dkwatcher.go +++ b/pkg/dkwatcher/dkwatcher.go @@ -98,7 +98,6 @@ func procedure(ctx context.Context, } for _, track := range tracks { logger := rootLogger.WithValues("trackId", track.Id) - if disabled, err := mr.DisableAutomation(track.Id); err != nil { logger.Error(xerrors.Errorf("message: %w", err), "mr.DisableAutomation() was failed") return nil @@ -111,22 +110,26 @@ func procedure(ctx context.Context, logger.Error(xerrors.Errorf("message: %w", err), "mw.SetTrack was failed") continue } + currentTalk, err := track.Talks.GetCurrentTalk() + if err != nil { + logger.Info("currentTalk is none") + continue + } + nextTalk, err := track.Talks.GetNextTalk() + if err != nil { + logger.Info("nextTalk is none") + continue + } + if !track.Talks.IsStartNextTalkSoon(howManyMinutesUntilNotify) { + logger.Info("nextTalk is not start soon. trackNo:%s", track.Id) + continue + } hasNotify, err := track.Talks.HasNotify(ctx, redisClient, howManyMinutesUntilNotify) if err != nil { logger.Error(xerrors.Errorf("message: %w", err), "talks.HasNotify() was failed") continue } if !hasNotify { - currentTalk, err := track.Talks.GetCurrentTalk() - if err != nil { - logger.Info("currentTalk is none") - currentTalk = &model.Talk{} - } - nextTalk, err := track.Talks.GetNextTalk() - if err != nil { - logger.Info("nextTalk is none") - nextTalk = &model.Talk{} - } notificationEventSendChan <- model.CurrentAndNextTalk{ Current: *currentTalk, Next: *nextTalk} } diff --git a/pkg/infrastructure/db/redis.go b/pkg/infrastructure/db/redis.go index 16eb597..ccb4c0d 100644 --- a/pkg/infrastructure/db/redis.go +++ b/pkg/infrastructure/db/redis.go @@ -13,9 +13,9 @@ type RedisClient struct { } const ( - RedisExpiration = 10 * time.Minute - NextTalkNotificationKey = "nextTalkNotificationAlreadySentFlag" - NextTalkNotificationAlreadySentFlag = true + RedisExpiration = 10 * time.Minute + NextTalkNotificationKey = "nextTalkNotificationAlreadySentFlag" + NextTalkNotificationAlreadySent = true ) func NewRedisClient(addr string) (*RedisClient, error) { diff --git a/pkg/model/talk.go b/pkg/model/talk.go index df9029a..cffe6d8 100644 --- a/pkg/model/talk.go +++ b/pkg/model/talk.go @@ -26,7 +26,7 @@ const ( type Talks []Talk -func (ts Talks) isStartNextTalkSoon(untilNotify time.Duration) bool { +func (ts Talks) IsStartNextTalkSoon(untilNotify time.Duration) bool { now := nowFunc() nextTalk, err := ts.GetNextTalk() if err != nil { @@ -36,15 +36,12 @@ func (ts Talks) isStartNextTalkSoon(untilNotify time.Duration) bool { } func (ts Talks) HasNotify(ctx context.Context, rc *db.RedisClient, untilNotify time.Duration) (bool, error) { - // 次のtalkがもうすぐ始まるか判定し,まだ通知が行われていない場合は通知を行う. - if ts.isStartNextTalkSoon(untilNotify) { - result := rc.Client.Get(ctx, db.NextTalkNotificationKey) - if result.Err() != nil { - return false, result.Err() - } - if result != nil { - return false, nil - } + result := rc.Client.Get(ctx, db.NextTalkNotificationKey) + if result.Err() != nil { + return false, result.Err() + } + if result != nil { + return false, nil } return true, nil } diff --git a/pkg/notifier/notifier.go b/pkg/notifier/notifier.go index d03bc59..f0daa42 100644 --- a/pkg/notifier/notifier.go +++ b/pkg/notifier/notifier.go @@ -69,7 +69,7 @@ func Run(ctx context.Context, conf Config) error { if err := c.Receive(talk); err != nil { logger.Error(xerrors.Errorf("message: %w", err), "notification failed") } - if result := redisClient.Client.Set(ctx, db.NextTalkNotificationKey, db.NextTalkNotificationAlreadySentFlag, db.RedisExpiration); result.Err() != nil { + if result := redisClient.Client.Set(ctx, db.NextTalkNotificationKey, db.NextTalkNotificationAlreadySent, db.RedisExpiration); result.Err() != nil { return xerrors.Errorf("message: %w", result.Err()) } } From 0daa5fcd7bd77aeba41a3cf73367c0c4e644177a Mon Sep 17 00:00:00 2001 From: tkc66 Date: Sat, 22 Oct 2022 08:50:14 +0900 Subject: [PATCH 10/18] fix: rename package --- pkg/dkwatcher/dkwatcher.go | 6 +++--- pkg/{infrastructure/db => infra/infra}/redis.go | 2 +- pkg/model/talk.go | 6 +++--- pkg/model/talk_test.go | 6 +++--- pkg/notifier/notifier.go | 6 +++--- 5 files changed, 13 insertions(+), 13 deletions(-) rename pkg/{infrastructure/db => infra/infra}/redis.go (97%) diff --git a/pkg/dkwatcher/dkwatcher.go b/pkg/dkwatcher/dkwatcher.go index aea1cc7..7e75d62 100644 --- a/pkg/dkwatcher/dkwatcher.go +++ b/pkg/dkwatcher/dkwatcher.go @@ -10,8 +10,8 @@ import ( "golang.org/x/xerrors" "github.com/cloudnativedaysjp/cnd-operation-server/pkg/infra/dreamkast" + "github.com/cloudnativedaysjp/cnd-operation-server/pkg/infra/infra" "github.com/cloudnativedaysjp/cnd-operation-server/pkg/infra/sharedmem" - "github.com/cloudnativedaysjp/cnd-operation-server/pkg/infrastructure/db" "github.com/cloudnativedaysjp/cnd-operation-server/pkg/metrics" "github.com/cloudnativedaysjp/cnd-operation-server/pkg/model" "github.com/cloudnativedaysjp/cnd-operation-server/pkg/utils" @@ -58,7 +58,7 @@ func Run(ctx context.Context, conf Config) error { return err } - redisClient, err := db.NewRedisClient(conf.RedisHost) + redisClient, err := infra.NewRedisClient(conf.RedisHost) if err != nil { return err } @@ -87,7 +87,7 @@ func Run(ctx context.Context, conf Config) error { func procedure(ctx context.Context, dkClient dreamkast.Client, mw sharedmem.WriterIface, mr sharedmem.ReaderIface, - notificationEventSendChan chan<- model.CurrentAndNextTalk, redisClient *db.RedisClient, + notificationEventSendChan chan<- model.CurrentAndNextTalk, redisClient *infra.RedisClient, ) error { rootLogger := utils.GetLogger(ctx) diff --git a/pkg/infrastructure/db/redis.go b/pkg/infra/infra/redis.go similarity index 97% rename from pkg/infrastructure/db/redis.go rename to pkg/infra/infra/redis.go index ccb4c0d..1dcf264 100644 --- a/pkg/infrastructure/db/redis.go +++ b/pkg/infra/infra/redis.go @@ -1,4 +1,4 @@ -package db +package infra import ( "context" diff --git a/pkg/model/talk.go b/pkg/model/talk.go index cffe6d8..174777e 100644 --- a/pkg/model/talk.go +++ b/pkg/model/talk.go @@ -5,7 +5,7 @@ import ( "fmt" "time" - "github.com/cloudnativedaysjp/cnd-operation-server/pkg/infrastructure/db" + "github.com/cloudnativedaysjp/cnd-operation-server/pkg/infra/infra" ) type TalkType int32 @@ -35,8 +35,8 @@ func (ts Talks) IsStartNextTalkSoon(untilNotify time.Duration) bool { return nextTalk.StartAt.Sub(now) <= untilNotify } -func (ts Talks) HasNotify(ctx context.Context, rc *db.RedisClient, untilNotify time.Duration) (bool, error) { - result := rc.Client.Get(ctx, db.NextTalkNotificationKey) +func (ts Talks) HasNotify(ctx context.Context, rc *infra.RedisClient, untilNotify time.Duration) (bool, error) { + result := rc.Client.Get(ctx, infra.NextTalkNotificationKey) if result.Err() != nil { return false, result.Err() } diff --git a/pkg/model/talk_test.go b/pkg/model/talk_test.go index 0859d14..88d5bc5 100644 --- a/pkg/model/talk_test.go +++ b/pkg/model/talk_test.go @@ -6,7 +6,7 @@ import ( "testing" "time" - "github.com/cloudnativedaysjp/cnd-operation-server/pkg/infrastructure/db" + "github.com/cloudnativedaysjp/cnd-operation-server/pkg/infra/infra" "github.com/cloudnativedaysjp/cnd-operation-server/pkg/testutils" ) @@ -94,7 +94,7 @@ func TestTalk_HasNotify(t *testing.T) { return time.Date(2022, 10, 01, 12, 27, 00, 0, time.UTC) } ctx := context.Background() - rcClient, err := db.NewRedisClient("http://localhost:6379") + rcClient, err := infra.NewRedisClient("http://localhost:6379") if err != nil { return } @@ -214,7 +214,7 @@ func TestTalk_HasNotify(t *testing.T) { return } if got { - cmd := rcClient.Client.Get(ctx, db.NextTalkNotificationKey) + cmd := rcClient.Client.Get(ctx, infra.NextTalkNotificationKey) fmt.Println(cmd.Bool()) } }) diff --git a/pkg/notifier/notifier.go b/pkg/notifier/notifier.go index f0daa42..ea00dc9 100644 --- a/pkg/notifier/notifier.go +++ b/pkg/notifier/notifier.go @@ -8,8 +8,8 @@ import ( "go.uber.org/zap" "golang.org/x/xerrors" + "github.com/cloudnativedaysjp/cnd-operation-server/pkg/infra/infra" "github.com/cloudnativedaysjp/cnd-operation-server/pkg/infra/slack" - "github.com/cloudnativedaysjp/cnd-operation-server/pkg/infrastructure/db" "github.com/cloudnativedaysjp/cnd-operation-server/pkg/model" ) @@ -46,7 +46,7 @@ func Run(ctx context.Context, conf Config) error { slackClients := make(map[int32]slack.Client) channelIds := make(map[int32]string) - redisClient, err := db.NewRedisClient(conf.RedisHost) + redisClient, err := infra.NewRedisClient(conf.RedisHost) if err != nil { return err } @@ -69,7 +69,7 @@ func Run(ctx context.Context, conf Config) error { if err := c.Receive(talk); err != nil { logger.Error(xerrors.Errorf("message: %w", err), "notification failed") } - if result := redisClient.Client.Set(ctx, db.NextTalkNotificationKey, db.NextTalkNotificationAlreadySent, db.RedisExpiration); result.Err() != nil { + if result := redisClient.Client.Set(ctx, infra.NextTalkNotificationKey, infra.NextTalkNotificationAlreadySent, infra.RedisExpiration); result.Err() != nil { return xerrors.Errorf("message: %w", result.Err()) } } From 4019ef7a60f36f5bc24a8d33088b6126ebc7ee68 Mon Sep 17 00:00:00 2001 From: tkc66 Date: Sat, 22 Oct 2022 09:03:30 +0900 Subject: [PATCH 11/18] =?UTF-8?q?fix:=20=E4=B8=80=E7=95=AA=E6=9C=80?= =?UTF-8?q?=E5=88=9D=E3=81=AE=E3=82=BB=E3=83=83=E3=82=B7=E3=83=A7=E3=83=B3?= =?UTF-8?q?=E3=81=8C=E5=A7=8B=E3=81=BE=E3=81=A3=E3=81=A6=E3=81=84=E3=81=AA?= =?UTF-8?q?=E3=81=84=E3=81=93=E3=81=A8=E3=82=92=E8=80=83=E6=85=AE=E3=81=AB?= =?UTF-8?q?=E5=85=A5=E3=82=8C=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/dkwatcher/dkwatcher.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/dkwatcher/dkwatcher.go b/pkg/dkwatcher/dkwatcher.go index 7e75d62..35fc72e 100644 --- a/pkg/dkwatcher/dkwatcher.go +++ b/pkg/dkwatcher/dkwatcher.go @@ -113,7 +113,7 @@ func procedure(ctx context.Context, currentTalk, err := track.Talks.GetCurrentTalk() if err != nil { logger.Info("currentTalk is none") - continue + currentTalk = &model.Talk{} } nextTalk, err := track.Talks.GetNextTalk() if err != nil { From 49553b7f50d6f48ecc378881e6ff2df3d8ce724f Mon Sep 17 00:00:00 2001 From: tkc66 Date: Sun, 23 Oct 2022 13:16:15 +0900 Subject: [PATCH 12/18] =?UTF-8?q?fix:=20review=20=E3=82=B3=E3=83=A1?= =?UTF-8?q?=E3=83=B3=E3=83=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/dkwatcher/dkwatcher.go | 17 +- pkg/infra/{infra => db}/redis.go | 13 +- pkg/model/talk.go | 14 -- pkg/model/talk_test.go | 265 +++++++++++++++---------------- pkg/notifier/notifier.go | 10 +- 5 files changed, 157 insertions(+), 162 deletions(-) rename pkg/infra/{infra => db}/redis.go (61%) diff --git a/pkg/dkwatcher/dkwatcher.go b/pkg/dkwatcher/dkwatcher.go index 35fc72e..059bdd1 100644 --- a/pkg/dkwatcher/dkwatcher.go +++ b/pkg/dkwatcher/dkwatcher.go @@ -9,8 +9,8 @@ import ( "go.uber.org/zap" "golang.org/x/xerrors" + infra "github.com/cloudnativedaysjp/cnd-operation-server/pkg/infra/db" "github.com/cloudnativedaysjp/cnd-operation-server/pkg/infra/dreamkast" - "github.com/cloudnativedaysjp/cnd-operation-server/pkg/infra/infra" "github.com/cloudnativedaysjp/cnd-operation-server/pkg/infra/sharedmem" "github.com/cloudnativedaysjp/cnd-operation-server/pkg/metrics" "github.com/cloudnativedaysjp/cnd-operation-server/pkg/model" @@ -105,7 +105,6 @@ func procedure(ctx context.Context, logger.Info("DisableAutomation was true, skipped") continue } - if err := mw.SetTrack(track); err != nil { logger.Error(xerrors.Errorf("message: %w", err), "mw.SetTrack was failed") continue @@ -124,15 +123,17 @@ func procedure(ctx context.Context, logger.Info("nextTalk is not start soon. trackNo:%s", track.Id) continue } - hasNotify, err := track.Talks.HasNotify(ctx, redisClient, howManyMinutesUntilNotify) + val, err := redisClient.GetNextTalkNotification(ctx, int(nextTalk.Id)) if err != nil { - logger.Error(xerrors.Errorf("message: %w", err), "talks.HasNotify() was failed") - continue + logger.Error(xerrors.Errorf("message: %w", err), "db.GetNextTalkNotification() was failed") + return err } - if !hasNotify { - notificationEventSendChan <- model.CurrentAndNextTalk{ - Current: *currentTalk, Next: *nextTalk} + if val != "" { + logger.Info("nextTalkNotification already sent . trackNo:%s", track.Id) + continue } + notificationEventSendChan <- model.CurrentAndNextTalk{ + Current: *currentTalk, Next: *nextTalk} } return nil } diff --git a/pkg/infra/infra/redis.go b/pkg/infra/db/redis.go similarity index 61% rename from pkg/infra/infra/redis.go rename to pkg/infra/db/redis.go index 1dcf264..87529a1 100644 --- a/pkg/infra/infra/redis.go +++ b/pkg/infra/db/redis.go @@ -1,7 +1,8 @@ -package infra +package db import ( "context" + "strconv" "time" "github.com/go-redis/redis/v8" @@ -14,7 +15,7 @@ type RedisClient struct { const ( RedisExpiration = 10 * time.Minute - NextTalkNotificationKey = "nextTalkNotificationAlreadySentFlag" + NextTalkNotificationKey = "nextTalkNotificationAlreadySentFlag:" NextTalkNotificationAlreadySent = true ) @@ -31,3 +32,11 @@ func NewRedisClient(addr string) (*RedisClient, error) { Client: client, }, nil } + +func (rc *RedisClient) SetNextTalkNotification(ctx context.Context, id int) error { + return rc.Client.Set(ctx, NextTalkNotificationKey, NextTalkNotificationAlreadySent, RedisExpiration).Err() +} + +func (rc *RedisClient) GetNextTalkNotification(ctx context.Context, id int) (string, error) { + return rc.Client.Get(ctx, NextTalkNotificationKey+strconv.Itoa(int(id))).Result() +} diff --git a/pkg/model/talk.go b/pkg/model/talk.go index 174777e..d2a3ad5 100644 --- a/pkg/model/talk.go +++ b/pkg/model/talk.go @@ -1,11 +1,8 @@ package model import ( - "context" "fmt" "time" - - "github.com/cloudnativedaysjp/cnd-operation-server/pkg/infra/infra" ) type TalkType int32 @@ -35,17 +32,6 @@ func (ts Talks) IsStartNextTalkSoon(untilNotify time.Duration) bool { return nextTalk.StartAt.Sub(now) <= untilNotify } -func (ts Talks) HasNotify(ctx context.Context, rc *infra.RedisClient, untilNotify time.Duration) (bool, error) { - result := rc.Client.Get(ctx, infra.NextTalkNotificationKey) - if result.Err() != nil { - return false, result.Err() - } - if result != nil { - return false, nil - } - return true, nil -} - func (ts Talks) GetCurrentTalk() (*Talk, error) { if len(ts) == 0 { return nil, fmt.Errorf("Talks is empty") diff --git a/pkg/model/talk_test.go b/pkg/model/talk_test.go index 88d5bc5..a487e60 100644 --- a/pkg/model/talk_test.go +++ b/pkg/model/talk_test.go @@ -1,12 +1,9 @@ package model import ( - "context" - "fmt" "testing" "time" - "github.com/cloudnativedaysjp/cnd-operation-server/pkg/infra/infra" "github.com/cloudnativedaysjp/cnd-operation-server/pkg/testutils" ) @@ -89,137 +86,137 @@ func setTestTime(v string) time.Time { return pt } -func TestTalk_HasNotify(t *testing.T) { - nowFunc = func() time.Time { - return time.Date(2022, 10, 01, 12, 27, 00, 0, time.UTC) - } - ctx := context.Background() - rcClient, err := infra.NewRedisClient("http://localhost:6379") - if err != nil { - return - } - tests := []struct { - name string - talks Talks - want bool - wantErr bool - }{ - { - name: "no talk", - talks: Talks{}, - want: false, - wantErr: true, - }, - { - name: "not found next talk", - talks: Talks{ - Talk{ - Id: 1, - TalkName: "talk1", - TrackId: 1, - TrackName: "track1", - EventAbbr: "test event", - SpeakerNames: []string{"speakerA", "speaker"}, - Type: 1, - StartAt: setTestTime("2022-10-01T11:00:00"), - EndAt: setTestTime("2022-10-01T12:00:00"), - }, - Talk{ - Id: 2, - TalkName: "talk2", - TrackId: 1, - TrackName: "track1", - EventAbbr: "test event", - SpeakerNames: []string{"speakerA", "speaker"}, - Type: 1, - StartAt: setTestTime("2022-10-01T12:00:00"), - EndAt: setTestTime("2022-10-01T13:00:00"), - }, - Talk{ - Id: 3, - TalkName: "talk3", - TrackId: 1, - TrackName: "track1", - EventAbbr: "test event", - SpeakerNames: []string{"speakerA", "speaker"}, - Type: 1, - StartAt: setTestTime("2022-10-01T13:00:00"), - EndAt: setTestTime("2022-10-01T14:00:00"), - }, - }, - want: false, - wantErr: true, - }, - { - name: "found next talk", - talks: Talks{ - Talk{ - Id: 1, - TalkName: "talk1", - TrackId: 1, - TrackName: "track1", - EventAbbr: "test event", - SpeakerNames: []string{"speakerA", "speaker"}, - Type: 1, - StartAt: setTestTime("2022-10-01T11:00:00"), - EndAt: setTestTime("2022-10-01T11:30:00"), - }, - Talk{ - Id: 2, - TalkName: "talk2", - TrackId: 1, - TrackName: "track1", - EventAbbr: "test event", - SpeakerNames: []string{"speakerA", "speaker"}, - Type: 1, - StartAt: setTestTime("2022-10-01T11:30:00"), - EndAt: setTestTime("2022-10-01T12:00:00"), - }, - Talk{ - Id: 3, - TalkName: "talk3", - TrackId: 1, - TrackName: "track1", - EventAbbr: "test event", - SpeakerNames: []string{"speakerA", "speaker"}, - Type: 1, - StartAt: setTestTime("2022-10-01T12:00:00"), - EndAt: setTestTime("2022-10-01T12:30:00"), - }, - Talk{ - Id: 4, - TalkName: "talk4", - TrackId: 1, - TrackName: "track1", - EventAbbr: "test event", - SpeakerNames: []string{"speakerA", "speaker"}, - Type: 1, - StartAt: setTestTime("2022-10-01T12:30:00"), - EndAt: setTestTime("2022-10-01T13:00:00"), - }, - }, - want: true, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := tt.talks.HasNotify(ctx, rcClient, test_howManyMinutesUntilNotify) - if (err != nil) != tt.wantErr { - t.Errorf("Talk.HasNotify() error = %v, wantErr %v", err, tt.wantErr) - return - } - if got != tt.want { - t.Errorf("Talk.HasNotify() = %v, want %v", got, tt.want) - return - } - if got { - cmd := rcClient.Client.Get(ctx, infra.NextTalkNotificationKey) - fmt.Println(cmd.Bool()) - } - }) - } -} +// func TestTalk_HasNotify(t *testing.T) { +// nowFunc = func() time.Time { +// return time.Date(2022, 10, 01, 12, 27, 00, 0, time.UTC) +// } +// ctx := context.Background() +// rcClient, err := db.NewRedisClient("http://localhost:6379") +// if err != nil { +// return +// } +// tests := []struct { +// name string +// talks Talks +// want bool +// wantErr bool +// }{ +// { +// name: "no talk", +// talks: Talks{}, +// want: false, +// wantErr: true, +// }, +// { +// name: "not found next talk", +// talks: Talks{ +// Talk{ +// Id: 1, +// TalkName: "talk1", +// TrackId: 1, +// TrackName: "track1", +// EventAbbr: "test event", +// SpeakerNames: []string{"speakerA", "speaker"}, +// Type: 1, +// StartAt: setTestTime("2022-10-01T11:00:00"), +// EndAt: setTestTime("2022-10-01T12:00:00"), +// }, +// Talk{ +// Id: 2, +// TalkName: "talk2", +// TrackId: 1, +// TrackName: "track1", +// EventAbbr: "test event", +// SpeakerNames: []string{"speakerA", "speaker"}, +// Type: 1, +// StartAt: setTestTime("2022-10-01T12:00:00"), +// EndAt: setTestTime("2022-10-01T13:00:00"), +// }, +// Talk{ +// Id: 3, +// TalkName: "talk3", +// TrackId: 1, +// TrackName: "track1", +// EventAbbr: "test event", +// SpeakerNames: []string{"speakerA", "speaker"}, +// Type: 1, +// StartAt: setTestTime("2022-10-01T13:00:00"), +// EndAt: setTestTime("2022-10-01T14:00:00"), +// }, +// }, +// want: false, +// wantErr: true, +// }, +// { +// name: "found next talk", +// talks: Talks{ +// Talk{ +// Id: 1, +// TalkName: "talk1", +// TrackId: 1, +// TrackName: "track1", +// EventAbbr: "test event", +// SpeakerNames: []string{"speakerA", "speaker"}, +// Type: 1, +// StartAt: setTestTime("2022-10-01T11:00:00"), +// EndAt: setTestTime("2022-10-01T11:30:00"), +// }, +// Talk{ +// Id: 2, +// TalkName: "talk2", +// TrackId: 1, +// TrackName: "track1", +// EventAbbr: "test event", +// SpeakerNames: []string{"speakerA", "speaker"}, +// Type: 1, +// StartAt: setTestTime("2022-10-01T11:30:00"), +// EndAt: setTestTime("2022-10-01T12:00:00"), +// }, +// Talk{ +// Id: 3, +// TalkName: "talk3", +// TrackId: 1, +// TrackName: "track1", +// EventAbbr: "test event", +// SpeakerNames: []string{"speakerA", "speaker"}, +// Type: 1, +// StartAt: setTestTime("2022-10-01T12:00:00"), +// EndAt: setTestTime("2022-10-01T12:30:00"), +// }, +// Talk{ +// Id: 4, +// TalkName: "talk4", +// TrackId: 1, +// TrackName: "track1", +// EventAbbr: "test event", +// SpeakerNames: []string{"speakerA", "speaker"}, +// Type: 1, +// StartAt: setTestTime("2022-10-01T12:30:00"), +// EndAt: setTestTime("2022-10-01T13:00:00"), +// }, +// }, +// want: true, +// wantErr: false, +// }, +// } +// for _, tt := range tests { +// t.Run(tt.name, func(t *testing.T) { +// got, err := tt.talks.HasNotify(ctx, rcClient, test_howManyMinutesUntilNotify) +// if (err != nil) != tt.wantErr { +// t.Errorf("Talk.HasNotify() error = %v, wantErr %v", err, tt.wantErr) +// return +// } +// if got != tt.want { +// t.Errorf("Talk.HasNotify() = %v, want %v", got, tt.want) +// return +// } +// if got { +// cmd := rcClient.Client.Get(ctx, infra.NextTalkNotificationKey) +// fmt.Println(cmd.Bool()) +// } +// }) +// } +// } func TestTalk_GetCurrentTalk(t *testing.T) { nowFunc = func() time.Time { diff --git a/pkg/notifier/notifier.go b/pkg/notifier/notifier.go index ea00dc9..1eb44a8 100644 --- a/pkg/notifier/notifier.go +++ b/pkg/notifier/notifier.go @@ -8,7 +8,7 @@ import ( "go.uber.org/zap" "golang.org/x/xerrors" - "github.com/cloudnativedaysjp/cnd-operation-server/pkg/infra/infra" + "github.com/cloudnativedaysjp/cnd-operation-server/pkg/infra/db" "github.com/cloudnativedaysjp/cnd-operation-server/pkg/infra/slack" "github.com/cloudnativedaysjp/cnd-operation-server/pkg/model" ) @@ -46,7 +46,7 @@ func Run(ctx context.Context, conf Config) error { slackClients := make(map[int32]slack.Client) channelIds := make(map[int32]string) - redisClient, err := infra.NewRedisClient(conf.RedisHost) + redisClient, err := db.NewRedisClient(conf.RedisHost) if err != nil { return err } @@ -68,9 +68,11 @@ func Run(ctx context.Context, conf Config) error { case talk := <-conf.NotificationEventReceiveChan: if err := c.Receive(talk); err != nil { logger.Error(xerrors.Errorf("message: %w", err), "notification failed") + return nil } - if result := redisClient.Client.Set(ctx, infra.NextTalkNotificationKey, infra.NextTalkNotificationAlreadySent, infra.RedisExpiration); result.Err() != nil { - return xerrors.Errorf("message: %w", result.Err()) + if err := redisClient.SetNextTalkNotification(ctx, int(talk.Next.Id)); err != nil { + logger.Error(xerrors.Errorf("message: %w", err), "set value to redis failed") + return nil } } } From 816034ea54697c2d6853bdccb95f24a812f6d95c Mon Sep 17 00:00:00 2001 From: tkc66 Date: Sun, 23 Oct 2022 13:28:04 +0900 Subject: [PATCH 13/18] fix: test --- pkg/model/talk_test.go | 245 +++++++++++++++++++---------------------- 1 file changed, 114 insertions(+), 131 deletions(-) diff --git a/pkg/model/talk_test.go b/pkg/model/talk_test.go index a487e60..53c0948 100644 --- a/pkg/model/talk_test.go +++ b/pkg/model/talk_test.go @@ -86,137 +86,120 @@ func setTestTime(v string) time.Time { return pt } -// func TestTalk_HasNotify(t *testing.T) { -// nowFunc = func() time.Time { -// return time.Date(2022, 10, 01, 12, 27, 00, 0, time.UTC) -// } -// ctx := context.Background() -// rcClient, err := db.NewRedisClient("http://localhost:6379") -// if err != nil { -// return -// } -// tests := []struct { -// name string -// talks Talks -// want bool -// wantErr bool -// }{ -// { -// name: "no talk", -// talks: Talks{}, -// want: false, -// wantErr: true, -// }, -// { -// name: "not found next talk", -// talks: Talks{ -// Talk{ -// Id: 1, -// TalkName: "talk1", -// TrackId: 1, -// TrackName: "track1", -// EventAbbr: "test event", -// SpeakerNames: []string{"speakerA", "speaker"}, -// Type: 1, -// StartAt: setTestTime("2022-10-01T11:00:00"), -// EndAt: setTestTime("2022-10-01T12:00:00"), -// }, -// Talk{ -// Id: 2, -// TalkName: "talk2", -// TrackId: 1, -// TrackName: "track1", -// EventAbbr: "test event", -// SpeakerNames: []string{"speakerA", "speaker"}, -// Type: 1, -// StartAt: setTestTime("2022-10-01T12:00:00"), -// EndAt: setTestTime("2022-10-01T13:00:00"), -// }, -// Talk{ -// Id: 3, -// TalkName: "talk3", -// TrackId: 1, -// TrackName: "track1", -// EventAbbr: "test event", -// SpeakerNames: []string{"speakerA", "speaker"}, -// Type: 1, -// StartAt: setTestTime("2022-10-01T13:00:00"), -// EndAt: setTestTime("2022-10-01T14:00:00"), -// }, -// }, -// want: false, -// wantErr: true, -// }, -// { -// name: "found next talk", -// talks: Talks{ -// Talk{ -// Id: 1, -// TalkName: "talk1", -// TrackId: 1, -// TrackName: "track1", -// EventAbbr: "test event", -// SpeakerNames: []string{"speakerA", "speaker"}, -// Type: 1, -// StartAt: setTestTime("2022-10-01T11:00:00"), -// EndAt: setTestTime("2022-10-01T11:30:00"), -// }, -// Talk{ -// Id: 2, -// TalkName: "talk2", -// TrackId: 1, -// TrackName: "track1", -// EventAbbr: "test event", -// SpeakerNames: []string{"speakerA", "speaker"}, -// Type: 1, -// StartAt: setTestTime("2022-10-01T11:30:00"), -// EndAt: setTestTime("2022-10-01T12:00:00"), -// }, -// Talk{ -// Id: 3, -// TalkName: "talk3", -// TrackId: 1, -// TrackName: "track1", -// EventAbbr: "test event", -// SpeakerNames: []string{"speakerA", "speaker"}, -// Type: 1, -// StartAt: setTestTime("2022-10-01T12:00:00"), -// EndAt: setTestTime("2022-10-01T12:30:00"), -// }, -// Talk{ -// Id: 4, -// TalkName: "talk4", -// TrackId: 1, -// TrackName: "track1", -// EventAbbr: "test event", -// SpeakerNames: []string{"speakerA", "speaker"}, -// Type: 1, -// StartAt: setTestTime("2022-10-01T12:30:00"), -// EndAt: setTestTime("2022-10-01T13:00:00"), -// }, -// }, -// want: true, -// wantErr: false, -// }, -// } -// for _, tt := range tests { -// t.Run(tt.name, func(t *testing.T) { -// got, err := tt.talks.HasNotify(ctx, rcClient, test_howManyMinutesUntilNotify) -// if (err != nil) != tt.wantErr { -// t.Errorf("Talk.HasNotify() error = %v, wantErr %v", err, tt.wantErr) -// return -// } -// if got != tt.want { -// t.Errorf("Talk.HasNotify() = %v, want %v", got, tt.want) -// return -// } -// if got { -// cmd := rcClient.Client.Get(ctx, infra.NextTalkNotificationKey) -// fmt.Println(cmd.Bool()) -// } -// }) -// } -// } +func TestTalk_IsStartNextTalkSoon(t *testing.T) { + nowFunc = func() time.Time { + return time.Date(2022, 10, 01, 12, 27, 00, 0, time.UTC) + } + tests := []struct { + name string + talks Talks + want bool + }{ + { + name: "no talk", + talks: Talks{}, + want: false, + }, + { + name: "not found next talk", + talks: Talks{ + Talk{ + Id: 1, + TalkName: "talk1", + TrackId: 1, + TrackName: "track1", + EventAbbr: "test event", + SpeakerNames: []string{"speakerA", "speaker"}, + Type: 1, + StartAt: setTestTime("2022-10-01T11:00:00"), + EndAt: setTestTime("2022-10-01T12:00:00"), + }, + Talk{ + Id: 2, + TalkName: "talk2", + TrackId: 1, + TrackName: "track1", + EventAbbr: "test event", + SpeakerNames: []string{"speakerA", "speaker"}, + Type: 1, + StartAt: setTestTime("2022-10-01T12:00:00"), + EndAt: setTestTime("2022-10-01T13:00:00"), + }, + Talk{ + Id: 3, + TalkName: "talk3", + TrackId: 1, + TrackName: "track1", + EventAbbr: "test event", + SpeakerNames: []string{"speakerA", "speaker"}, + Type: 1, + StartAt: setTestTime("2022-10-01T13:00:00"), + EndAt: setTestTime("2022-10-01T14:00:00"), + }, + }, + want: false, + }, + { + name: "found next talk", + talks: Talks{ + Talk{ + Id: 1, + TalkName: "talk1", + TrackId: 1, + TrackName: "track1", + EventAbbr: "test event", + SpeakerNames: []string{"speakerA", "speaker"}, + Type: 1, + StartAt: setTestTime("2022-10-01T11:00:00"), + EndAt: setTestTime("2022-10-01T11:30:00"), + }, + Talk{ + Id: 2, + TalkName: "talk2", + TrackId: 1, + TrackName: "track1", + EventAbbr: "test event", + SpeakerNames: []string{"speakerA", "speaker"}, + Type: 1, + StartAt: setTestTime("2022-10-01T11:30:00"), + EndAt: setTestTime("2022-10-01T12:00:00"), + }, + Talk{ + Id: 3, + TalkName: "talk3", + TrackId: 1, + TrackName: "track1", + EventAbbr: "test event", + SpeakerNames: []string{"speakerA", "speaker"}, + Type: 1, + StartAt: setTestTime("2022-10-01T12:00:00"), + EndAt: setTestTime("2022-10-01T12:30:00"), + }, + Talk{ + Id: 4, + TalkName: "talk4", + TrackId: 1, + TrackName: "track1", + EventAbbr: "test event", + SpeakerNames: []string{"speakerA", "speaker"}, + Type: 1, + StartAt: setTestTime("2022-10-01T12:30:00"), + EndAt: setTestTime("2022-10-01T13:00:00"), + }, + }, + want: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := tt.talks.IsStartNextTalkSoon(test_howManyMinutesUntilNotify) + if got != tt.want { + t.Errorf("Talk.HasNotify() = %v, want %v", got, tt.want) + return + } + }) + } +} func TestTalk_GetCurrentTalk(t *testing.T) { nowFunc = func() time.Time { From a44759e9ff65d4052210a792df91bf2999a2ba4e Mon Sep 17 00:00:00 2001 From: tkc66 Date: Sun, 23 Oct 2022 14:41:57 +0900 Subject: [PATCH 14/18] =?UTF-8?q?fix:=20=E9=80=9A=E7=9F=A5=E3=81=97?= =?UTF-8?q?=E3=81=9F=E3=81=8B=E3=81=AE=E5=88=A4=E5=AE=9A=E3=82=92=E5=85=88?= =?UTF-8?q?=E3=81=AB=E8=A1=8C=E3=81=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/dkwatcher/dkwatcher.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/pkg/dkwatcher/dkwatcher.go b/pkg/dkwatcher/dkwatcher.go index 059bdd1..0ccc947 100644 --- a/pkg/dkwatcher/dkwatcher.go +++ b/pkg/dkwatcher/dkwatcher.go @@ -105,15 +105,6 @@ func procedure(ctx context.Context, logger.Info("DisableAutomation was true, skipped") continue } - if err := mw.SetTrack(track); err != nil { - logger.Error(xerrors.Errorf("message: %w", err), "mw.SetTrack was failed") - continue - } - currentTalk, err := track.Talks.GetCurrentTalk() - if err != nil { - logger.Info("currentTalk is none") - currentTalk = &model.Talk{} - } nextTalk, err := track.Talks.GetNextTalk() if err != nil { logger.Info("nextTalk is none") @@ -132,6 +123,15 @@ func procedure(ctx context.Context, logger.Info("nextTalkNotification already sent . trackNo:%s", track.Id) continue } + currentTalk, err := track.Talks.GetCurrentTalk() + if err != nil { + logger.Info("currentTalk is none") + currentTalk = &model.Talk{} + } + if err := mw.SetTrack(track); err != nil { + logger.Error(xerrors.Errorf("message: %w", err), "mw.SetTrack was failed") + continue + } notificationEventSendChan <- model.CurrentAndNextTalk{ Current: *currentTalk, Next: *nextTalk} } From 52dec4b5a183374d8dc96f2125e9770f7f7f7fb2 Mon Sep 17 00:00:00 2001 From: tkc66 Date: Sun, 23 Oct 2022 14:48:00 +0900 Subject: [PATCH 15/18] fix: redis key --- pkg/infra/db/redis.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/infra/db/redis.go b/pkg/infra/db/redis.go index 87529a1..d715811 100644 --- a/pkg/infra/db/redis.go +++ b/pkg/infra/db/redis.go @@ -34,7 +34,7 @@ func NewRedisClient(addr string) (*RedisClient, error) { } func (rc *RedisClient) SetNextTalkNotification(ctx context.Context, id int) error { - return rc.Client.Set(ctx, NextTalkNotificationKey, NextTalkNotificationAlreadySent, RedisExpiration).Err() + return rc.Client.Set(ctx, NextTalkNotificationKey+strconv.Itoa(id), NextTalkNotificationAlreadySent, RedisExpiration).Err() } func (rc *RedisClient) GetNextTalkNotification(ctx context.Context, id int) (string, error) { From 6d98054a9c7c1b27e58ec4d2bdf6e3ba8a685f9e Mon Sep 17 00:00:00 2001 From: ShotaKitazawa Date: Sun, 23 Oct 2022 16:21:03 +0900 Subject: [PATCH 16/18] handled https://github.com/cloudnativedaysjp/cnd-operation-server/pull/50#discussion_r1002642951 --- pkg/dkwatcher/dkwatcher.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/pkg/dkwatcher/dkwatcher.go b/pkg/dkwatcher/dkwatcher.go index 0ccc947..2f36638 100644 --- a/pkg/dkwatcher/dkwatcher.go +++ b/pkg/dkwatcher/dkwatcher.go @@ -98,6 +98,7 @@ func procedure(ctx context.Context, } for _, track := range tracks { logger := rootLogger.WithValues("trackId", track.Id) + if disabled, err := mr.DisableAutomation(track.Id); err != nil { logger.Error(xerrors.Errorf("message: %w", err), "mr.DisableAutomation() was failed") return nil @@ -105,6 +106,12 @@ func procedure(ctx context.Context, logger.Info("DisableAutomation was true, skipped") continue } + + if err := mw.SetTrack(track); err != nil { + logger.Error(xerrors.Errorf("message: %w", err), "mw.SetTrack was failed") + continue + } + nextTalk, err := track.Talks.GetNextTalk() if err != nil { logger.Info("nextTalk is none") @@ -128,10 +135,6 @@ func procedure(ctx context.Context, logger.Info("currentTalk is none") currentTalk = &model.Talk{} } - if err := mw.SetTrack(track); err != nil { - logger.Error(xerrors.Errorf("message: %w", err), "mw.SetTrack was failed") - continue - } notificationEventSendChan <- model.CurrentAndNextTalk{ Current: *currentTalk, Next: *nextTalk} } From c8a9e342dc0b3dec63125a907b3e6ee937775fa0 Mon Sep 17 00:00:00 2001 From: ShotaKitazawa Date: Sun, 23 Oct 2022 16:26:18 +0900 Subject: [PATCH 17/18] handled https://github.com/cloudnativedaysjp/cnd-operation-server/pull/50#discussion_r1002643641 --- pkg/dkwatcher/dkwatcher.go | 12 +++++------- pkg/infra/db/redis.go | 2 +- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/pkg/dkwatcher/dkwatcher.go b/pkg/dkwatcher/dkwatcher.go index 2f36638..678ae75 100644 --- a/pkg/dkwatcher/dkwatcher.go +++ b/pkg/dkwatcher/dkwatcher.go @@ -9,7 +9,7 @@ import ( "go.uber.org/zap" "golang.org/x/xerrors" - infra "github.com/cloudnativedaysjp/cnd-operation-server/pkg/infra/db" + "github.com/cloudnativedaysjp/cnd-operation-server/pkg/infra/db" "github.com/cloudnativedaysjp/cnd-operation-server/pkg/infra/dreamkast" "github.com/cloudnativedaysjp/cnd-operation-server/pkg/infra/sharedmem" "github.com/cloudnativedaysjp/cnd-operation-server/pkg/metrics" @@ -58,7 +58,7 @@ func Run(ctx context.Context, conf Config) error { return err } - redisClient, err := infra.NewRedisClient(conf.RedisHost) + redisClient, err := db.NewRedisClient(conf.RedisHost) if err != nil { return err } @@ -87,7 +87,7 @@ func Run(ctx context.Context, conf Config) error { func procedure(ctx context.Context, dkClient dreamkast.Client, mw sharedmem.WriterIface, mr sharedmem.ReaderIface, - notificationEventSendChan chan<- model.CurrentAndNextTalk, redisClient *infra.RedisClient, + notificationEventSendChan chan<- model.CurrentAndNextTalk, redisClient *db.RedisClient, ) error { rootLogger := utils.GetLogger(ctx) @@ -121,12 +121,10 @@ func procedure(ctx context.Context, logger.Info("nextTalk is not start soon. trackNo:%s", track.Id) continue } - val, err := redisClient.GetNextTalkNotification(ctx, int(nextTalk.Id)) - if err != nil { + if val, err := redisClient.GetNextTalkNotification(ctx, int(nextTalk.Id)); err != nil { logger.Error(xerrors.Errorf("message: %w", err), "db.GetNextTalkNotification() was failed") return err - } - if val != "" { + } else if val != "" { logger.Info("nextTalkNotification already sent . trackNo:%s", track.Id) continue } diff --git a/pkg/infra/db/redis.go b/pkg/infra/db/redis.go index d715811..0ddd72f 100644 --- a/pkg/infra/db/redis.go +++ b/pkg/infra/db/redis.go @@ -5,7 +5,7 @@ import ( "strconv" "time" - "github.com/go-redis/redis/v8" + redis "github.com/go-redis/redis/v8" "golang.org/x/xerrors" ) From f7dd3c127d77e6cdd583f256a2e3e677cab3b7ff Mon Sep 17 00:00:00 2001 From: tkc66 Date: Sun, 23 Oct 2022 16:35:13 +0900 Subject: [PATCH 18/18] =?UTF-8?q?fix:=20mw=E3=82=92=E5=85=88=E3=81=AB?= =?UTF-8?q?=E6=8C=81=E3=81=A3=E3=81=A6=E3=81=8F=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/dkwatcher/dkwatcher.go | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/pkg/dkwatcher/dkwatcher.go b/pkg/dkwatcher/dkwatcher.go index 0ccc947..b7f78ce 100644 --- a/pkg/dkwatcher/dkwatcher.go +++ b/pkg/dkwatcher/dkwatcher.go @@ -105,6 +105,10 @@ func procedure(ctx context.Context, logger.Info("DisableAutomation was true, skipped") continue } + if err := mw.SetTrack(track); err != nil { + logger.Error(xerrors.Errorf("message: %w", err), "mw.SetTrack was failed") + continue + } nextTalk, err := track.Talks.GetNextTalk() if err != nil { logger.Info("nextTalk is none") @@ -114,12 +118,10 @@ func procedure(ctx context.Context, logger.Info("nextTalk is not start soon. trackNo:%s", track.Id) continue } - val, err := redisClient.GetNextTalkNotification(ctx, int(nextTalk.Id)) - if err != nil { + if val, err := redisClient.GetNextTalkNotification(ctx, int(nextTalk.Id)); err != nil { logger.Error(xerrors.Errorf("message: %w", err), "db.GetNextTalkNotification() was failed") return err - } - if val != "" { + } else if val != "" { logger.Info("nextTalkNotification already sent . trackNo:%s", track.Id) continue } @@ -128,10 +130,6 @@ func procedure(ctx context.Context, logger.Info("currentTalk is none") currentTalk = &model.Talk{} } - if err := mw.SetTrack(track); err != nil { - logger.Error(xerrors.Errorf("message: %w", err), "mw.SetTrack was failed") - continue - } notificationEventSendChan <- model.CurrentAndNextTalk{ Current: *currentTalk, Next: *nextTalk} }