github.com/psyb0t/mattermost-server@v4.6.1-0.20180125161845-5503a1351abf+incompatible/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  	l4g "github.com/alecthomas/log4go"
    11  	"github.com/mattermost/mattermost-server/model"
    12  	"github.com/mattermost/mattermost-server/utils"
    13  )
    14  
    15  const (
    16  	DISCOVERY_SERVICE_WRITE_PING = 60 * time.Second
    17  )
    18  
    19  type ClusterDiscoveryService struct {
    20  	model.ClusterDiscovery
    21  	app  *App
    22  	stop chan bool
    23  }
    24  
    25  func (a *App) NewClusterDiscoveryService() *ClusterDiscoveryService {
    26  	ds := &ClusterDiscoveryService{
    27  		ClusterDiscovery: model.ClusterDiscovery{},
    28  		app:              a,
    29  		stop:             make(chan bool),
    30  	}
    31  
    32  	return ds
    33  }
    34  
    35  func (me *ClusterDiscoveryService) Start() {
    36  
    37  	<-me.app.Srv.Store.ClusterDiscovery().Cleanup()
    38  
    39  	if cresult := <-me.app.Srv.Store.ClusterDiscovery().Exists(&me.ClusterDiscovery); cresult.Err != nil {
    40  		l4g.Error(fmt.Sprintf("ClusterDiscoveryService failed to check if row exists for %v with err=%v", me.ClusterDiscovery.ToJson(), cresult.Err))
    41  	} else {
    42  		if cresult.Data.(bool) {
    43  			if u := <-me.app.Srv.Store.ClusterDiscovery().Delete(&me.ClusterDiscovery); u.Err != nil {
    44  				l4g.Error(fmt.Sprintf("ClusterDiscoveryService failed to start clean for %v with err=%v", me.ClusterDiscovery.ToJson(), u.Err))
    45  			}
    46  		}
    47  	}
    48  
    49  	if result := <-me.app.Srv.Store.ClusterDiscovery().Save(&me.ClusterDiscovery); result.Err != nil {
    50  		l4g.Error(fmt.Sprintf("ClusterDiscoveryService failed to save for %v with err=%v", me.ClusterDiscovery.ToJson(), result.Err))
    51  		return
    52  	}
    53  
    54  	go func() {
    55  		l4g.Debug(fmt.Sprintf("ClusterDiscoveryService ping writer started for %v", me.ClusterDiscovery.ToJson()))
    56  		ticker := time.NewTicker(DISCOVERY_SERVICE_WRITE_PING)
    57  		defer func() {
    58  			ticker.Stop()
    59  			if u := <-me.app.Srv.Store.ClusterDiscovery().Delete(&me.ClusterDiscovery); u.Err != nil {
    60  				l4g.Error(fmt.Sprintf("ClusterDiscoveryService failed to cleanup for %v with err=%v", me.ClusterDiscovery.ToJson(), u.Err))
    61  			}
    62  			l4g.Debug(fmt.Sprintf("ClusterDiscoveryService ping writer stopped for %v", me.ClusterDiscovery.ToJson()))
    63  		}()
    64  
    65  		for {
    66  			select {
    67  			case <-ticker.C:
    68  				if u := <-me.app.Srv.Store.ClusterDiscovery().SetLastPingAt(&me.ClusterDiscovery); u.Err != nil {
    69  					l4g.Error(fmt.Sprintf("ClusterDiscoveryService failed to write ping for %v with err=%v", me.ClusterDiscovery.ToJson(), u.Err))
    70  				}
    71  			case <-me.stop:
    72  				return
    73  			}
    74  		}
    75  	}()
    76  }
    77  
    78  func (me *ClusterDiscoveryService) Stop() {
    79  	me.stop <- true
    80  }
    81  
    82  func (a *App) IsLeader() bool {
    83  	if utils.IsLicensed() && *a.Config().ClusterSettings.Enable && a.Cluster != nil {
    84  		return a.Cluster.IsLeader()
    85  	} else {
    86  		return true
    87  	}
    88  }