github.com/adacta-ru/mattermost-server/v6@v6.0.0/app/cluster.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package app
     5  
     6  import (
     7  	"github.com/adacta-ru/mattermost-server/v6/model"
     8  )
     9  
    10  // Registers a given function to be called when the cluster leader may have changed. Returns a unique ID for the
    11  // listener which can later be used to remove it. If clustering is not enabled in this build, the callback will never
    12  // be called.
    13  func (s *Server) AddClusterLeaderChangedListener(listener func()) string {
    14  	id := model.NewId()
    15  	s.clusterLeaderListeners.Store(id, listener)
    16  	return id
    17  }
    18  
    19  // Removes a listener function by the unique ID returned when AddConfigListener was called
    20  func (s *Server) RemoveClusterLeaderChangedListener(id string) {
    21  	s.clusterLeaderListeners.Delete(id)
    22  }
    23  
    24  func (s *Server) InvokeClusterLeaderChangedListeners() {
    25  	s.Log.Info("Cluster leader changed. Invoking ClusterLeaderChanged listeners.")
    26  	// This needs to be run in a separate goroutine otherwise a recursive lock happens
    27  	// because the listener function eventually ends up calling .IsLeader().
    28  	// Fixing this would require the changed event to pass the leader directly, but that
    29  	// requires a lot of work.
    30  	s.Go(func() {
    31  		s.clusterLeaderListeners.Range(func(_, listener interface{}) bool {
    32  			listener.(func())()
    33  			return true
    34  		})
    35  	})
    36  }