Exemple #1
0
func (this *electServer) doJoin(r *http.Request) error {
	var command raft.DefaultJoinCommand
	if err := json.NewDecoder(r.Body).Decode(&command); err != nil {
		return web.HTTPError{400, "MSG_ERROR"}
	}
	_, err := this.raftServer.Do(&command)
	if err != nil {
		log.Logf("raft do faild : %+v", err.Error())
		return err
	}
	log.Logf("raft join success")
	return nil
}
Exemple #2
0
func (this *electServer) JoinSelf() (err error) {
	log.Logf("attemping to join self")
	_, err = this.raftServer.Do(&raft.DefaultJoinCommand{
		Name:             this.Name,
		ConnectionString: this.HttpAddr(),
	})
	return
}
Exemple #3
0
func (this *Sheep) Start() (err error) {
	log.Logf("sheep start : %+v", this.Name)
	if err = this.Config.InitAll(); err != nil {
		return
	}

	this.Status = constant.STATUS_STARTING

	err = this.cmd.Start()
	if err != nil {
		this.Status = constant.STATUS_START_FAILED
		log.Errorf("start faild:%s:%+v", err.Error(), *this)
		return err
	}

	log.Logf("sheep %s started", this.Name)
	go this.watch()
	this.Status = constant.STATUS_STARTED
	time.Sleep(time.Second)
	return nil
}
Exemple #4
0
func (this *electServer) JoinLeader(leader string) (err error) {
	log.Logf("attemping to join leader : %s", leader)
	command := raft.DefaultJoinCommand{
		Name:             this.Name,
		ConnectionString: this.HttpAddr(),
	}
	var b bytes.Buffer
	if err = json.NewEncoder(&b).Encode(command); err != nil {
		return
	}
	resp, err := http.Post("http://"+leader+"/join", "application/json", &b)
	if err != nil {
		return
	}
	defer resp.Body.Close()
	if resp.StatusCode != 200 {
		return fmt.Errorf("bad status code : %+v", resp.StatusCode)
	}
	return
}
Exemple #5
0
func (this *Sheep) Stop() (err error) {
	if this.cmd.Process == nil {
		return errors.New(fmt.Sprintf("%s not started.", this.Name))
	}

	if this.cmd.ProcessState == nil {
		log.Log("process state unvailable")
	} else if this.cmd.ProcessState.Exited() {
		return errors.New(fmt.Sprintf("%s has stoped.", this.Name))
	}

	this.Status = constant.STATUS_STOPING
	log.Logf("signal:%d", this.StopSignal)
	if err = this.cmd.Process.Signal(syscall.Signal(this.StopSignal)); err == nil {
		this.Status = constant.STATUS_STOPPED
	} else {
		log.Error("stop faild:", err.Error())
	}
	return err
}