github.com/vnforks/kid/v5@v5.22.1-0.20200408055009-b89d99c65676/store/sqlstore/cluster_discovery_store.go (about) 1 // Copyright (c) 2015-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/vnforks/kid/v5/model" 10 "github.com/vnforks/kid/v5/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) *model.AppError { 32 ClusterDiscovery.PreSave() 33 if err := ClusterDiscovery.IsValid(); err != nil { 34 return err 35 } 36 37 if err := s.GetMaster().Insert(ClusterDiscovery); err != nil { 38 return model.NewAppError("SqlClusterDiscoveryStore.Save", "store.sql_cluster_discovery.save.app_error", nil, err.Error(), http.StatusInternalServerError) 39 } 40 return nil 41 } 42 43 func (s sqlClusterDiscoveryStore) Delete(ClusterDiscovery *model.ClusterDiscovery) (bool, *model.AppError) { 44 count, err := s.GetMaster().SelectInt( 45 ` 46 DELETE 47 FROM 48 ClusterDiscovery 49 WHERE 50 Type = :Type 51 AND ClusterName = :ClusterName 52 AND Hostname = :Hostname 53 `, 54 map[string]interface{}{ 55 "Type": ClusterDiscovery.Type, 56 "ClusterName": ClusterDiscovery.ClusterName, 57 "Hostname": ClusterDiscovery.Hostname, 58 }, 59 ) 60 if err != nil { 61 return false, model.NewAppError("SqlClusterDiscoveryStore.Delete", "store.sql_cluster_discovery.delete.app_error", nil, err.Error(), http.StatusInternalServerError) 62 } 63 if count == 0 { 64 return false, nil 65 } 66 return true, nil 67 } 68 69 func (s sqlClusterDiscoveryStore) Exists(ClusterDiscovery *model.ClusterDiscovery) (bool, *model.AppError) { 70 count, err := s.GetMaster().SelectInt( 71 ` 72 SELECT 73 COUNT(*) 74 FROM 75 ClusterDiscovery 76 WHERE 77 Type = :Type 78 AND ClusterName = :ClusterName 79 AND Hostname = :Hostname 80 `, 81 map[string]interface{}{ 82 "Type": ClusterDiscovery.Type, 83 "ClusterName": ClusterDiscovery.ClusterName, 84 "Hostname": ClusterDiscovery.Hostname, 85 }, 86 ) 87 if err != nil { 88 return false, model.NewAppError("SqlClusterDiscoveryStore.Exists", "store.sql_cluster_discovery.exists.app_error", nil, err.Error(), http.StatusInternalServerError) 89 } 90 if count == 0 { 91 return false, nil 92 } 93 return true, nil 94 } 95 96 func (s sqlClusterDiscoveryStore) GetAll(ClusterDiscoveryType, clusterName string) ([]*model.ClusterDiscovery, *model.AppError) { 97 lastPingAt := model.GetMillis() - model.CDS_OFFLINE_AFTER_MILLIS 98 99 var list []*model.ClusterDiscovery 100 if _, err := s.GetMaster().Select( 101 &list, 102 ` 103 SELECT 104 * 105 FROM 106 ClusterDiscovery 107 WHERE 108 Type = :ClusterDiscoveryType 109 AND ClusterName = :ClusterName 110 AND LastPingAt > :LastPingAt 111 `, 112 map[string]interface{}{ 113 "ClusterDiscoveryType": ClusterDiscoveryType, 114 "ClusterName": clusterName, 115 "LastPingAt": lastPingAt, 116 }, 117 ); err != nil { 118 return nil, model.NewAppError("SqlClusterDiscoveryStore.GetAllForType", "store.sql_cluster_discovery.get_all.app_error", nil, err.Error(), http.StatusInternalServerError) 119 } 120 return list, nil 121 } 122 123 func (s sqlClusterDiscoveryStore) SetLastPingAt(ClusterDiscovery *model.ClusterDiscovery) *model.AppError { 124 if _, err := s.GetMaster().Exec( 125 ` 126 UPDATE ClusterDiscovery 127 SET 128 LastPingAt = :LastPingAt 129 WHERE 130 Type = :Type 131 AND ClusterName = :ClusterName 132 AND Hostname = :Hostname 133 `, 134 map[string]interface{}{ 135 "LastPingAt": model.GetMillis(), 136 "Type": ClusterDiscovery.Type, 137 "ClusterName": ClusterDiscovery.ClusterName, 138 "Hostname": ClusterDiscovery.Hostname, 139 }, 140 ); err != nil { 141 return model.NewAppError("SqlClusterDiscoveryStore.GetAllForType", "store.sql_cluster_discovery.set_last_ping.app_error", nil, err.Error(), http.StatusInternalServerError) 142 } 143 return nil 144 } 145 146 func (s sqlClusterDiscoveryStore) Cleanup() *model.AppError { 147 if _, err := s.GetMaster().Exec( 148 ` 149 DELETE FROM ClusterDiscovery 150 WHERE 151 LastPingAt < :LastPingAt 152 `, 153 map[string]interface{}{ 154 "LastPingAt": model.GetMillis() - model.CDS_OFFLINE_AFTER_MILLIS, 155 }, 156 ); err != nil { 157 return model.NewAppError("SqlClusterDiscoveryStore.Save", "store.sql_cluster_discovery.cleanup.app_error", nil, err.Error(), http.StatusInternalServerError) 158 } 159 return nil 160 }