refactor: maxMonitorsPerGroup, maxGroupsPerServer -> ENVs (#27)
All checks were successful
continuous-integration/drone/push Build is passing

Reviewed-on: #27
This commit is contained in:
Dawid Wysokiński 2022-10-27 12:12:29 +00:00
parent 0a0c0b2acd
commit de19857b34
6 changed files with 44 additions and 38 deletions

View File

@ -47,8 +47,8 @@ func New() *cli.Command {
monitorRepo := bundb.NewMonitor(db) monitorRepo := bundb.NewMonitor(db)
choiceSvc := service.NewChoice(client) choiceSvc := service.NewChoice(client)
groupSvc := service.NewGroup(groupRepo, client) groupSvc := service.NewGroup(groupRepo, client, cfg.MaxGroupsPerServer)
monitorSvc := service.NewMonitor(monitorRepo, groupRepo, client) monitorSvc := service.NewMonitor(monitorRepo, groupRepo, client, cfg.MaxMonitorsPerGroup)
bot, err := discord.NewBot(cfg.Token, groupSvc, monitorSvc, choiceSvc) bot, err := discord.NewBot(cfg.Token, groupSvc, monitorSvc, choiceSvc)
if err != nil { if err != nil {
@ -77,7 +77,9 @@ func New() *cli.Command {
} }
type botConfig struct { type botConfig struct {
Token string `envconfig:"TOKEN" required:"true"` Token string `envconfig:"TOKEN" required:"true"`
MaxGroupsPerServer int `envconfig:"MAX_GROUPS_PER_SERVER" default:"10"`
MaxMonitorsPerGroup int `envconfig:"MAX_MONITORS_PER_GROUP" default:"10"`
} }
func newBotConfig() (botConfig, error) { func newBotConfig() (botConfig, error) {

View File

@ -9,10 +9,6 @@ import (
"gitea.dwysokinski.me/twhelp/dcbot/internal/twhelp" "gitea.dwysokinski.me/twhelp/dcbot/internal/twhelp"
) )
const (
maxGroupsPerServer = 10
)
//counterfeiter:generate -o internal/mock/group_repository.gen.go . GroupRepository //counterfeiter:generate -o internal/mock/group_repository.gen.go . GroupRepository
type GroupRepository interface { type GroupRepository interface {
Create(ctx context.Context, params domain.CreateGroupParams) (domain.Group, error) Create(ctx context.Context, params domain.CreateGroupParams) (domain.Group, error)
@ -23,12 +19,17 @@ type GroupRepository interface {
} }
type Group struct { type Group struct {
repo GroupRepository repo GroupRepository
client TWHelpClient client TWHelpClient
maxGroupsPerServer int
} }
func NewGroup(repo GroupRepository, client TWHelpClient) *Group { func NewGroup(repo GroupRepository, client TWHelpClient, maxGroupsPerServer int) *Group {
return &Group{repo: repo, client: client} return &Group{
repo: repo,
client: client,
maxGroupsPerServer: maxGroupsPerServer,
}
} }
func (g *Group) Create(ctx context.Context, params domain.CreateGroupParams) (domain.Group, error) { func (g *Group) Create(ctx context.Context, params domain.CreateGroupParams) (domain.Group, error) {
@ -39,10 +40,10 @@ func (g *Group) Create(ctx context.Context, params domain.CreateGroupParams) (do
return domain.Group{}, fmt.Errorf("GroupRepository.List: %w", err) return domain.Group{}, fmt.Errorf("GroupRepository.List: %w", err)
} }
if len(groups) >= maxGroupsPerServer { if len(groups) >= g.maxGroupsPerServer {
return domain.Group{}, domain.GroupLimitReachedError{ return domain.Group{}, domain.GroupLimitReachedError{
Current: len(groups), Current: len(groups),
Limit: maxGroupsPerServer, Limit: g.maxGroupsPerServer,
} }
} }

View File

@ -53,7 +53,7 @@ func TestGroup_Create(t *testing.T) {
}, nil }, nil
}) })
g, err := service.NewGroup(repo, client).Create(context.Background(), params) g, err := service.NewGroup(repo, client, 1).Create(context.Background(), params)
assert.NoError(t, err) assert.NoError(t, err)
assert.NotEmpty(t, g.ID) assert.NotEmpty(t, g.ID)
assert.Equal(t, params.ServerID(), g.ServerID) assert.Equal(t, params.ServerID(), g.ServerID)
@ -72,7 +72,7 @@ func TestGroup_Create(t *testing.T) {
repo := &mock.FakeGroupRepository{} repo := &mock.FakeGroupRepository{}
repo.ListReturns(make([]domain.Group, maxGroupsPerServer), nil) repo.ListReturns(make([]domain.Group, maxGroupsPerServer), nil)
g, err := service.NewGroup(repo, nil).Create(context.Background(), params) g, err := service.NewGroup(repo, nil, maxGroupsPerServer).Create(context.Background(), params)
assert.ErrorIs(t, err, domain.GroupLimitReachedError{ assert.ErrorIs(t, err, domain.GroupLimitReachedError{
Current: maxGroupsPerServer, Current: maxGroupsPerServer,
Limit: maxGroupsPerServer, Limit: maxGroupsPerServer,
@ -94,7 +94,7 @@ func TestGroup_Create(t *testing.T) {
} }
}) })
g, err := service.NewGroup(repo, client).Create(context.Background(), params) g, err := service.NewGroup(repo, client, 1).Create(context.Background(), params)
assert.ErrorIs(t, err, domain.ServerDoesNotExistError{ assert.ErrorIs(t, err, domain.ServerDoesNotExistError{
VersionCode: params.VersionCode(), VersionCode: params.VersionCode(),
Key: params.ServerKey(), Key: params.ServerKey(),
@ -117,7 +117,7 @@ func TestGroup_Create(t *testing.T) {
}, nil }, nil
}) })
g, err := service.NewGroup(repo, client).Create(context.Background(), params) g, err := service.NewGroup(repo, client, 1).Create(context.Background(), params)
assert.ErrorIs(t, err, domain.ServerIsClosedError{ assert.ErrorIs(t, err, domain.ServerIsClosedError{
VersionCode: params.VersionCode(), VersionCode: params.VersionCode(),
Key: params.ServerKey(), Key: params.ServerKey(),

View File

@ -11,10 +11,6 @@ import (
"gitea.dwysokinski.me/twhelp/dcbot/internal/twhelp" "gitea.dwysokinski.me/twhelp/dcbot/internal/twhelp"
) )
const (
maxMonitorsPerGroup = 10
)
//counterfeiter:generate -o internal/mock/monitor_repository.gen.go . MonitorRepository //counterfeiter:generate -o internal/mock/monitor_repository.gen.go . MonitorRepository
type MonitorRepository interface { type MonitorRepository interface {
Create(ctx context.Context, params domain.CreateMonitorParams) (domain.Monitor, error) Create(ctx context.Context, params domain.CreateMonitorParams) (domain.Monitor, error)
@ -30,23 +26,26 @@ type GroupReader interface {
} }
type Monitor struct { type Monitor struct {
repo MonitorRepository repo MonitorRepository
client TWHelpClient client TWHelpClient
groupSvc GroupReader groupSvc GroupReader
ennoblementsMu sync.Mutex // ennoblementsMu is used by Monitor.fetchEnnoblements maxMonitorsPerGroup int
ennoblementsSince map[string]time.Time // ennoblementsSince is used by Monitor.fetchEnnoblements ennoblementsMu sync.Mutex // ennoblementsMu is used by Monitor.fetchEnnoblements
ennoblementsSince map[string]time.Time // ennoblementsSince is used by Monitor.fetchEnnoblements
} }
func NewMonitor( func NewMonitor(
repo MonitorRepository, repo MonitorRepository,
groupSvc GroupReader, groupSvc GroupReader,
client TWHelpClient, client TWHelpClient,
maxMonitorsPerGroup int,
) *Monitor { ) *Monitor {
return &Monitor{ return &Monitor{
repo: repo, repo: repo,
client: client, client: client,
groupSvc: groupSvc, groupSvc: groupSvc,
ennoblementsSince: make(map[string]time.Time), maxMonitorsPerGroup: maxMonitorsPerGroup,
ennoblementsSince: make(map[string]time.Time),
} }
} }
@ -66,10 +65,10 @@ func (m *Monitor) Create(ctx context.Context, groupID, serverID, tribeTag string
return domain.Monitor{}, fmt.Errorf("MonitorRepository.List: %w", err) return domain.Monitor{}, fmt.Errorf("MonitorRepository.List: %w", err)
} }
if len(monitors) >= maxMonitorsPerGroup { if len(monitors) >= m.maxMonitorsPerGroup {
return domain.Monitor{}, domain.MonitorLimitReachedError{ return domain.Monitor{}, domain.MonitorLimitReachedError{
Current: len(monitors), Current: len(monitors),
Limit: maxMonitorsPerGroup, Limit: m.maxMonitorsPerGroup,
} }
} }

View File

@ -56,7 +56,7 @@ func TestMonitor_Create(t *testing.T) {
groupID := uuid.NewString() groupID := uuid.NewString()
monitor, err := service.NewMonitor(repo, groupSvc, client). monitor, err := service.NewMonitor(repo, groupSvc, client, 10).
Create(context.Background(), groupID, uuid.NewString(), tribe.Tag) Create(context.Background(), groupID, uuid.NewString(), tribe.Tag)
assert.NoError(t, err) assert.NoError(t, err)
assert.NotEmpty(t, monitor.ID) assert.NotEmpty(t, monitor.ID)
@ -72,7 +72,7 @@ func TestMonitor_Create(t *testing.T) {
groupSvc := &mock.FakeGroupReader{} groupSvc := &mock.FakeGroupReader{}
groupSvc.GetReturns(domain.Group{}, domain.GroupNotFoundError{ID: groupID}) groupSvc.GetReturns(domain.Group{}, domain.GroupNotFoundError{ID: groupID})
monitor, err := service.NewMonitor(nil, groupSvc, nil). monitor, err := service.NewMonitor(nil, groupSvc, nil, 10).
Create(context.Background(), groupID, uuid.NewString(), "tag") Create(context.Background(), groupID, uuid.NewString(), "tag")
assert.ErrorIs(t, err, domain.GroupDoesNotExistError{ assert.ErrorIs(t, err, domain.GroupDoesNotExistError{
ID: groupID, ID: groupID,
@ -101,7 +101,7 @@ func TestMonitor_Create(t *testing.T) {
}, nil }, nil
}) })
monitor, err := service.NewMonitor(repo, groupSvc, nil). monitor, err := service.NewMonitor(repo, groupSvc, nil, maxMonitorsPerGroup).
Create(context.Background(), uuid.NewString(), uuid.NewString(), "TAG") Create(context.Background(), uuid.NewString(), uuid.NewString(), "TAG")
assert.ErrorIs(t, err, domain.MonitorLimitReachedError{ assert.ErrorIs(t, err, domain.MonitorLimitReachedError{
Current: maxMonitorsPerGroup, Current: maxMonitorsPerGroup,
@ -148,7 +148,7 @@ func TestMonitor_Create(t *testing.T) {
tag := "TAG" tag := "TAG"
monitor, err := service.NewMonitor(repo, groupSvc, client). monitor, err := service.NewMonitor(repo, groupSvc, client, 10).
Create(context.Background(), uuid.NewString(), uuid.NewString(), tag) Create(context.Background(), uuid.NewString(), uuid.NewString(), tag)
assert.ErrorIs(t, err, domain.TribeDoesNotExistError{ assert.ErrorIs(t, err, domain.TribeDoesNotExistError{
Tag: tag, Tag: tag,
@ -185,7 +185,7 @@ func TestMonitor_Create(t *testing.T) {
} }
client.GetTribeByTagReturns(tribe, nil) client.GetTribeByTagReturns(tribe, nil)
monitor, err := service.NewMonitor(repo, groupSvc, client). monitor, err := service.NewMonitor(repo, groupSvc, client, 10).
Create(context.Background(), uuid.NewString(), uuid.NewString(), tribe.Tag) Create(context.Background(), uuid.NewString(), uuid.NewString(), tribe.Tag)
assert.ErrorIs(t, err, domain.TribeDoesNotExistError{ assert.ErrorIs(t, err, domain.TribeDoesNotExistError{
Tag: tribe.Tag, Tag: tribe.Tag,
@ -598,7 +598,7 @@ func TestMonitor_Execute(t *testing.T) {
return monitors[groupID], nil return monitors[groupID], nil
}) })
notifications, err := service.NewMonitor(repo, groupSvc, client). notifications, err := service.NewMonitor(repo, groupSvc, client, 10).
Execute(context.Background()) Execute(context.Background())
assert.NoError(t, err) assert.NoError(t, err)
expectedNotifications := []domain.EnnoblementNotification{ expectedNotifications := []domain.EnnoblementNotification{

View File

@ -34,6 +34,10 @@ spec:
key: token key: token
- name: TWHELP_URL - name: TWHELP_URL
value: "https://tribalwarshelp.com" value: "https://tribalwarshelp.com"
- name: BOT_MAX_GROUPS_PER_SERVER
value: "10"
- name: BOT_MAX_MONITORS_PER_GROUP
value: "10"
livenessProbe: livenessProbe:
exec: exec:
command: [ "cat", "/tmp/healthy" ] command: [ "cat", "/tmp/healthy" ]