示例#1
0
func _BlockProcedure(jq, foq *Queue.Queue, lf interface{}, jdq, fdq *diskq.DiskQueue, persist bool) (mystruct.JOBINFO, error) {
	switch {
	case lf != nil:
		return lf.(mystruct.JOBINFO), nil
	case jq.Len() > 0:
		if persist {
			_, err := jdq.Get(false)
			if err != nil {
				return mystruct.JOBINFO{ID: -1, INFO: ""}, err
			}
		}
		jobInfo := jq.Pop()
		return jobInfo.(mystruct.JOBINFO), nil
	}
	return mystruct.JOBINFO{ID: -1, INFO: ""}, nil //no job to do,send a heartbeat job
}
示例#2
0
func New(c cfg.SHEEPCFG) *SHEEP {
	var jdq = new(diskq.DiskQueue)
	var fdq = new(diskq.DiskQueue)
	var jobq = Queue.NewQueue(c.CAPACITY)
	var foq = Queue.NewQueue(c.CAPACITY)
	var err error
	log4go.Logger.Printf("[INFO]create a new sheep,Name:%s Host:%s Port:%d .  ", c.NAME, c.HOST, c.PORT)
	s := SHEEP{
		NAME:        c.NAME,
		HOST:        c.HOST,
		PORT:        c.PORT,
		GROUPID:     0,
		STATUS:      1,
		RETRY:       0,
		ENABLE:      c.ENABLE,
		LAST_TIME:   0,
		REGIST_TIME: (time.Now()).Unix(),
		CAPACITY:    c.CAPACITY,
		JOBQ:        jobq,
		FOQ:         foq,
		LASTFAIL:    nil,
		AUTOID:      0,
		URL:         fmt.Sprintf("%s:%d", c.HOST, c.PORT),
		JDQ:         jdq,
		FDQ:         fdq,
		PERSIST:     c.PERSIST,
	}
	if c.PERSIST {
		jdq, err = diskq.New(c.NAME+"_jdq", cfg.DATAPATH, int64(c.CAPACITY), int64(cfg.PERFILESIZE))
		fdq, err = diskq.New(c.NAME+"_fdq", cfg.DATAPATH, int64(c.CAPACITY), int64(cfg.PERFILESIZE))
		if err != nil {
			log4go.Logger.Printf("[ERROR]Sheep '%s' Create Disk Queue error :%s", c.NAME, err.Error())
			os.Exit(-1)
		}
		log4go.Logger.Printf("[INFO]Sheep '%s' Create Disk Queue success", c.NAME)
		err = jdq.LoadQueue()
		if err != nil {
			log4go.Logger.Printf("[WARNING]Sheep '%s' Load Disk Queue error :%s", c.NAME, err.Error())
		} else {
			qls, err := jdq.SnapShot()
			if err != nil {
				log4go.Logger.Printf("[ERROR]Sheep '%s' Init  Queue error :%s", c.NAME, err.Error())
			} else {
				if len(qls) > s.CAPACITY {
					log4go.Logger.Printf("[ERROR]Sheep '%s' Init  Queue error :%s", c.NAME, "history in disk queue is more than sheep`s capacity.")
					os.Exit(-1)
				}
				for i := 0; i < len(qls); i++ {
					thisId := atomic.AddInt64(&s.AUTOID, 1)
					s.JOBQ.Push(mystruct.JOBINFO{ID: thisId, INFO: string(qls[i])})
				}
			}
		}
		log4go.Logger.Printf("[INFO]Sheep '%s' Load job Queue from disk success", c.NAME)
		err = fdq.LoadQueue()
		thisId := atomic.AddInt64(&s.AUTOID, 1)
		tmp, err := fdq.GetLastFail()
		if err != nil {
			log4go.Logger.Printf("[WARNING]Sheep '%s' Init  last failed error :%s", c.NAME, err.Error())
			//os.Exit(-1)
		}
		if tmp != nil {
			s.LASTFAIL = mystruct.JOBINFO{ID: thisId, INFO: tmp.(string)}
		}
		if err != nil {
			log4go.Logger.Printf("[WARNING]Sheep '%s' Load Disk Queue error :%s", c.NAME, err.Error())
		} else {
			qls, err := fdq.SnapShot()
			if err != nil {
				log4go.Logger.Printf("[ERROR]Sheep '%s' Init  Queue error :%s", c.NAME, err.Error())
			} else {
				if len(qls) > s.CAPACITY {
					log4go.Logger.Printf("[ERROR]Sheep '%s' Init  Queue error :%s", c.NAME, "history in disk queue is more than sheep`s capacity.")
					os.Exit(-1)
				}
				for i := 0; i < len(qls); i++ {
					thisId := atomic.AddInt64(&s.AUTOID, 1)
					s.FOQ.Push(mystruct.JOBINFO{ID: thisId, INFO: string(qls[i])})
				}
			}
		}
		s.FDQ = fdq
		s.JDQ = jdq
	}
	return &s
}