Skip to content

Commit

Permalink
Krutoy kostil
Browse files Browse the repository at this point in the history
  • Loading branch information
Satont committed Dec 17, 2023
1 parent 2244205 commit 2152790
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
2 changes: 2 additions & 0 deletions apps/timers/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/satont/twir/apps/timers/internal/activity"
"github.com/satont/twir/apps/timers/internal/gorm"
"github.com/satont/twir/apps/timers/internal/grpc_server"
"github.com/satont/twir/apps/timers/internal/redis"
"github.com/satont/twir/apps/timers/internal/repositories/channels"
"github.com/satont/twir/apps/timers/internal/repositories/streams"
"github.com/satont/twir/apps/timers/internal/repositories/timers"
Expand All @@ -27,6 +28,7 @@ func main() {
sentryInternal.NewFx(sentryInternal.NewFxOpts{Service: "timers"}),
logger.NewFx(logger.Opts{Level: slog.LevelInfo, Service: "timers"}),
gorm.New,
redis.New,
timers.NewGorm,
activity.New,
workflow.New,
Expand Down
25 changes: 20 additions & 5 deletions apps/timers/internal/activity/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package activity
import (
"context"
"errors"
"time"

"github.com/redis/go-redis/v9"
"github.com/satont/twir/apps/timers/internal/repositories/channels"
"github.com/satont/twir/apps/timers/internal/repositories/streams"
"github.com/satont/twir/apps/timers/internal/repositories/timers"
Expand All @@ -22,6 +24,7 @@ type Opts struct {
Cfg config.Config
ParserGrpc parser.ParserClient
BotsGrpc bots.BotsClient
Redis *redis.Client
}

func New(opts Opts) *Activity {
Expand All @@ -32,6 +35,7 @@ func New(opts Opts) *Activity {
cfg: opts.Cfg,
parserGrpc: opts.ParserGrpc,
botsGrpc: opts.BotsGrpc,
redis: opts.Redis,
}
}

Expand All @@ -42,28 +46,34 @@ type Activity struct {
cfg config.Config
parserGrpc parser.ParserClient
botsGrpc bots.BotsClient
redis *redis.Client
}

func (c *Activity) SendMessage(ctx context.Context, timerId string, currentResponse int) (
func (c *Activity) SendMessage(ctx context.Context, timerId string, _ int) (
int,
error,
) {
timer, err := c.timersRepository.GetById(timerId)
if err != nil {
return currentResponse, err
return 0, err
}

channel, err := c.channelsRepository.GetById(timer.ChannelID)
if err != nil {
return currentResponse, err
return 0, err
}

if !channel.Enabled {
return currentResponse, nil
return 0, nil
}

if !channel.IsBotMod {
return currentResponse, nil
return 0, nil
}

currentResponse, err := c.redis.Get(ctx, "timers:current_response:"+timerId).Int()
if err != nil && !errors.Is(err, redis.Nil) {
return currentResponse, err
}

stream, err := c.streamsRepository.GetByChannelId(channel.ID)
Expand Down Expand Up @@ -101,6 +111,11 @@ func (c *Activity) SendMessage(ctx context.Context, timerId string, currentRespo
nextIndex = 0
}

err = c.redis.Set(ctx, "timers:current_response:"+timerId, nextIndex, 24*time.Hour).Err()
if err != nil {
return nextIndex, err
}

return nextIndex, nil
}

Expand Down
15 changes: 15 additions & 0 deletions apps/timers/internal/redis/redis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package redis

import (
"github.com/redis/go-redis/v9"
cfg "github.com/satont/twir/libs/config"
)

func New(config cfg.Config) (*redis.Client, error) {
redisOpts, err := redis.ParseURL(config.RedisUrl)
if err != nil {
return nil, err
}

return redis.NewClient(redisOpts), nil
}
2 changes: 0 additions & 2 deletions apps/timers/internal/workflow/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,5 @@ func (c *Workflow) Flow(ctx workflow.Context, timer timers.Timer) error {
return err
}

_ = workflow.Sleep(ctx, 1*time.Second)

return nil
}

0 comments on commit 2152790

Please sign in to comment.