Exemplo n.º 1
0
func syncData2Slave(node *models.Node, data interface{}, dataVer *models.DataVersion, opUserKey string) error {
	kind := ""
	switch data.(type) {
	case *models.User:
		kind = NODE_REQUEST_SYNC_TYPE_USER
	case *models.App:
		kind = NODE_REQUEST_SYNC_TYPE_APP
	case *models.WebHook:
		kind = NODE_REQUEST_SYNC_TYPE_WEBHOOK
	case *models.Config:
		kind = NODE_REQUEST_SYNC_TYPE_CONFIG
	case *models.Node:
		kind = NODE_REQUEST_SYNC_TYPE_NODE
	case *cloneData:
		kind = NODE_REQUEST_SYNC_TYPE_CLONE
	default:
		log.Panicln("unknown node data sync type: ", reflect.TypeOf(data))
	}

	bs, _ := json.Marshal(data)
	syncDataString, _ := json.Marshal(&syncDataT{
		DataVersion: dataVer,
		Kind:        kind,
		Data:        string(bs),
		OpUserKey:   opUserKey,
	})

	reqData := nodeRequestDataT{
		Auth: nodeAuthString,
		Data: string(syncDataString),
	}
	_, err := nodeRequest(node.NodeURL, NODE_REQUEST_TYPE_SYNCSLAVE, reqData)

	if err == nil && kind != NODE_REQUEST_SYNC_TYPE_NODE {
		// update slave data version here
		dataVersionStr, _ := json.Marshal(dataVer)
		memConfMux.Lock()
		node.DataVersion = dataVer
		node.DataVersionStr = string(dataVersionStr)
		node.LastCheckUTC = utils.GetNowSecond()
		memConfMux.Unlock()
		updateNodeDataVersion(nil, node, dataVer)
	}

	return err
}
Exemplo n.º 2
0
func updateNodeDataVersion(s *models.Session, node *models.Node, ver *models.DataVersion) (err error) {
	var _s *models.Session
	var bs []byte

	if s == nil {
		_s = models.NewSession()
		defer _s.Close()
		if err = _s.Begin(); err != nil {
			goto ERROR
		}
	} else {
		_s = s
	}

	bs, _ = json.Marshal(ver)
	node.DataVersion = ver
	node.DataVersionStr = string(bs)
	if err = models.UpdateDBModel(_s, node); err != nil {
		goto ERROR
	}

	if node.URL == conf.ClientAddr {
		if err = models.UpdateDataVersion(_s, ver); err != nil {
			goto ERROR
		}
	}

	if s != nil {
		return
	}

	if err = _s.Commit(); err != nil {
		goto ERROR
	}

	return
ERROR:
	if s == nil {
		_s.Rollback()
	}

	return
}