github.com/mad-app/mattermost-server@v5.11.1+incompatible/store/sqlstore/cluster_discovery_store.go (about) 1 // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package sqlstore 5 6 import ( 7 "net/http" 8 9 "github.com/mattermost/mattermost-server/model" 10 "github.com/mattermost/mattermost-server/store" 11 ) 12 13 type sqlClusterDiscoveryStore struct { 14 SqlStore 15 } 16 17 func NewSqlClusterDiscoveryStore(sqlStore SqlStore) store.ClusterDiscoveryStore { 18 s := &sqlClusterDiscoveryStore{sqlStore} 19 20 for _, db := range sqlStore.GetAllConns() { 21 table := db.AddTableWithName(model.ClusterDiscovery{}, "ClusterDiscovery").SetKeys(false, "Id") 22 table.ColMap("Id").SetMaxSize(26) 23 table.ColMap("Type").SetMaxSize(64) 24 table.ColMap("ClusterName").SetMaxSize(64) 25 table.ColMap("Hostname").SetMaxSize(512) 26 } 27 28 return s 29 } 30 31 func (s sqlClusterDiscoveryStore) Save(ClusterDiscovery *model.ClusterDiscovery) store.StoreChannel { 32 return store.Do(func(result *store.StoreResult) { 33 ClusterDiscovery.PreSave() 34 if result.Err = ClusterDiscovery.IsValid(); result.Err != nil { 35 return 36 } 37 38 if err := s.GetMaster().Insert(ClusterDiscovery); err != nil { 39 result.Err = model.NewAppError("SqlClusterDiscoveryStore.Save", "store.sql_cluster_discovery.save.app_error", nil, err.Error(), http.StatusInternalServerError) 40 } 41 }) 42 } 43 44 func (s sqlClusterDiscoveryStore) Delete(ClusterDiscovery *model.ClusterDiscovery) store.StoreChannel { 45 return store.Do(func(result *store.StoreResult) { 46 result.Data = false 47 48 if count, err := s.GetMaster().SelectInt( 49 ` 50 DELETE 51 FROM 52 ClusterDiscovery 53 WHERE 54 Type = :Type 55 AND ClusterName = :ClusterName 56 AND Hostname = :Hostname 57 `, 58 map[string]interface{}{ 59 "Type": ClusterDiscovery.Type, 60 "ClusterName": ClusterDiscovery.ClusterName, 61 "Hostname": ClusterDiscovery.Hostname, 62 }, 63 ); err != nil { 64 result.Err = model.NewAppError("SqlClusterDiscoveryStore.Delete", "store.sql_cluster_discovery.delete.app_error", nil, err.Error(), http.StatusInternalServerError) 65 } else { 66 if count > 0 { 67 result.Data = true 68 } 69 } 70 }) 71 } 72 73 func (s sqlClusterDiscoveryStore) Exists(ClusterDiscovery *model.ClusterDiscovery) store.StoreChannel { 74 return store.Do(func(result *store.StoreResult) { 75 result.Data = false 76 77 if count, err := s.GetMaster().SelectInt( 78 ` 79 SELECT 80 COUNT(*) 81 FROM 82 ClusterDiscovery 83 WHERE 84 Type = :Type 85 AND ClusterName = :ClusterName 86 AND Hostname = :Hostname 87 `, 88 map[string]interface{}{ 89 "Type": ClusterDiscovery.Type, 90 "ClusterName": ClusterDiscovery.ClusterName, 91 "Hostname": ClusterDiscovery.Hostname, 92 }, 93 ); err != nil { 94 result.Err = model.NewAppError("SqlClusterDiscoveryStore.Exists", "store.sql_cluster_discovery.exists.app_error", nil, err.Error(), http.StatusInternalServerError) 95 } else { 96 if count > 0 { 97 result.Data = true 98 } 99 } 100 }) 101 } 102 103 func (s sqlClusterDiscoveryStore) GetAll(ClusterDiscoveryType, clusterName string) store.StoreChannel { 104 return store.Do(func(result *store.StoreResult) { 105 lastPingAt := model.GetMillis() - model.CDS_OFFLINE_AFTER_MILLIS 106 107 var list []*model.ClusterDiscovery 108 if _, err := s.GetMaster().Select( 109 &list, 110 ` 111 SELECT 112 * 113 FROM 114 ClusterDiscovery 115 WHERE 116 Type = :ClusterDiscoveryType 117 AND ClusterName = :ClusterName 118 AND LastPingAt > :LastPingAt 119 `, 120 map[string]interface{}{ 121 "ClusterDiscoveryType": ClusterDiscoveryType, 122 "ClusterName": clusterName, 123 "LastPingAt": lastPingAt, 124 }, 125 ); err != nil { 126 result.Err = model.NewAppError("SqlClusterDiscoveryStore.GetAllForType", "store.sql_cluster_discovery.get_all.app_error", nil, err.Error(), http.StatusInternalServerError) 127 } else { 128 result.Data = list 129 } 130 }) 131 } 132 133 func (s sqlClusterDiscoveryStore) SetLastPingAt(ClusterDiscovery *model.ClusterDiscovery) store.StoreChannel { 134 return store.Do(func(result *store.StoreResult) { 135 if _, err := s.GetMaster().Exec( 136 ` 137 UPDATE ClusterDiscovery 138 SET 139 LastPingAt = :LastPingAt 140 WHERE 141 Type = :Type 142 AND ClusterName = :ClusterName 143 AND Hostname = :Hostname 144 `, 145 map[string]interface{}{ 146 "LastPingAt": model.GetMillis(), 147 "Type": ClusterDiscovery.Type, 148 "ClusterName": ClusterDiscovery.ClusterName, 149 "Hostname": ClusterDiscovery.Hostname, 150 }, 151 ); err != nil { 152 result.Err = model.NewAppError("SqlClusterDiscoveryStore.GetAllForType", "store.sql_cluster_discovery.set_last_ping.app_error", nil, err.Error(), http.StatusInternalServerError) 153 } 154 }) 155 } 156 157 func (s sqlClusterDiscoveryStore) Cleanup() store.StoreChannel { 158 return store.Do(func(result *store.StoreResult) { 159 if _, err := s.GetMaster().Exec( 160 ` 161 DELETE FROM ClusterDiscovery 162 WHERE 163 LastPingAt < :LastPingAt 164 `, 165 map[string]interface{}{ 166 "LastPingAt": model.GetMillis() - model.CDS_OFFLINE_AFTER_MILLIS, 167 }, 168 ); err != nil { 169 result.Err = model.NewAppError("SqlClusterDiscoveryStore.Save", "store.sql_cluster_discovery.cleanup.app_error", nil, err.Error(), http.StatusInternalServerError) 170 } 171 }) 172 }