提交 53f88865 编写于 作者: Y yangrui07

cube agent code format

上级 049013ee
......@@ -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)
}
......
......@@ -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
}
......@@ -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
}
......@@ -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
}
......@@ -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)
......
......@@ -3,8 +3,8 @@ package agent
import (
"errors"
"fmt"
"sync/atomic"
"sync"
"sync/atomic"
)
type (
......
......@@ -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"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册