forked from twirapp/twir
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
212 additions
and
28 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package timers | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
"time" | ||
|
||
"github.com/redis/go-redis/v9" | ||
"github.com/samber/lo" | ||
config "github.com/satont/twir/libs/config" | ||
model "github.com/satont/twir/libs/gomodels" | ||
"github.com/satont/twir/libs/logger" | ||
commandscache "github.com/twirapp/twir/libs/cache/commands" | ||
generic_cacher "github.com/twirapp/twir/libs/cache/generic-cacher" | ||
"go.uber.org/fx" | ||
"gorm.io/gorm" | ||
) | ||
|
||
type ExpiredCommandsOpts struct { | ||
fx.In | ||
Lc fx.Lifecycle | ||
|
||
Logger logger.Logger | ||
Config config.Config | ||
|
||
Gorm *gorm.DB | ||
RedisClient *redis.Client | ||
} | ||
|
||
type expiredCommands struct { | ||
config config.Config | ||
logger logger.Logger | ||
db *gorm.DB | ||
commandsCache *generic_cacher.GenericCacher[[]model.ChannelsCommands] | ||
} | ||
|
||
func NewExpiredCommands(opts ExpiredCommandsOpts) *expiredCommands { | ||
timeTick := lo.If(opts.Config.AppEnv != "production", 15*time.Second).Else(5 * time.Minute) | ||
ticker := time.NewTicker(timeTick) | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
|
||
s := &expiredCommands{ | ||
config: opts.Config, | ||
logger: opts.Logger, | ||
db: opts.Gorm, | ||
commandsCache: commandscache.New(opts.Gorm, opts.RedisClient), | ||
} | ||
|
||
opts.Lc.Append( | ||
fx.Hook{ | ||
OnStart: func(_ context.Context) error { | ||
go func() { | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
ticker.Stop() | ||
return | ||
case <-ticker.C: | ||
s.checkForExpiredCommands(ctx) | ||
} | ||
} | ||
}() | ||
|
||
return nil | ||
}, | ||
OnStop: func(_ context.Context) error { | ||
cancel() | ||
return nil | ||
}, | ||
}, | ||
) | ||
|
||
return s | ||
} | ||
|
||
func (s *expiredCommands) checkForExpiredCommands(ctx context.Context) { | ||
var commands []model.ChannelsCommands | ||
if err := s.db.WithContext(ctx).Preload("Channel").Where( | ||
`"expires_at" < ? AND "expired" = ?`, | ||
time.Now().UTC(), | ||
false, | ||
).Find(&commands).Error; err != nil { | ||
s.logger.Error("failed to get commands", slog.Any("err", err)) | ||
return | ||
} | ||
|
||
for _, c := range commands { | ||
s.logger.Info("Command expired", slog.Any("command", c)) | ||
c.Expired = true | ||
|
||
err := s.db.WithContext(ctx).Updates( | ||
&c, | ||
).Error | ||
if err != nil { | ||
s.logger.Error("failed to update command", slog.Any("err", err)) | ||
} | ||
|
||
err = s.commandsCache.Invalidate( | ||
ctx, | ||
c.Channel.ID) | ||
if err != nil { | ||
s.logger.Error("failed to invalidate commands cache", slog.Any("err", err)) | ||
return | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
libs/migrations/migrations/20241025040229_commands_expiration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
-- +goose Up | ||
-- +goose StatementBegin | ||
SELECT 'up SQL query'; | ||
|
||
ALTER TABLE channels_commands ADD COLUMN expires_at timestamptz; | ||
ALTER TABLE channels_commands ADD COLUMN expired boolean default false; | ||
ALTER TABLE channels_commands ADD COLUMN expired_in integer DEFAULT 0; | ||
-- +goose StatementEnd | ||
|
||
-- +goose Down | ||
-- +goose StatementBegin | ||
SELECT 'down SQL query'; | ||
-- +goose StatementEnd |