Add wait option for populateUsers/Channels (slack) Fixes #579 (#653)

When setting wait to true, we wait until the populating isn't in progress anymore.
This is used on startup connections where we really need the initial information
which could take a long time on big servers.
This commit is contained in:
Wim 2018-12-15 23:11:03 +01:00 committed by GitHub
parent 192fe89789
commit 53c86702a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 10 deletions

View File

@ -75,7 +75,7 @@ func (b *Bslack) handleSlackClient(messages chan *config.Message) {
// When we join a channel we update the full list of users as // When we join a channel we update the full list of users as
// well as the information for the channel that we joined as this // well as the information for the channel that we joined as this
// should now tell that we are a member of it. // should now tell that we are a member of it.
b.populateUsers() b.populateUsers(false)
b.channelsMutex.Lock() b.channelsMutex.Lock()
b.channelsByID[ev.Channel.ID] = &ev.Channel b.channelsByID[ev.Channel.ID] = &ev.Channel
@ -83,8 +83,8 @@ func (b *Bslack) handleSlackClient(messages chan *config.Message) {
b.channelsMutex.Unlock() b.channelsMutex.Unlock()
case *slack.ConnectedEvent: case *slack.ConnectedEvent:
b.si = ev.Info b.si = ev.Info
b.populateChannels() b.populateChannels(true)
b.populateUsers() b.populateUsers(true)
case *slack.InvalidAuthEvent: case *slack.InvalidAuthEvent:
b.Log.Fatalf("Invalid Token %#v", ev) b.Log.Fatalf("Invalid Token %#v", ev)
case *slack.ConnectionErrorEvent: case *slack.ConnectionErrorEvent:
@ -200,7 +200,7 @@ func (b *Bslack) handleMessageEvent(ev *slack.MessageEvent) (*config.Message, er
func (b *Bslack) handleStatusEvent(ev *slack.MessageEvent, rmsg *config.Message) bool { func (b *Bslack) handleStatusEvent(ev *slack.MessageEvent, rmsg *config.Message) bool {
switch ev.SubType { switch ev.SubType {
case sChannelJoined, sMemberJoined: case sChannelJoined, sMemberJoined:
b.populateUsers() b.populateUsers(false)
// There's no further processing needed on channel events // There's no further processing needed on channel events
// so we return 'true'. // so we return 'true'.
return true return true
@ -208,7 +208,7 @@ func (b *Bslack) handleStatusEvent(ev *slack.MessageEvent, rmsg *config.Message)
rmsg.Username = sSystemUser rmsg.Username = sSystemUser
rmsg.Event = config.EventJoinLeave rmsg.Event = config.EventJoinLeave
case sChannelTopic, sChannelPurpose: case sChannelTopic, sChannelPurpose:
b.populateChannels() b.populateChannels(false)
rmsg.Event = config.EventTopicChange rmsg.Event = config.EventTopicChange
case sMessageChanged: case sMessageChanged:
rmsg.Text = ev.SubMessage.Text rmsg.Text = ev.SubMessage.Text

View File

@ -83,15 +83,18 @@ func (b *Bslack) populateUser(userID string) {
b.usersMutex.Unlock() b.usersMutex.Unlock()
} }
func (b *Bslack) populateUsers() { func (b *Bslack) populateUsers(wait bool) {
b.refreshMutex.Lock() b.refreshMutex.Lock()
if time.Now().Before(b.earliestUserRefresh) || b.refreshInProgress { if !wait && (time.Now().Before(b.earliestUserRefresh) || b.refreshInProgress) {
b.Log.Debugf("Not refreshing user list as it was done less than %v ago.", b.Log.Debugf("Not refreshing user list as it was done less than %v ago.",
minimumRefreshInterval) minimumRefreshInterval)
b.refreshMutex.Unlock() b.refreshMutex.Unlock()
return return
} }
for b.refreshInProgress {
time.Sleep(time.Second)
}
b.refreshInProgress = true b.refreshInProgress = true
b.refreshMutex.Unlock() b.refreshMutex.Unlock()
@ -127,14 +130,17 @@ func (b *Bslack) populateUsers() {
b.refreshInProgress = false b.refreshInProgress = false
} }
func (b *Bslack) populateChannels() { func (b *Bslack) populateChannels(wait bool) {
b.refreshMutex.Lock() b.refreshMutex.Lock()
if time.Now().Before(b.earliestChannelRefresh) || b.refreshInProgress { if !wait && (time.Now().Before(b.earliestChannelRefresh) || b.refreshInProgress) {
b.Log.Debugf("Not refreshing channel list as it was done less than %v seconds ago.", b.Log.Debugf("Not refreshing channel list as it was done less than %v seconds ago.",
minimumRefreshInterval) minimumRefreshInterval)
b.refreshMutex.Unlock() b.refreshMutex.Unlock()
return return
} }
for b.refreshInProgress {
time.Sleep(time.Second)
}
b.refreshInProgress = true b.refreshInProgress = true
b.refreshMutex.Unlock() b.refreshMutex.Unlock()

View File

@ -160,7 +160,7 @@ func (b *Bslack) JoinChannel(channel config.ChannelInfo) error {
return nil return nil
} }
b.populateChannels() b.populateChannels(false)
channelInfo, err := b.getChannel(channel.Name) channelInfo, err := b.getChannel(channel.Name)
if err != nil { if err != nil {