提交 985133f1 编写于 作者: Y yangrui07

cube agent code format

上级 3da70904
...@@ -39,7 +39,7 @@ func StartHttp(addr string) error { ...@@ -39,7 +39,7 @@ func StartHttp(addr string) error {
func handleRest(w http.ResponseWriter, r *http.Request) { func handleRest(w http.ResponseWriter, r *http.Request) {
var ( var (
req_log string req_log string
status int32 status int32
) )
time_begin := time.Now() time_begin := time.Now()
...@@ -82,7 +82,7 @@ func handleRest(w http.ResponseWriter, r *http.Request) { ...@@ -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"}`, fmt.Fprintf(w, `{"success":"%v", "message":"wrong api", "method":"%s", "api":"%s", "api_list":"%v"}`,
status, r.Method, api, key_list) 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) req_log, time.Now().Sub(time_begin).Nanoseconds()/1000000, status)
return return
} }
...@@ -94,23 +94,23 @@ func handleRest(w http.ResponseWriter, r *http.Request) { ...@@ -94,23 +94,23 @@ func handleRest(w http.ResponseWriter, r *http.Request) {
s = fmt.Sprintf(`{"success":"%v", "message":"query ok", "data":%s}`, status, rst) s = fmt.Sprintf(`{"success":"%v", "message":"query ok", "data":%s}`, status, rst)
} else { } else {
status = 255 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) status, quote(err.Error()), rst)
} }
if isJsonDict(s) { if isJsonDict(s) {
fmt.Fprintln(w, s) fmt.Fprintln(w, s)
}else { } else {
logex.Fatalf("invalid json: %v", s) logex.Fatalf("invalid json: %v", s)
} }
if err == nil { if err == nil {
logex.Noticef(`%v, time=%v, status=%v, handle_log=%v`, logex.Noticef(`%v, time=%v, status=%v, handle_log=%v`,
req_log, time.Now().Sub(time_begin).Nanoseconds()/1000000, req_log, time.Now().Sub(time_begin).Nanoseconds()/1000000,
status, quote(handle_log)) status, quote(handle_log))
} else { } else {
logex.Noticef(`%v, time=%v, status=%v, err=%v, handle_log=%v`, logex.Noticef(`%v, time=%v, status=%v, err=%v, handle_log=%v`,
req_log, time.Now().Sub(time_begin).Nanoseconds()/1000000, req_log, time.Now().Sub(time_begin).Nanoseconds()/1000000,
status, quote(err.Error()), quote(handle_log)) status, quote(err.Error()), quote(handle_log))
} }
} }
...@@ -126,9 +126,9 @@ func parseHttpKv(r *http.Request) map[string]string { ...@@ -126,9 +126,9 @@ func parseHttpKv(r *http.Request) map[string]string {
m[k] = v[0] m[k] = v[0]
} }
} }
// allow passing hostname for debug // allow passing hostname for debug
if _, ok := m["hostname"]; !ok{ if _, ok := m["hostname"]; !ok {
ip := r.RemoteAddr[:strings.Index(r.RemoteAddr, ":")] ip := r.RemoteAddr[:strings.Index(r.RemoteAddr, ":")]
m["hostname"], _ = getHostname(ip) m["hostname"], _ = getHostname(ip)
} }
......
...@@ -2,21 +2,21 @@ ...@@ -2,21 +2,21 @@
package agent package agent
import ( import (
"fmt"
"encoding/json" "encoding/json"
"fmt"
) )
func initGetHandlers() { func initGetHandlers() {
getHandler = map[string]handlerFunc{ getHandler = map[string]handlerFunc{
"/agent/status": GetStatus, "/agent/status": GetStatus,
} }
} }
func GetStatus(subpath string, m map[string]string, b []byte) (string, string, error) { 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 { if err != nil {
return quote(""), "", fmt.Errorf("json marshal failed, %v", err) return quote(""), "", fmt.Errorf("json marshal failed, %v", err)
} }
return string(b), "", err return string(b), "", err
} }
\ No newline at end of file
...@@ -2,14 +2,14 @@ ...@@ -2,14 +2,14 @@
package agent package agent
import ( import (
"fmt"
"encoding/json" "encoding/json"
"fmt"
"github.com/Badangel/logex" "github.com/Badangel/logex"
) )
func initPostHandlers() { func initPostHandlers() {
postHandler = map[string]handlerFunc{ 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 ...@@ -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()) 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) return quote(""), "", fmt.Errorf("Another work with same key is doing.", err)
} }
CmdWorkFilter.Store(work.Token(), true) CmdWorkFilter.Store(work.Token(), true)
err = work.DoWork() err = work.DoWork()
CmdWorkFilter.Delete(work.Token()) CmdWorkFilter.Delete(work.Token())
...@@ -34,4 +34,4 @@ func PostCmd(subpath string, m map[string]string, b []byte) (string, string, err ...@@ -34,4 +34,4 @@ func PostCmd(subpath string, m map[string]string, b []byte) (string, string, err
} }
return quote(""), "", err return quote(""), "", err
} }
\ No newline at end of file
...@@ -154,21 +154,21 @@ func GetLocalHostnameCmd() (hostname string, err error) { ...@@ -154,21 +154,21 @@ func GetLocalHostnameCmd() (hostname string, err error) {
// quote quotes string for json output. eg: s="123", quote(s)=`"123"` // quote quotes string for json output. eg: s="123", quote(s)=`"123"`
func quote(s string) string { func quote(s string) string {
return fmt.Sprintf("%q", s) return fmt.Sprintf("%q", s)
} }
// quoteb quotes byte array for json output. // quoteb quotes byte array for json output.
func quoteb(b []byte) string { func quoteb(b []byte) string {
return quote(string(b)) return quote(string(b))
} }
// quotea quotes string array for json output // quotea quotes string array for json output
func quotea(a []string) string { func quotea(a []string) string {
b, _ := json.Marshal(a) b, _ := json.Marshal(a)
return string(b) return string(b)
} }
func isJsonDict(s string) bool { func isJsonDict(s string) bool {
var js map[string]interface{} var js map[string]interface{}
return json.Unmarshal([]byte(s), &js) == nil return json.Unmarshal([]byte(s), &js) == nil
} }
...@@ -417,7 +417,7 @@ func (work *Work) CheckToReload() bool { ...@@ -417,7 +417,7 @@ func (work *Work) CheckToReload() bool {
} }
func (work *Work) Reload() (err error) { func (work *Work) Reload() (err error) {
if (work.Port == "") { if work.Port == "" {
err = errors.New("Reload with invalid port.") err = errors.New("Reload with invalid port.")
return return
} }
...@@ -544,8 +544,8 @@ func (work *Work) Check() (err error) { ...@@ -544,8 +544,8 @@ func (work *Work) Check() (err error) {
work.clearPatchData() work.clearPatchData()
work.clearPatchLink() work.clearPatchLink()
master_host, master_port, _ := GetMaster(work.MasterAddress) 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", 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) work.DeployPath, work.DeployPath, master_host, master_port, work.DictName, work.ShardSeq)
_, _, err = RetryCmd(cmd, RETRY_TIMES) _, _, err = RetryCmd(cmd, RETRY_TIMES)
} }
...@@ -632,7 +632,7 @@ func (work *Work) Pop() (err error) { ...@@ -632,7 +632,7 @@ func (work *Work) Pop() (err error) {
func writeStateFile(dictName string, shardSeq int, service, state string) { func writeStateFile(dictName string, shardSeq int, service, state string) {
stateFile := fmt.Sprintf(".state_%s_%d", dictName, shardSeq) stateFile := fmt.Sprintf(".state_%s_%d", dictName, shardSeq)
if service != "" { if service != "" {
stateFile = stateFile + "_" + service; stateFile = stateFile + "_" + service
} }
cmd := fmt.Sprintf("echo '%s' > %s/state/%s", state, Dir, stateFile) cmd := fmt.Sprintf("echo '%s' > %s/state/%s", state, Dir, stateFile)
...@@ -644,7 +644,7 @@ func writeStateFile(dictName string, shardSeq int, service, state string) { ...@@ -644,7 +644,7 @@ func writeStateFile(dictName string, shardSeq int, service, state string) {
func RemoveStateFile(dictName string, shardSeq int, service string) { func RemoveStateFile(dictName string, shardSeq int, service string) {
stateFile := fmt.Sprintf(".state_%s_%d", dictName, shardSeq) stateFile := fmt.Sprintf(".state_%s_%d", dictName, shardSeq)
if service != "" { if service != "" {
stateFile = stateFile + "_" + service; stateFile = stateFile + "_" + service
} }
cmd := fmt.Sprintf("rm -f %s/state/%s", Dir, stateFile) cmd := fmt.Sprintf("rm -f %s/state/%s", Dir, stateFile)
......
...@@ -3,8 +3,8 @@ package agent ...@@ -3,8 +3,8 @@ package agent
import ( import (
"errors" "errors"
"fmt" "fmt"
"sync/atomic"
"sync" "sync"
"sync/atomic"
) )
type ( type (
......
...@@ -3,8 +3,8 @@ package main ...@@ -3,8 +3,8 @@ package main
import ( import (
"agent" "agent"
"fmt" "fmt"
"github.com/docopt/docopt-go"
"github.com/Badangel/logex" "github.com/Badangel/logex"
"github.com/docopt/docopt-go"
"os" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册