示例#1
0
func (this *SQLServer) AddObserver(observer Observer) {
	this.obsevers = append(this.obsevers, observer)

	zkm.AddObserver(zkm.MakeDatabaseGroup(DB_DRIVER_POSTGRES, observer.Group()), this)

	//request when register
	this.requestDatabaseList(observer)
}
示例#2
0
func (this *SQLServer) requestDatabaseList(observer Observer) {
	defer logger.CatchException()

	for {
		sl, _, err := zkm.ChildrenW(zkm.MakeDatabaseGroup(DB_DRIVER_POSTGRES, observer.Group()))
		if err != nil {
			logger.Errorf("zkm.ChildrenW failed! err=%v", err)
			time.Sleep(5 * time.Second)
			continue
		}

		var master []zkm.DBServerInfo
		var slaves []zkm.DBServerInfo
		var node zkm.DBServerInfo

		for _, s := range sl {
			if err := json.Unmarshal([]byte(s), &node); err != nil {
				logger.Errorf("requestDatabaseList failed! parse.err=%v", err)
				continue
			}

			if len(node.Slaveof) == 0 {
				master = append(master, node)
			} else {
				slaves = append(slaves, node)
			}
		}

		this.Lock()
		defer this.Unlock()
		this.master = master
		this.slaves = slaves

		observer.OnUpdateDatabaseList(master, slaves)
		return
	}
}