func HandleNodeCommand(cmd *cobra.Command, args []string) { var ( name string err tree_lib.TreeError ) err.From = tree_lib.FROM_HANDLE_NODE_COMMAND name, err.Err = cmd.Flags().GetString("set-name") if !err.IsNull() { tree_log.Error(err.From, err.Error()) } // If we have set-name flag then we just setting current_node in database and exiting if len(name) > 0 { tree_db.Set(tree_db.DB_RANDOM, []byte("current_node"), []byte(name)) return } daemon := false daemon, err.Err = cmd.Flags().GetBool("daemon") if !err.IsNull() { tree_log.Error(err.From, err.Error()) return } if daemon { cmd := exec.Command("/bin/sh", "-c", fmt.Sprintf("%s node > %s 2>&1 &", os.Args[0], tree_log.LogFile)) err.Err = cmd.Run() if !err.IsNull() { log.Fatal(err.Err) } return } name, err.Err = cmd.Flags().GetString("name") if !err.IsNull() { tree_log.Error(err.From, err.Error()) return } if len(name) == 0 { current_node_byte, err := tree_db.Get(tree_db.DB_RANDOM, []byte("current_node")) if !err.IsNull() { tree_log.Error(err.From, "Getting current node name from Random database, ", err.Error()) return } if len(current_node_byte) == 0 { fmt.Println("Name is important for the first time run") return } } else { err = tree_node.SetCurrentNode(name) if !err.IsNull() { tree_log.Error(err.From, err.Error()) return } } tree_node.Start() }
// Adding new custom event handler // if event doesn't exists it will be created func ON(name string, handlers ...Handler) (err tree_lib.TreeError) { var ( handlers_data []byte event_byte []byte old_handlers []Handler ) event_byte, err = tree_db.Get(tree_db.DB_EVENT, []byte(name)) if !err.IsNull() { return } if len(event_byte) > 0 { err.Err = ffjson.Unmarshal(event_byte, &old_handlers) if !err.IsNull() { return } } old_handlers = append(old_handlers, handlers...) handlers_data, err.Err = ffjson.Marshal(old_handlers) err = tree_db.Set(tree_db.DB_EVENT, []byte(name), handlers_data) return }
func DBFromConfig() { var ( err tree_lib.TreeError prev_prime = int64(1) ) err.From = tree_lib.FROM_DB_FROM_CONFIG for n, nf := range GLOBAL_CONFIG.TreeNode { nf.Value = tree_lib.NextPrimeNumber(prev_prime) prev_prime = nf.Value err = tree_db.SetNodeInfo(n, nf) if !err.IsNull() { tree_log.Error(err.From, err.Error()) } } // After having All nodes information now we can set related things for every node for n, _ := range GLOBAL_CONFIG.TreeNode { // Setting relations for every Node err = tree_db.SetRelations(n) if !err.IsNull() { tree_log.Error(err.From, err.Error()) } // Setting Groups with node lists in Group database err = tree_db.AddNodeToHisGroups(n) if !err.IsNull() { tree_log.Error(err.From, err.Error()) } // Setting Tags with node lists in Group database err = tree_db.AddNodeToHisTags(n) if !err.IsNull() { tree_log.Error(err.From, err.Error()) } } // Setting Balancers for b, b_conf := range GLOBAL_CONFIG.Balancer { var b_data []byte b_data, err.Err = ffjson.Marshal(b_conf) if !err.IsNull() { tree_log.Error(err.From, "Error encoding balancer config", b, " -> ", err.Error()) continue } err = tree_db.Set(tree_db.DB_BALANCER, []byte(b), b_data) if !err.IsNull() { tree_log.Error(err.From, "Error setting balancer config", b, " -> ", err.Error()) } } // Setting Registry for r, r_conf := range GLOBAL_CONFIG.Registry { var r_data []byte r_data, err.Err = ffjson.Marshal(r_conf) if !err.IsNull() { tree_log.Error(err.From, "Error encoding registry config", r, " -> ", err.Error()) continue } err = tree_db.Set(tree_db.DB_REGISTRY, []byte(r), r_data) if !err.IsNull() { tree_log.Error(err.From, "Error setting registry config", r, " -> ", err.Error()) } } }
func SetCurrentNode(name string) (err tree_lib.TreeError) { err.From = tree_lib.FROM_SET_CURRENT_NODE err = tree_db.Set(tree_db.DB_RANDOM, []byte("current_node"), []byte(name)) return }