cube-agent.go 2.1 KB
Newer Older
Y
yangrui07 已提交
1 2 3 4 5 6
package main

import (
	"agent"
	"fmt"
	"github.com/Badangel/logex"
Y
yangrui07 已提交
7
	"github.com/docopt/docopt-go"
Y
yangrui07 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
	"os"
	"path/filepath"
	"runtime"
	"strconv"
)

func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())

	agent.Dir, _ = filepath.Abs(filepath.Dir(os.Args[0]))
	usage := fmt.Sprintf(`Usage: ./m_master [options]

Options:
  -n WORKERNUM    set worker num.
  -q QUEUENUM     set queue num.
  -P LISTEN_PORT  agent listen port

Log options:
  -l LOG_LEVEL      set log level, values: 0,1,2,4,8,16. [default: 16]
  --log_dir=DIR     set log output dir. [default: ./log]
  --log_name=NAME   set log name. [default: m_agent]`, agent.Dir)

	opts, err := docopt.Parse(usage, nil, true, "Cube Agent Checker 1.0.0", false)
	if err != nil {
		fmt.Println("ERROR:", err)
		os.Exit(1)
	}

	log_level, _ := strconv.Atoi(opts["-l"].(string))
	log_name := opts["--log_name"].(string)
	log_dir := opts["--log_dir"].(string)
	logex.SetLevel(getLogLevel(log_level))
	if err := logex.SetUpFileLogger(log_dir, log_name, nil); err != nil {
		fmt.Println("ERROR:", err)
	}

	logex.Notice("--- NEW SESSION -------------------------")
	logex.Notice(">>> log_level:", log_level)

	agent.WorkerNum = 10
	if opts["-n"] != nil {
		n, err := strconv.Atoi(opts["-n"].(string))
		if err == nil {
			agent.WorkerNum = n
		}
	}

	agent.QueueCapacity = 20
	if opts["-q"] != nil {
		q, err := strconv.Atoi(opts["-q"].(string))
		if err == nil {
			agent.QueueCapacity = int32(q)
		}
	}

	agent.CmdWorkPool = agent.NewWorkPool(agent.WorkerNum, agent.QueueCapacity)

	if opts["-P"] == nil {
		logex.Fatalf("ERROR: -P LISTEN PORT must be set!")
		os.Exit(255)
	}

	agentPort := opts["-P"].(string)
	logex.Notice(">>> starting server...")
	addr := ":" + agentPort

	if agent.StartHttp(addr) != nil {
		logex.Noticef("cant start http(addr=%v). quit.", addr)
		os.Exit(0)
	}
}

func getLogLevel(log_level int) logex.Level {
	switch log_level {
	case 16:
		return logex.DEBUG
	case 8:
		return logex.TRACE
	case 4:
		return logex.NOTICE
	case 2:
		return logex.WARNING
	case 1:
		return logex.FATAL
	case 0:
		return logex.NONE
	}
	return logex.DEBUG
}