Пример #1
0
func (this *Job) initializeJob(name string) {
	tuples := strings.Split(name, ".")
	if len(tuples) != 3 {
		panic("invalid name")
	}

	kws, err := this.zkzone.KatewayInfos()
	swallow(err)
	if len(kws) < 1 {
		panic("no live kateway instance")
	}

	uri := kws[0].ManAddr
	if !strings.HasPrefix(uri, "http:") {
		uri = fmt.Sprintf("http://%s", uri)
	}
	uri = fmt.Sprintf("%s/v1/jobs/%s/%s/%s", uri, tuples[0], tuples[1], tuples[2])
	this.Ui.Info(uri)

	req := gorequest.New()
	resp, _, errs := req.Post(uri).Set("Appid", "_psubAdmin_").Set("Pubkey", "_wandafFan_").End()
	if resp.StatusCode != http.StatusCreated {
		this.Ui.Error(resp.Status)
	} else if len(errs) > 0 {
		for _, err = range errs {
			this.Ui.Error(err.Error())
		}
	} else {
		this.Ui.Info(fmt.Sprintf("%s initialized", name))
	}

}
Пример #2
0
// CallSOS will send SOS message to the zone wide kguard leader.
func (this *ZkZone) CallSOS(caller string, msg string) {
	log.Critical("SOS[%s] %s: sending...", caller, msg)

	// kguard leader might float, so refresh on each SOS message
	kguards, err := this.KguardInfos()
	if err != nil {
		log.Error("SOS[%s] %s: %v", caller, msg, err)
		return
	}

	leader := kguards[0]
	request := gorequest.New().Timeout(time.Second * 10)
	res, body, errs := request.Post(fmt.Sprintf("http://%s:%d", leader.Host, telemetry.SOSPort)).
		Set("User-Agent", fmt.Sprintf("sos-go-%s", gafka.BuildId)).
		Set(telemetry.SOSIdentHeader, caller).
		End()
	if len(errs) > 0 {
		log.Error("SOS[%s] %s: %+v", caller, msg, errs)
		return
	}

	if res.StatusCode != http.StatusAccepted {
		log.Error("SOS[%s] %s: HTTP %s %s", caller, msg, http.StatusText(res.StatusCode), body)
		return

	}

	log.Info("SOS[%s] %s: sent ok", caller, msg)
}
Пример #3
0
func (this *Kguard) showKguardVersion(host string) {
	url := fmt.Sprintf("http://%s:10025/ver", host)
	req := gorequest.New()
	req.Get(url).Set("User-Agent", "gk")
	_, b, errs := req.EndBytes()
	if len(errs) > 0 {
		for _, err := range errs {
			this.Ui.Error(err.Error())
		}
		return
	}

	this.Ui.Output(fmt.Sprintf("kguard ver: %s", string(b)))
}
Пример #4
0
func (this *Kguard) showStats(host string) {
	url := fmt.Sprintf("http://%s:10025/metrics", host)
	req := gorequest.New()
	req.Get(url).Set("User-Agent", "gk")
	_, b, errs := req.EndBytes()
	if len(errs) > 0 {
		for _, err := range errs {
			this.Ui.Error(err.Error())
		}
		return
	}

	var prettyJSON bytes.Buffer
	json.Indent(&prettyJSON, b, "", "    ")
	this.Ui.Output(prettyJSON.String())
}
Пример #5
0
// SubX is advanced Sub with features of delayed ack and shadow bury.
func (this *Client) SubX(opt SubOption, h SubXHandler) error {
	var u url.URL
	u.Scheme = this.cf.Sub.Scheme
	u.Host = this.cf.Sub.Endpoint
	u.Path = fmt.Sprintf("/v1/msgs/%s/%s/%s", opt.AppId, opt.Topic, opt.Ver)
	q := u.Query()
	q.Set("group", opt.Group)
	q.Set("ack", "1")
	if opt.Shadow != "" && sla.ValidateShadowName(opt.Shadow) {
		q.Set("q", opt.Shadow)
	}
	if opt.Reset != "" {
		q.Set("reset", opt.Reset)
	}
	if opt.Batch > 1 {
		q.Set("batch", strconv.Itoa(opt.Batch))
	}
	if opt.Wait != "" {
		q.Set("wait", opt.Wait)
	}
	u.RawQuery = q.Encode()

	req := gorequest.New()
	req.Get(u.String()).
		Set(gateway.HttpHeaderAppid, this.cf.AppId).
		Set(gateway.HttpHeaderSubkey, this.cf.Secret).
		Set("User-Agent", UserAgent).
		Set(gateway.HttpHeaderPartition, "-1").
		Set(gateway.HttpHeaderOffset, "-1")
	r := &SubXResult{}
	for {
		response, b, errs := req.EndBytes()
		if len(errs) > 0 {
			return errs[0]
		}

		// reset the request header
		req.Set(gateway.HttpHeaderPartition, "")
		req.Set(gateway.HttpHeaderOffset, "")
		req.Set(gateway.HttpHeaderMsgBury, "")

		if this.cf.Debug {
			log.Printf("--> [%s]", response.Status)
			log.Printf("Partition:%s Offset:%s",
				response.Header.Get(gateway.HttpHeaderPartition),
				response.Header.Get(gateway.HttpHeaderOffset))
		}

		r.Reset()
		r.Partition = response.Header.Get(gateway.HttpHeaderPartition)
		r.Offset = response.Header.Get(gateway.HttpHeaderOffset)
		r.Tag = response.Header.Get(gateway.HttpHeaderMsgTag)
		if err := h(response.StatusCode, b, r); err != nil {
			return err
		}

		req.Set(gateway.HttpHeaderPartition, r.Partition)
		req.Set(gateway.HttpHeaderOffset, r.Offset)

		if r.Bury != "" {
			if r.Bury != ShadowRetry && r.Bury != ShadowDead {
				return ErrInvalidBury
			}

			req.Set("X-Bury", r.Bury)
		}
	}

}