コード例 #1
0
ファイル: sheep.go プロジェクト: luckykris/shepherd
//do Assign Job and check
func (self *SHEEP) DoAssign(info string, isbypass bool) (bool, interface{}) {
	var err error
	if isbypass {
		reply, err := protocol.Request(self.URL, protocol.HeatbeatMethod, mystruct.JOBINFO{ID: 0, INFO: info})
		if err != nil {
			return false, err
		}
		var success bool
		if reply.STATUS == "1" {
			success = true
		} else {
			success = false
		}
		return success, reply.MESSAGE
	}
	if self.JOBQ.Len() >= self.CAPACITY {
		log4go.Logger.Printf("[ERROR]Sheep '%s'`s job queue is full,new job has been dropped.", self.NAME)
		return false, fmt.Errorf("the sheep '%s'`s queue is full,job is dropped. ", self.NAME)
	}
	thisId := atomic.AddInt64(&self.AUTOID, 1) //auto increase sheep.AUTOID
	if self.PERSIST {
		err = self.JDQ.Put([]byte(info), false)
		if err != nil {
			log4go.Logger.Printf("[ERROR]Sheep '%s'`s disk queue write failed,job.", self.NAME)
			return false, fmt.Errorf("the sheep '%s'`s disk queue write failed,job. ", self.NAME)
		}
	}
	self.JOBQ.Push(mystruct.JOBINFO{ID: thisId, INFO: info})
	return true, nil
}
コード例 #2
0
ファイル: sheep.go プロジェクト: luckykris/shepherd
func (self *SHEEP) Heartbeat() bool {
	job, err := algorithm.FailoverAlgorithm(self.JOBQ, self.FOQ, self.LASTFAIL, self.JDQ, self.FDQ, self.PERSIST)
	if err != nil {
		log4go.Logger.Printf("[ERROR]Algorithm error :%s", err.Error())
	}
	reply, err := protocol.Request(self.URL, protocol.HeatbeatMethod, job)
	switch {
	case err != nil:
		err = self._FailoverQueueManage(job)
		if err != nil {
			log4go.Logger.Printf("[ERROR]Sheep '%s' status is persist failed", self.NAME)
			return false
		}
		if self.STATUS != 2 && self.RETRY >= cfg.RETRY {
			self.GoDead(2)
			log4go.Logger.Printf("[ERROR]Sheep '%s' status is 'no heartbeat'", self.NAME)
		} else {
			self.RETRY += 1
		}
	case reply.STATUS == "0": //when reply is '0'  ,the request was failed.
		err = self._FailoverQueueManage(job)
		if err != nil {
			log4go.Logger.Printf("[ERROR]Sheep '%s' status is persist failed", self.NAME)
			return false
		}
		if self.STATUS != 3 && self.RETRY >= cfg.RETRY {
			self.GoDead(3)
			log4go.Logger.Printf("[ERROR]Sheep '%s' status is 'invalue data'", self.NAME)
		} else {
			self.RETRY += 1
		}
	default:
		err = self._CorrectQueueManage()
		if err != nil {
			log4go.Logger.Printf("[ERROR]Sheep '%s' status is persist failed:'%s'", self.NAME, err.Error())
			return false
		}
		if self.STATUS != 0 {
			self.ComeAlive()
			log4go.Logger.Printf("[INFO]Sheep '%s' status is 'ok'", self.NAME)
		}
		self.RETRY = 0
		if job.ID != -1 {
			return true
		}
	}
	return false
}