github.com/lologarithm/mattermost-server@v5.3.2-0.20181002060438-c82a84ed765b+incompatible/app/cluster.go (about)

     1  // Copyright (c) 2018-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package app
     5  
     6  import (
     7  	"github.com/mattermost/mattermost-server/mlog"
     8  	"github.com/mattermost/mattermost-server/model"
     9  )
    10  
    11  // Registers a given function to be called when the cluster leader may have changed. Returns a unique ID for the
    12  // listener which can later be used to remove it. If clustering is not enabled in this build, the callback will never
    13  // be called.
    14  func (a *App) AddClusterLeaderChangedListener(listener func()) string {
    15  	id := model.NewId()
    16  	a.clusterLeaderListeners.Store(id, listener)
    17  	return id
    18  }
    19  
    20  // Removes a listener function by the unique ID returned when AddConfigListener was called
    21  func (a *App) RemoveClusterLeaderChangedListener(id string) {
    22  	a.clusterLeaderListeners.Delete(id)
    23  }
    24  
    25  func (a *App) InvokeClusterLeaderChangedListeners() {
    26  	mlog.Info("Cluster leader changed. Invoking ClusterLeaderChanged listeners.")
    27  	a.Go(func() {
    28  		a.clusterLeaderListeners.Range(func(_, listener interface{}) bool {
    29  			listener.(func())()
    30  			return true
    31  		})
    32  	})
    33  }