Example #1
0
func (p *ScaleProcessor) Process(img *img4g.Image) error {
	l4g.Debug("process scale")
	var err error
	tran := cat.Instance().NewTransaction(Image, "Scale")
	defer func() {
		tran.SetStatus(err)
		tran.Complete()
	}()
	err = img.Resize(p.Width, p.Height)
	return err
}
Example #2
0
func (handler *Handler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
	Cat := cat.Instance()
	handler.ChainBuilder = &ProcChainBuilder{Cat}
	tran := Cat.NewTransaction("Image.Request", "Request")
	var (
		ok  bool
		err error
	)
	defer func() {
		p := recover()
		if p != nil {
			l4g.Error(p)
			Cat.LogPanic(p)
		}
		if !ok {
			tran.SetStatus("1")
		}
		if err != nil {
			l4g.Error(err)
			Cat.LogError(err)
			tran.SetStatus(err)
		}
		if p != nil {
			tran.SetStatus(p)
		} else {
			tran.SetStatus("0")
		}
		tran.Complete()

		if !ok || err != nil || p != nil {
			http.Error(writer, http.StatusText(404), 404)
		}
	}()
	uri := request.URL.String()
	tran.AddData("url", uri)
	params, ok1 := legalUrl.FindStringSubmatchMap(uri)
	ok = ok1
	if !ok1 {
		l4g.Error("url.unexpected.mark(url:%s)", uri)
		Cat.LogPanic(JoinString("url.unexpected.mark url:", uri))
		return
	}
	store, err1 := FindStorage(params)
	err = err1
	if err1 != nil {
		l4g.Error("%s; rcv(url:%s)", err, request.URL.String())
		return
	}
	chain, err1 := handler.ChainBuilder.Build(params)
	err = err1
	if err1 != nil {
		return
	}

	bts, err1 := store.GetImage()
	err = err1
	if err1 != nil {
		return
	}
	l4g.Debug("get image length(%d) rcv(url:%s)", len(bts), request.URL.String())
	format, _ := params["ext"]
	img := &img4g.Image{Blob: bts, Format: format}
	status := make(chan bool)
	imgHd := imgHandle{img, chain, status}
	imgHandleChan <- imgHd
	timeout := make(chan bool, 1)
	ok = true
	go func() {
		time.Sleep(10 * time.Second) //wait 10s
		timeout <- true
	}()
	select {
	case ok = <-status:
		if !ok {
			return
		}
	case <-timeout:
		ok = false
		return
	}
	writer.Header().Set("Content-Type", "image/"+format)
	writer.Header().Set("Content-Length", strconv.Itoa(len(img.Blob)))
	if _, err = writer.Write(img.Blob); err != nil {
		l4g.Error(err)
		return
	}
}
Example #3
0
		return err
	}
	if err = chain.Process(img); err != nil {
		return err
	}
	if err = img.WriteImageBlob(); err != nil {
		return err
	}
	return nil
}

func FindStorage(params map[string]string) (storage.Storage, error) {
	srcPath, ok := params[":1"]
	if !ok {
		return nil, errors.New("Url.UnExpected")
	}
	format, ok := params["ext"]
	if !ok {
		return nil, errors.New("Image.Ext.Invalid()")
	}
	sourceType, _, path := ParseUri(srcPath)
	return GetStorage(sourceType, path+"."+format)
}

var catimage cat.Cat = cat.Instance()

func log(msg string, err interface{}) {
	l4g.Error("%s -- %s", msg, err)
	catimage.LogPanic(err)
}
Example #4
0
	defer func() {
		if err := recover(); err != nil {
			this.log(JoinString("subprocessor->sendStats(port:", this.Port, ")"), err)
			this.sendStats()
		}
	}()
	for {
		time.Sleep(10 * time.Second)
		uri := JoinString("http://localhost:", this.HostPort, "/heartbeat/")
		l4g.Debug(JoinString("port:", this.Port, " | ", uri))
		status := GetStats()
		data := url.Values{}
		data.Add("port", this.Port)
		for k, v := range status {
			data.Add(k, v)
		}
		_, err := PostHttp(uri, data)
		if err != nil {
			// handle error
			this.log(JoinString("subprocessor->sendStats->posthttp(port:", this.Port, ")"), err)
		}
	}
}

var catsub cat.Cat = cat.Instance()

func (this *SubProcessor) log(msg string, err interface{}) {
	l4g.Error("%s -- %s", msg, err)
	catsub.LogPanic(err)
}
Example #5
0
func (this *HostProcessor) sendCatHeartBeat() {
	defer func() {
		if err := recover(); err != nil {
			l4g.Error(err)
			this.sendCatHeartBeat()
		}
	}()
	ip := GetIP()
	second := time.Now().Second()
	if second < 29 {
		sleep := time.Duration((29 - second) * 1000000000)
		time.Sleep(sleep)
	}

	catinstance := cat.Instance()
	for {
		l4g.Debug("send cat heartbeat")
		stats1 := GetStats()
		data := url.Values{}
		data.Add("port", hostPost)
		for k, v := range stats1 {
			data.Add(k, v)
		}
		portstats[hostPost] = data

		tran := catinstance.NewTransaction("System", "Stats")
		h := catinstance.NewHeartbeat("HeartBeat", ip)
		for _, heart := range portstats {
			if heart == nil {
				continue
			}
			port := heart.Get("port")
			if port == "" {
				continue
			}
			h.Set("System", JoinString("Alloc_", port), heart.Get("Alloc"))
			h.Set("System", JoinString("TotalAlloc_", port), heart.Get("TotalAlloc"))
			h.Set("System", JoinString("Sys_", port), heart.Get("Sys"))
			h.Set("System", JoinString("Mallocs_", port), heart.Get("Mallocs"))
			h.Set("System", JoinString("Frees_", port), heart.Get("Frees"))
			h.Set("System", JoinString("OtherSys_", port), heart.Get("OtherSys"))
			h.Set("System", JoinString("PauseNs_", port), heart.Get("PauseNs"))

			h.Set("HeapUsage", JoinString("HeapAlloc_", port), heart.Get("HeapAlloc"))
			h.Set("HeapUsage", JoinString("HeapSys_", port), heart.Get("HeapSys"))
			h.Set("HeapUsage", JoinString("HeapIdle_", port), heart.Get("HeapIdle"))
			h.Set("HeapUsage", JoinString("HeapInuse_", port), heart.Get("HeapInuse"))
			h.Set("HeapUsage", JoinString("HeapReleased_", port), heart.Get("HeapReleased"))
			h.Set("HeapUsage", JoinString("HeapObjects_", port), heart.Get("HeapObjects"))

			h.Set("GC", JoinString("NextGC_", port), heart.Get("NextGC"))
			h.Set("GC", JoinString("LastGC_", port), heart.Get("LastGC"))
			h.Set("GC", JoinString("NumGC_", port), heart.Get("NumGC"))
			portstats[port] = nil
		}
		h.SetStatus("0")
		h.Complete()
		tran.SetStatus("0")
		tran.Complete()
		second = time.Now().Second()
		sleep := time.Duration((90 - second) * 1000000000)
		time.Sleep(sleep)
	}
}
Example #6
0
	l4g.Debug("listen and serve port:" + hostPost)
	//start server
	http.HandleFunc("/heartbeat/", this.heartbeatHandler)
	if err := http.ListenAndServe(":"+hostPost, nil); err != nil {
		this.log(JoinString("hostprocessor->listenHeartbeat(port:", hostPost, ")"), err)
		os.Exit(1)
	}
}

func (this *HostProcessor) heartbeatHandler(w http.ResponseWriter, request *http.Request) {
	port := request.FormValue("port")
	var value string = "0"
	if port != "" {
		portstats[port] = request.Form
		value = "1"
	}
	l4g.Debug(JoinString("get heartbeat from port[", port, "]"))

	w.Header().Set("Connection", "keep-alive")
	a := []byte(value)
	w.Header().Set("Content-Length", strconv.Itoa(len(a)))
	w.Write(a)
}

var cathost cat.Cat = cat.Instance()

func (this *HostProcessor) log(msg string, err interface{}) {
	l4g.Error("%s -- %s", msg, err)
	cathost.LogPanic(err)
}