http_post.go 948 字节
Newer Older
Y
yangrui07 已提交
1 2 3 4 5
//-*- coding:utf-8; indent-tabs-mode:nil; -*-
package agent

import (
	"encoding/json"
Y
yangrui07 已提交
6
	"fmt"
Y
yangrui07 已提交
7 8 9 10 11
	"github.com/Badangel/logex"
)

func initPostHandlers() {
	postHandler = map[string]handlerFunc{
Y
yangrui07 已提交
12
		"/agent/cmd": PostCmd,
Y
yangrui07 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
	}
}

func PostCmd(subpath string, m map[string]string, b []byte) (string, string, error) {
	var work Work
	err := json.Unmarshal(b, &work)
	if err != nil {
		logex.Warningf("Unmarshal from %s error (+%v)", string(b), err)
		return quote(""), "", fmt.Errorf("Work json unmarshal work failed, %v", err)
	}

	if _, ok := CmdWorkFilter.Load(work.Token()); ok {
		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)
	}
Y
yangrui07 已提交
28

Y
yangrui07 已提交
29 30 31 32 33 34 35 36
	CmdWorkFilter.Store(work.Token(), true)
	err = work.DoWork()
	CmdWorkFilter.Delete(work.Token())
	if err != nil {
		return quote(""), "", fmt.Errorf("Do work failed.", err)
	}

	return quote(""), "", err
Y
yangrui07 已提交
37
}