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)
choiceSvc := service.NewChoice(client)
groupSvc := service.NewGroup(groupRepo, client)
monitorSvc := service.NewMonitor(monitorRepo, groupRepo, client)
groupSvc := service.NewGroup(groupRepo, client, cfg.MaxGroupsPerServer)
monitorSvc := service.NewMonitor(monitorRepo, groupRepo, client, cfg.MaxMonitorsPerGroup)
bot, err := discord.NewBot(cfg.Token, groupSvc, monitorSvc, choiceSvc)
if err != nil {
@ -77,7 +77,9 @@ func New() *cli.Command {
}
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) {

View File

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

View File

@ -53,7 +53,7 @@ func TestGroup_Create(t *testing.T) {
}, 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.NotEmpty(t, g.ID)
assert.Equal(t, params.ServerID(), g.ServerID)
@ -72,7 +72,7 @@ func TestGroup_Create(t *testing.T) {
repo := &mock.FakeGroupRepository{}
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{
Current: 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{
VersionCode: params.VersionCode(),
Key: params.ServerKey(),
@ -117,7 +117,7 @@ func TestGroup_Create(t *testing.T) {
}, 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{
VersionCode: params.VersionCode(),
Key: params.ServerKey(),

View File

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

View File

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

View File

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