Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue/#19 #50

Merged
merged 20 commits into from
Oct 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cmd/server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type Config struct {
WsProxy WsProxyConfig `json:"wsProxy" validate:"required"`
Metrics MetricsConfig `json:"metrics"`
Dreamkast DreamkastConfig `json:"dreamkast" validate:"required"`
Redis RedisConfig `json:"redis " validate:"required"`
Tracks []TrackConfig `json:"tracks" validate:"required"`
}

Expand Down Expand Up @@ -83,3 +84,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"`
}
2 changes: 2 additions & 0 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func main() {
Auth0ClientId: conf.Dreamkast.Auth0ClientId,
Auth0ClientSecret: conf.Dreamkast.Auth0ClientSecret,
Auth0ClientAudience: conf.Dreamkast.Auth0ClientAudience,
RedisHost: conf.Redis.Host,
NotificationEventSendChan: talkStream,
})
})
Expand All @@ -89,6 +90,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,
})
Expand Down
1 change: 1 addition & 0 deletions development/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ SWITHCER01_SLACK_CHANNEL_ID=...
GITHUB_ACCESS_TOKEN=...
SWITCHER01_HOST=...
SWITCHER01_PASSWORD=...
REDIS_HOST=...
```

* コンテナの起動
Expand Down
7 changes: 7 additions & 0 deletions development/docker-compose.without-server.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 7 additions & 0 deletions development/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,15 @@ require (

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/buger/jsonparser v1.1.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/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
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.1 // indirect
Expand Down
21 changes: 9 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -58,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=
Expand All @@ -70,10 +68,13 @@ 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=
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=
Expand All @@ -98,6 +99,8 @@ 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/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=
Expand Down Expand Up @@ -169,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=
Expand Down Expand Up @@ -199,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=
Expand All @@ -226,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=
Expand Down Expand Up @@ -269,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=
Expand Down Expand Up @@ -574,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=
Expand All @@ -599,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=
Expand Down
51 changes: 35 additions & 16 deletions pkg/dkwatcher/dkwatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"go.uber.org/zap"
"golang.org/x/xerrors"

"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"
Expand All @@ -27,6 +28,7 @@ type Config struct {
Auth0ClientId string
Auth0ClientSecret string
Auth0ClientAudience string
RedisHost string
NotificationEventSendChan chan<- model.CurrentAndNextTalk
}

Expand Down Expand Up @@ -56,11 +58,16 @@ func Run(ctx context.Context, conf Config) error {
return err
}

redisClient, err := db.NewRedisClient(conf.RedisHost)
if err != nil {
return err
}

mw := sharedmem.Writer{UseStorageForTrack: true}
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 {
Expand All @@ -69,7 +76,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
}
}
Expand All @@ -78,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,
notificationEventSendChan chan<- model.CurrentAndNextTalk, redisClient *db.RedisClient,
) error {
rootLogger := utils.GetLogger(ctx)

Expand All @@ -102,20 +111,30 @@ func procedure(ctx context.Context,
logger.Error(xerrors.Errorf("message: %w", err), "mw.SetTrack was failed")
continue
}
if track.Talks.WillStartNextTalkSince(howManyMinutesUntilNotify) {
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}

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
}
if val, err := redisClient.GetNextTalkNotification(ctx, int(nextTalk.Id)); err != nil {
logger.Error(xerrors.Errorf("message: %w", err), "db.GetNextTalkNotification() was failed")
return err
} else if val != "" {
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{}
}
notificationEventSendChan <- model.CurrentAndNextTalk{
Current: *currentTalk, Next: *nextTalk}
}
return nil
}
42 changes: 42 additions & 0 deletions pkg/infra/db/redis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package db

import (
"context"
"strconv"
"time"

redis "github.com/go-redis/redis/v8"
"golang.org/x/xerrors"
)

type RedisClient struct {
Client *redis.Client
}

const (
RedisExpiration = 10 * time.Minute
NextTalkNotificationKey = "nextTalkNotificationAlreadySentFlag:"
NextTalkNotificationAlreadySent = 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
}

func (rc *RedisClient) SetNextTalkNotification(ctx context.Context, id int) error {
return rc.Client.Set(ctx, NextTalkNotificationKey+strconv.Itoa(id), 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()
}
18 changes: 9 additions & 9 deletions pkg/model/talk.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const (

type Talks []Talk

func (ts Talks) WillStartNextTalkSince(untilNotify time.Duration) bool {
func (ts Talks) IsStartNextTalkSoon(untilNotify time.Duration) bool {
now := nowFunc()
nextTalk, err := ts.GetNextTalk()
if err != nil {
Expand All @@ -42,12 +42,12 @@ 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() (*Talk, error) {
if len(ts) == 0 {
return nil, fmt.Errorf("Talks is empty")
return nil, fmt.Errorf("talks is empty")
}

currentTalk, err := ts.GetCurrentTalk()
Expand All @@ -64,12 +64,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")
}

//
Expand All @@ -88,10 +88,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 {
Expand Down Expand Up @@ -142,6 +138,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
//
Expand Down
Loading