Skip to content

Commit

Permalink
allow engine copy to perform safe state mutations
Browse files Browse the repository at this point in the history
  • Loading branch information
fmartingr committed Jul 31, 2023
1 parent 08b039e commit b240b85
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 7 deletions.
4 changes: 2 additions & 2 deletions server/mscalendar/availability.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,14 @@ func (m *mscalendar) retrieveUsersToSync(userIndex store.UserIndex, syncJobSumma
users = append(users, user)

if fetchIndividually {
err = m.Filter(withActingUser(user.MattermostUserID))
engine, err := m.FilterCopy(withActingUser(user.MattermostUserID))
if err != nil {
m.Logger.Warnf("Not able to enable active user %s from user index. err=%v", user.MattermostUserID, err)
continue
}

calendarUser := newUserFromStoredUser(user)
calendarEvents, err := m.GetCalendarEvents(calendarUser, start, end)
calendarEvents, err := engine.GetCalendarEvents(calendarUser, start, end)
if err != nil {
syncJobSummary.NumberOfUsersFailedStatusChanged++
m.Logger.With(bot.LogContext{
Expand Down
14 changes: 9 additions & 5 deletions server/mscalendar/daily_summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,17 +145,21 @@ func (m *mscalendar) ProcessAllDailySummary(now time.Time) error {
continue
}

m.Filter(withActingUser(storeUser.MattermostUserID))
engine, err := m.FilterCopy(withActingUser(storeUser.MattermostUserID))
if err != nil {
m.Logger.Errorf("Error creating user engine %s. err=%v", storeUser.MattermostUserID, err)
continue
}

tz, err := m.GetTimezone(u)
tz, err := engine.GetTimezone(u)
if err != nil {
m.Logger.Errorf("Error posting daily summary for user %s. err=%v", storeUser.MattermostUserID, shouldPostErr)
m.Logger.Errorf("Error posting daily summary for user %s. err=%v", storeUser.MattermostUserID, err)
continue
}

events, err := m.getTodayCalendarEvents(u, now, tz)
events, err := engine.getTodayCalendarEvents(u, now, tz)
if err != nil {
m.Logger.Errorf("Error posting daily summary for user %s. err=%v", storeUser.MattermostUserID, shouldPostErr)
m.Logger.Errorf("Error posting daily summary for user %s. err=%v", storeUser.MattermostUserID, err)
continue
}

Expand Down
12 changes: 12 additions & 0 deletions server/mscalendar/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

package mscalendar

import "github.com/pkg/errors"

type filterf func(*mscalendar) error

func (m *mscalendar) Filter(filters ...filterf) error {
Expand All @@ -15,6 +17,16 @@ func (m *mscalendar) Filter(filters ...filterf) error {
return nil
}

// FilterCopy creates a copy of the calendar engine and applies filters to it
func (m *mscalendar) FilterCopy(filters ...filterf) (*mscalendar, error) {
engine := m.copy()
if err := engine.Filter(filters...); err != nil {
return nil, errors.Wrap(err, "error filtering engine copy")
}

return engine, nil
}

func withActingUserExpanded(m *mscalendar) error {
return m.ExpandUser(m.actingUser)
}
Expand Down
11 changes: 11 additions & 0 deletions server/mscalendar/mscalendar.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ type mscalendar struct {
client remote.Client
}

// copy returns a copy of the calendar engine
func (m mscalendar) copy() *mscalendar {
user := *m.actingUser
client := m.client
return &mscalendar{
Env: m.Env,
actingUser: &user,
client: client,
}
}

func New(env Env, actingMattermostUserID string) MSCalendar {
return &mscalendar{
Env: env,
Expand Down
23 changes: 23 additions & 0 deletions server/mscalendar/mscalendar_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package mscalendar

import (
"testing"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
)

func TestCopy(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
user := newTestUserNumbered(1)
env, _ := makeStatusSyncTestEnv(ctrl)

engine := New(env, user.MattermostUserID)

engineCopy := engine.(*mscalendar).copy()

assert.NotSame(t, engine, engineCopy)
assert.NotSame(t, engine.(*mscalendar).actingUser, engineCopy.actingUser)
assert.NotSame(t, engine.(*mscalendar).client, engineCopy.client)
}

0 comments on commit b240b85

Please sign in to comment.