github.com/vnforks/kid/v5@v5.22.1-0.20200408055009-b89d99c65676/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/vnforks/kid/v5/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  	s.Go(func() {
    27  		s.clusterLeaderListeners.Range(func(_, listener interface{}) bool {
    28  			listener.(func())()
    29  			return true
    30  		})
    31  	})
    32  }