-
Notifications
You must be signed in to change notification settings - Fork 0
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
Issue/#19 #50
Changes from 16 commits
69c36d9
c1729bb
53b1ab5
dfc56c2
aa3d258
324ec72
f584a2e
53bfe8f
9b5c68f
4fa914b
0daa5fc
4019ef7
49553b7
816034e
a44759e
52dec4b
6d98054
c8a9e34
f7dd3c1
67c2612
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +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/dreamkast" | ||
"github.com/cloudnativedaysjp/cnd-operation-server/pkg/infra/sharedmem" | ||
"github.com/cloudnativedaysjp/cnd-operation-server/pkg/metrics" | ||
|
@@ -27,6 +28,7 @@ type Config struct { | |
Auth0ClientId string | ||
Auth0ClientSecret string | ||
Auth0ClientAudience string | ||
RedisHost string | ||
NotificationEventSendChan chan<- model.CurrentAndNextTalk | ||
} | ||
|
||
|
@@ -56,11 +58,16 @@ func Run(ctx context.Context, conf Config) error { | |
return err | ||
} | ||
|
||
redisClient, err := infra.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 { | ||
|
@@ -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 | ||
} | ||
} | ||
|
@@ -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 *infra.RedisClient, | ||
) error { | ||
rootLogger := utils.GetLogger(ctx) | ||
|
||
|
@@ -89,33 +98,42 @@ 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 | ||
} else if disabled { | ||
logger.Info("DisableAutomation was true, skipped") | ||
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 | ||
} | ||
val, err := redisClient.GetNextTalkNotification(ctx, int(nextTalk.Id)) | ||
if err != nil { | ||
logger.Error(xerrors.Errorf("message: %w", err), "db.GetNextTalkNotification() was failed") | ||
return err | ||
} | ||
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{} | ||
} | ||
if err := mw.SetTrack(track); err != nil { | ||
logger.Error(xerrors.Errorf("message: %w", err), "mw.SetTrack was failed") | ||
continue | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dreamkast から取得してきたデータはまず mw に保存して、それから dkwatcher -> notifier に通知するかを判断するべきです。(でないと obswatcher が古いタイムテーブルを見続ける可能性があるため) そのため、 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ありがとうございます。確かに先にmwに保存するべきですね。 |
||
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} | ||
} | ||
notificationEventSendChan <- model.CurrentAndNextTalk{ | ||
Current: *currentTalk, Next: *nextTalk} | ||
} | ||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package db | ||
|
||
import ( | ||
"context" | ||
"strconv" | ||
"time" | ||
|
||
"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() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(nits なので直さなくて問題ないです)