diff --git a/cube/cube-agent/src/agent/http.go b/cube/cube-agent/src/agent/http.go index f0e4002b3b79197ec4df99dd1c53986af9057a27..67687c4e42aa179bf506b72664031b2cd0c97bbf 100755 --- a/cube/cube-agent/src/agent/http.go +++ b/cube/cube-agent/src/agent/http.go @@ -39,7 +39,7 @@ func StartHttp(addr string) error { func handleRest(w http.ResponseWriter, r *http.Request) { var ( req_log string - status int32 + status int32 ) time_begin := time.Now() @@ -82,7 +82,7 @@ func handleRest(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, `{"success":"%v", "message":"wrong api", "method":"%s", "api":"%s", "api_list":"%v"}`, status, r.Method, api, key_list) - logex.Noticef(`%v, time=%v, status=%v`, + logex.Noticef(`%v, time=%v, status=%v`, req_log, time.Now().Sub(time_begin).Nanoseconds()/1000000, status) return } @@ -94,23 +94,23 @@ func handleRest(w http.ResponseWriter, r *http.Request) { s = fmt.Sprintf(`{"success":"%v", "message":"query ok", "data":%s}`, status, rst) } else { status = 255 - s = fmt.Sprintf(`{"success":"%v", "message":%v, "data":%s}`, + s = fmt.Sprintf(`{"success":"%v", "message":%v, "data":%s}`, status, quote(err.Error()), rst) } if isJsonDict(s) { fmt.Fprintln(w, s) - }else { + } else { logex.Fatalf("invalid json: %v", s) } if err == nil { - logex.Noticef(`%v, time=%v, status=%v, handle_log=%v`, - req_log, time.Now().Sub(time_begin).Nanoseconds()/1000000, + logex.Noticef(`%v, time=%v, status=%v, handle_log=%v`, + req_log, time.Now().Sub(time_begin).Nanoseconds()/1000000, status, quote(handle_log)) } else { - logex.Noticef(`%v, time=%v, status=%v, err=%v, handle_log=%v`, - req_log, time.Now().Sub(time_begin).Nanoseconds()/1000000, + logex.Noticef(`%v, time=%v, status=%v, err=%v, handle_log=%v`, + req_log, time.Now().Sub(time_begin).Nanoseconds()/1000000, status, quote(err.Error()), quote(handle_log)) } } @@ -126,9 +126,9 @@ func parseHttpKv(r *http.Request) map[string]string { m[k] = v[0] } } - + // allow passing hostname for debug - if _, ok := m["hostname"]; !ok{ + if _, ok := m["hostname"]; !ok { ip := r.RemoteAddr[:strings.Index(r.RemoteAddr, ":")] m["hostname"], _ = getHostname(ip) } diff --git a/cube/cube-agent/src/agent/http_get.go b/cube/cube-agent/src/agent/http_get.go index 967e6426ef895edfc8e9d3669d76a80ac8218030..66065cb2a0e31543ce2cf07207abfa6f5bff32f6 100755 --- a/cube/cube-agent/src/agent/http_get.go +++ b/cube/cube-agent/src/agent/http_get.go @@ -2,21 +2,21 @@ package agent import ( - "fmt" "encoding/json" + "fmt" ) func initGetHandlers() { getHandler = map[string]handlerFunc{ - "/agent/status": GetStatus, + "/agent/status": GetStatus, } } func GetStatus(subpath string, m map[string]string, b []byte) (string, string, error) { - b, err := json.Marshal(BUILTIN_STATUS); + b, err := json.Marshal(BUILTIN_STATUS) if err != nil { return quote(""), "", fmt.Errorf("json marshal failed, %v", err) - } + } return string(b), "", err -} \ No newline at end of file +} diff --git a/cube/cube-agent/src/agent/http_post.go b/cube/cube-agent/src/agent/http_post.go index 024ede44133b84abc1685557818f63c4af4c0f12..4ded0a37c033a85da5fdd4b5b9932f9559bec44f 100755 --- a/cube/cube-agent/src/agent/http_post.go +++ b/cube/cube-agent/src/agent/http_post.go @@ -2,14 +2,14 @@ package agent import ( - "fmt" "encoding/json" + "fmt" "github.com/Badangel/logex" ) func initPostHandlers() { postHandler = map[string]handlerFunc{ - "/agent/cmd": PostCmd, + "/agent/cmd": PostCmd, } } @@ -25,7 +25,7 @@ func PostCmd(subpath string, m map[string]string, b []byte) (string, string, err logex.Warningf("Another work with same token is doing. Token(%s)", work.Token()) return quote(""), "", fmt.Errorf("Another work with same key is doing.", err) } - + CmdWorkFilter.Store(work.Token(), true) err = work.DoWork() CmdWorkFilter.Delete(work.Token()) @@ -34,4 +34,4 @@ func PostCmd(subpath string, m map[string]string, b []byte) (string, string, err } return quote(""), "", err -} \ No newline at end of file +} diff --git a/cube/cube-agent/src/agent/util.go b/cube/cube-agent/src/agent/util.go index 22d36ff54a2d8670955f8fa66f358fa723ac7ccf..3a878e50a0a11bdb83649361f23190ae67b2c757 100644 --- a/cube/cube-agent/src/agent/util.go +++ b/cube/cube-agent/src/agent/util.go @@ -154,21 +154,21 @@ func GetLocalHostnameCmd() (hostname string, err error) { // quote quotes string for json output. eg: s="123", quote(s)=`"123"` func quote(s string) string { - return fmt.Sprintf("%q", s) + return fmt.Sprintf("%q", s) } // quoteb quotes byte array for json output. func quoteb(b []byte) string { - return quote(string(b)) + return quote(string(b)) } // quotea quotes string array for json output func quotea(a []string) string { - b, _ := json.Marshal(a) - return string(b) + b, _ := json.Marshal(a) + return string(b) } func isJsonDict(s string) bool { - var js map[string]interface{} - return json.Unmarshal([]byte(s), &js) == nil + var js map[string]interface{} + return json.Unmarshal([]byte(s), &js) == nil } diff --git a/cube/cube-agent/src/agent/work.go b/cube/cube-agent/src/agent/work.go index b9be00609a4212a1e6e0c4bf12cce7b4c44e787f..1017bf13effd62cde2857221e4d3ef8e1de0febc 100644 --- a/cube/cube-agent/src/agent/work.go +++ b/cube/cube-agent/src/agent/work.go @@ -417,7 +417,7 @@ func (work *Work) CheckToReload() bool { } func (work *Work) Reload() (err error) { - if (work.Port == "") { + if work.Port == "" { err = errors.New("Reload with invalid port.") return } @@ -544,8 +544,8 @@ func (work *Work) Check() (err error) { work.clearPatchData() work.clearPatchLink() master_host, master_port, _ := GetMaster(work.MasterAddress) - cmd := fmt.Sprintf("cd %s && export STRATEGY_DIR=%s && ./downloader -h %s -p %s -d %s -s %d", - work.DeployPath, work.DeployPath, master_host, master_port, work.DictName, work.ShardSeq) + cmd := fmt.Sprintf("cd %s && export STRATEGY_DIR=%s && ./downloader -h %s -p %s -d %s -s %d", + work.DeployPath, work.DeployPath, master_host, master_port, work.DictName, work.ShardSeq) _, _, err = RetryCmd(cmd, RETRY_TIMES) } @@ -632,7 +632,7 @@ func (work *Work) Pop() (err error) { func writeStateFile(dictName string, shardSeq int, service, state string) { stateFile := fmt.Sprintf(".state_%s_%d", dictName, shardSeq) if service != "" { - stateFile = stateFile + "_" + service; + stateFile = stateFile + "_" + service } cmd := fmt.Sprintf("echo '%s' > %s/state/%s", state, Dir, stateFile) @@ -644,7 +644,7 @@ func writeStateFile(dictName string, shardSeq int, service, state string) { func RemoveStateFile(dictName string, shardSeq int, service string) { stateFile := fmt.Sprintf(".state_%s_%d", dictName, shardSeq) if service != "" { - stateFile = stateFile + "_" + service; + stateFile = stateFile + "_" + service } cmd := fmt.Sprintf("rm -f %s/state/%s", Dir, stateFile) diff --git a/cube/cube-agent/src/agent/work_pool.go b/cube/cube-agent/src/agent/work_pool.go index 5fd3dc6154ed64caa2e4ecd8b6eeb8320e76f112..451b509af1aa8a7cce7e8f5c08bf6cb5f57de1cf 100644 --- a/cube/cube-agent/src/agent/work_pool.go +++ b/cube/cube-agent/src/agent/work_pool.go @@ -3,8 +3,8 @@ package agent import ( "errors" "fmt" - "sync/atomic" "sync" + "sync/atomic" ) type ( diff --git a/cube/cube-agent/src/cube-agent.go b/cube/cube-agent/src/cube-agent.go index ee8bc918c14b2c10aeaf32c8e2ef48bcbbfcb518..c044407da45bb1e19bcd98d61c47fe424de243ea 100644 --- a/cube/cube-agent/src/cube-agent.go +++ b/cube/cube-agent/src/cube-agent.go @@ -3,8 +3,8 @@ package main import ( "agent" "fmt" - "github.com/docopt/docopt-go" "github.com/Badangel/logex" + "github.com/docopt/docopt-go" "os" "path/filepath" "runtime"