github.com/crspeller/mattermost-server@v0.0.0-20190328001957-a200beb3d111/app/cluster_discovery.go (about)

     1  // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package app
     5  
     6  import (
     7  	"fmt"
     8  	"time"
     9  
    10  	"github.com/crspeller/mattermost-server/mlog"
    11  	"github.com/crspeller/mattermost-server/model"
    12  )
    13  
    14  const (
    15  	DISCOVERY_SERVICE_WRITE_PING = 60 * time.Second
    16  )
    17  
    18  type ClusterDiscoveryService struct {
    19  	model.ClusterDiscovery
    20  	app  *App
    21  	stop chan bool
    22  }
    23  
    24  func (a *App) NewClusterDiscoveryService() *ClusterDiscoveryService {
    25  	ds := &ClusterDiscoveryService{
    26  		ClusterDiscovery: model.ClusterDiscovery{},
    27  		app:              a,
    28  		stop:             make(chan bool),
    29  	}
    30  
    31  	return ds
    32  }
    33  
    34  func (me *ClusterDiscoveryService) Start() {
    35  
    36  	<-me.app.Srv.Store.ClusterDiscovery().Cleanup()
    37  
    38  	if cresult := <-me.app.Srv.Store.ClusterDiscovery().Exists(&me.ClusterDiscovery); cresult.Err != nil {
    39  		mlog.Error(fmt.Sprintf("ClusterDiscoveryService failed to check if row exists for %v with err=%v", me.ClusterDiscovery.ToJson(), cresult.Err))
    40  	} else {
    41  		if cresult.Data.(bool) {
    42  			if u := <-me.app.Srv.Store.ClusterDiscovery().Delete(&me.ClusterDiscovery); u.Err != nil {
    43  				mlog.Error(fmt.Sprintf("ClusterDiscoveryService failed to start clean for %v with err=%v", me.ClusterDiscovery.ToJson(), u.Err))
    44  			}
    45  		}
    46  	}
    47  
    48  	if result := <-me.app.Srv.Store.ClusterDiscovery().Save(&me.ClusterDiscovery); result.Err != nil {
    49  		mlog.Error(fmt.Sprintf("ClusterDiscoveryService failed to save for %v with err=%v", me.ClusterDiscovery.ToJson(), result.Err))
    50  		return
    51  	}
    52  
    53  	go func() {
    54  		mlog.Debug(fmt.Sprintf("ClusterDiscoveryService ping writer started for %v", me.ClusterDiscovery.ToJson()))
    55  		ticker := time.NewTicker(DISCOVERY_SERVICE_WRITE_PING)
    56  		defer func() {
    57  			ticker.Stop()
    58  			if u := <-me.app.Srv.Store.ClusterDiscovery().Delete(&me.ClusterDiscovery); u.Err != nil {
    59  				mlog.Error(fmt.Sprintf("ClusterDiscoveryService failed to cleanup for %v with err=%v", me.ClusterDiscovery.ToJson(), u.Err))
    60  			}
    61  			mlog.Debug(fmt.Sprintf("ClusterDiscoveryService ping writer stopped for %v", me.ClusterDiscovery.ToJson()))
    62  		}()
    63  
    64  		for {
    65  			select {
    66  			case <-ticker.C:
    67  				if u := <-me.app.Srv.Store.ClusterDiscovery().SetLastPingAt(&me.ClusterDiscovery); u.Err != nil {
    68  					mlog.Error(fmt.Sprintf("ClusterDiscoveryService failed to write ping for %v with err=%v", me.ClusterDiscovery.ToJson(), u.Err))
    69  				}
    70  			case <-me.stop:
    71  				return
    72  			}
    73  		}
    74  	}()
    75  }
    76  
    77  func (me *ClusterDiscoveryService) Stop() {
    78  	me.stop <- true
    79  }
    80  
    81  func (a *App) IsLeader() bool {
    82  	if a.License() != nil && *a.Config().ClusterSettings.Enable && a.Cluster != nil {
    83  		return a.Cluster.IsLeader()
    84  	}
    85  	return true
    86  }
    87  
    88  func (a *App) GetClusterId() string {
    89  	if a.Cluster == nil {
    90  		return ""
    91  	}
    92  
    93  	return a.Cluster.GetClusterId()
    94  }